---
title: Create or replace an AI Gateway routing policy
description: Create or replace the routing policy document for an inbound pattern. Pass user_id to scope the policy to a user.
breadcrumbs: Docs > API > Create or replace an AI Gateway routing policy
---

> For the complete documentation index, see [llms.txt](https://docs.datadoghq.com/llms.txt).

# Create or replace an AI Gateway routing policy

## v2 (latest)

{% alert level="warning" %}
**Note**: This endpoint is in Preview and is subject to change.
If you have any feedback, contact [Datadog support](https://docs.datadoghq.com/help.md).
{% /alert %}

| Datadog site      | API endpoint                                       |
| ----------------- | -------------------------------------------------- |
| app.datadoghq.com | POST https://api.datadoghq.com/api/v2/policies     |
| us3.datadoghq.com | POST https://api.us3.datadoghq.com/api/v2/policies |
| us5.datadoghq.com | POST https://api.us5.datadoghq.com/api/v2/policies |
| app.datadoghq.eu  | POST https://api.datadoghq.eu/api/v2/policies      |
| ap1.datadoghq.com | POST https://api.ap1.datadoghq.com/api/v2/policies |
| ap2.datadoghq.com | POST https://api.ap2.datadoghq.com/api/v2/policies |
| uk1.datadoghq.com | POST https://api.uk1.datadoghq.com/api/v2/policies |
| app.ddog-gov.com  | POST https://api.ddog-gov.com/api/v2/policies      |
| us2.ddog-gov.com  | POST https://api.us2.ddog-gov.com/api/v2/policies  |

### Overview

Create or replace the routing policy document for an inbound pattern. Pass user_id to scope the policy to a user.

This endpoint requires the `ai_gateway_config_write` permission.

OAuth apps require the `ai_gateway_config_write` authorization [scope](https://docs.datadoghq.com/api/latest/scopes.md#ai-gateway) to access this endpoint.

### Arguments

#### Query Strings

| Field   | Type   | Description                                                                       |
| ------- | ------ | --------------------------------------------------------------------------------- |
| user_id | string | Filter or scope policies to a specific user. Omit for organization-wide policies. |

### Request

#### Body Data (required)

{% tabs %}
{% tab label="Model" %}
| Parent field | Field                   | Type     | Description                                                                                                                             |
| ------------ | ----------------------- | -------- | --------------------------------------------------------------------------------------------------------------------------------------- |
|              | data [*required*]       | object   | Data used to create or replace a routing policy document.                                                                               |
| data         | attributes [*required*] | object   | Attributes used to create or replace a routing policy document.                                                                         |
| attributes   | inbound [*required*]    | string   | Inbound request pattern.                                                                                                                |
| attributes   | priority                | int64    | Routing priority.                                                                                                                       |
| attributes   | tiers [*required*]      | [object] |                                                                                                                                         |
| tiers        | priority                | int64    | Routing priority.                                                                                                                       |
| tiers        | targets [*required*]    | [object] |                                                                                                                                         |
| targets      | id                      | int64    | Saved target row ID. On replacement, retain this ID only within its original route and at most once. A stale or foreign ID is rejected. |
| targets      | model                   | string   | Model name or pattern. Omitted when retaining a saved target by ID or when its provider is hidden.                                      |
| targets      | provider                | string   | Provider slug. Omitted for dynamic resolution or when its provider is hidden.                                                           |
| data         | type [*required*]       | enum     | AI Gateway routing policy resource type. Allowed enum values: `routing-documents`.                                                      |
{% /tab %}

{% tab label="Example" %}
```json
{
  "data": {
    "attributes": {
      "inbound": "coding-agent",
      "priority": 1,
      "tiers": [
        {
          "priority": 1,
          "targets": [
            {
              "id": 901,
              "model": "gpt-5.2-codex",
              "provider": "prod-us"
            }
          ]
        }
      ]
    },
    "type": "routing-documents"
  }
}
```
{% /tab %}
{% /tabs %}

### Response

{% tabs %}
{% tab label="200" %}
OK

{% tabs %}
{% tab label="Model" %}
Response containing one routing policy document.

| Parent field | Field                   | Type     | Description                                                                                                                             |
| ------------ | ----------------------- | -------- | --------------------------------------------------------------------------------------------------------------------------------------- |
|              | data [*required*]       | object   | AI Gateway routing policy resource.                                                                                                     |
| data         | attributes [*required*] | object   | Routing policy configuration.                                                                                                           |
| attributes   | priority [*required*]   | int64    | Routing priority.                                                                                                                       |
| attributes   | tiers [*required*]      | [object] |                                                                                                                                         |
| tiers        | targets [*required*]    | [object] |                                                                                                                                         |
| targets      | id [*required*]         | int64    | Saved target row ID. On replacement, retain this ID only within its original route and at most once. A stale or foreign ID is rejected. |
| targets      | model                   | string   | Model name or pattern. Omitted when retaining a saved target by ID or when its provider is hidden.                                      |
| targets      | priority [*required*]   | int64    | Routing priority.                                                                                                                       |
| targets      | provider                | string   | Provider slug. Omitted for dynamic resolution or when its provider is hidden.                                                           |
| data         | id [*required*]         | string   | Resource identifier.                                                                                                                    |
| data         | type [*required*]       | enum     | AI Gateway routing policy resource type. Allowed enum values: `routing-documents`.                                                      |
{% /tab %}

{% tab label="Example" %}
```json
{
  "data": {
    "attributes": {
      "priority": 1,
      "tiers": [
        {
          "targets": [
            {
              "id": 901,
              "model": "gpt-5.2-codex",
              "priority": 1,
              "provider": "prod-us"
            }
          ]
        }
      ]
    },
    "id": "coding-agent",
    "type": "routing-documents"
  }
}
```
{% /tab %}
{% /tabs %}
{% /tab %}

{% tab label="400" %}
Bad Request

{% tabs %}
{% tab label="Model" %}
API error response.

| Field               | Type     | Description       |
| ------------------- | -------- | ----------------- |
| errors [*required*] | [string] | A list of errors. |
{% /tab %}

{% tab label="Example" %}
```json
{
  "errors": [
    "Bad Request"
  ]
}
```
{% /tab %}
{% /tabs %}
{% /tab %}

{% tab label="429" %}
Too many requests

{% tabs %}
{% tab label="Model" %}
API error response.

| Field               | Type     | Description       |
| ------------------- | -------- | ----------------- |
| errors [*required*] | [string] | A list of errors. |
{% /tab %}

{% tab label="Example" %}
```json
{
  "errors": [
    "Bad Request"
  ]
}
```
{% /tab %}
{% /tabs %}
{% /tab %}
{% /tabs %}

### Code Example

{% tabs %}
{% tab label="Curl" %}
```bash
# Set your Datadog site and credentials
export DD_SITE="datadoghq.com" # Or us3.datadoghq.com, us5.datadoghq.com, datadoghq.eu, ap1.datadoghq.com, ap2.datadoghq.com, uk1.datadoghq.com, ddog-gov.com, us2.ddog-gov.com
# Use a Personal Access Token or Service Access Token
export DD_BEARER_TOKEN="<PERSONAL_ACCESS_TOKEN OR SERVICE_ACCESS_TOKEN>"

curl -X POST "https://api.${DD_SITE}/api/v2/policies" \
-H "Accept: application/json" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${DD_BEARER_TOKEN}" \
-d @- << EOF
{
  "data": {
    "attributes": {
      "inbound": "coding-agent",
      "priority": 1,
      "tiers": [
        {
          "priority": 1,
          "targets": [
            {
              "id": 901,
              "model": "gpt-5.2-codex",
              "provider": "prod-us"
            }
          ]
        }
      ]
    },
    "type": "routing-documents"
  }
}
EOF
```
{% /tab %}
{% /tabs %}
