Skip to content
Start here

Create a policy

POST/accounts/{account_id}/ai-gateway/gateways/{gateway_id}/policies

Creates a policy on a gateway. It applies to requests as soon as it’s created. A gateway can have up to 100 policies.

Security
API Token

The preferred authorization scheme for interacting with the Cloudflare API. Create a token.

Example:Authorization: Bearer Sn3lZJTBX6kkg7OdcBUAxOO963GEIyGQqnFTOFYY
API Email + API Key

The previous authorization scheme for interacting with the Cloudflare API, used in conjunction with a Global API key.

Example:X-Auth-Email: user@example.com

The previous authorization scheme for interacting with the Cloudflare API. When possible, use API tokens instead of Global API keys.

Example:X-Auth-Key: 144c9defac04969c7bfad8efaa8ea194
Accepted Permissions (at least one required)
AI Search Index EngineAuto Rag Write Run EngineAI Gateway Write
Path ParametersExpand Collapse
account_id: string

The account ID.

gateway_id: string

The gateway ID.

Body ParametersJSONExpand Collapse
action: object { type } or object { type, except } or object { amount, technique, type, 3 more }

What the policy does to matching requests.

One of the following:
Type object { type }
type: "no-limit"

Allow matching requests with no limit.

object { type, except }
type: "block"

Block matching requests.

except: optional boolean

When true, block requests that do not match resource instead.

object { amount, technique, type, 3 more }
amount: number

The spend limit in USD.

exclusiveMinimum
minimum0
technique: "fixed" or "sliding" or "named-interval"

How the limit resets: sliding covers the last window seconds, fixed resets every window seconds, and named-interval resets at the start of each period.

One of the following:
"fixed"
"sliding"
"named-interval"
type: "limit"

Limit spend over a time window or a calendar period.

alerts: optional array of object { percentage }

Up to 5 alerts. Only for named-interval.

percentage: number

The share of the limit, in whole percent, at which to alert.

maximum100
minimum1
period: optional "monthly" or "weekly" or "daily" or "hourly"

A UTC calendar period. Required for named-interval, and only for it.

One of the following:
"monthly"
"weekly"
"daily"
"hourly"
window: optional number

The window length in seconds. Required for fixed and sliding, and only for them.

exclusiveMinimum
minimum0
actor: string

Who the policy applies to: a template whose {…} parts are CEL, such as {cf.user.email}. Each value gets its own spend limit.

condition: string

A CEL expression over cf and metadata; the policy applies when it is true.

name: string

A label for the policy.

resource: object { models, providers }

The requests the policy applies to. Omit both fields to match every request.

models: optional array of string

The models to match. Omit to match every model.

providers: optional array of string

The providers to match. Omit to match every provider. With models, list exactly one.

enabled: optional boolean

Whether the policy applies to requests. Defaults to true.

ReturnsExpand Collapse
result: object { id, action, actor, 6 more }

The policy.

id: string

The policy ID.

formatuuid
action: object { type } or object { type, except } or object { amount, technique, type, 3 more }

What the policy does to matching requests.

One of the following:
Type object { type }
type: "no-limit"

Allow matching requests with no limit.

object { type, except }
type: "block"

Block matching requests.

except: optional boolean

When true, block requests that do not match resource instead.

object { amount, technique, type, 3 more }
amount: number

The spend limit in USD.

exclusiveMinimum
minimum0
technique: "fixed" or "sliding" or "named-interval"

How the limit resets: sliding covers the last window seconds, fixed resets every window seconds, and named-interval resets at the start of each period.

One of the following:
"fixed"
"sliding"
"named-interval"
type: "limit"

Limit spend over a time window or a calendar period.

alerts: optional array of object { percentage }

Up to 5 alerts. Only for named-interval.

percentage: number

The share of the limit, in whole percent, at which to alert.

maximum100
minimum1
period: optional "monthly" or "weekly" or "daily" or "hourly"

A UTC calendar period. Required for named-interval, and only for it.

One of the following:
"monthly"
"weekly"
"daily"
"hourly"
window: optional number

The window length in seconds. Required for fixed and sliding, and only for them.

exclusiveMinimum
minimum0
actor: string

Who the policy applies to: a template whose {…} parts are CEL, such as {cf.user.email}. Each value gets its own spend limit.

condition: string

A CEL expression over cf and metadata; the policy applies when it is true.

created_at: string

When the policy was created.

formatdate-time
enabled: boolean

Whether the policy applies to requests.

modified_at: string

When the policy was last changed.

formatdate-time
name: string

A label for the policy.

resource: object { models, providers }

The requests the policy applies to. Omit both fields to match every request.

models: optional array of string

The models to match. Omit to match every model.

providers: optional array of string

The providers to match. Omit to match every provider. With models, list exactly one.

success: true

Whether the request succeeded.

Create a policy

curl https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/ai-gateway/gateways/$GATEWAY_ID/policies \
    -H 'Content-Type: application/json' \
    -H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
    -d '{
          "action": {
            "type": "no-limit"
          },
          "actor": "actor",
          "condition": "condition",
          "name": "name",
          "resource": {}
        }'
{
  "result": {
    "id": "182bd5e5-6e1a-4fe4-a799-aa6d9a6ab26e",
    "action": {
      "type": "no-limit"
    },
    "actor": "actor",
    "condition": "condition",
    "created_at": "2019-12-27T18:11:19.117Z",
    "enabled": true,
    "modified_at": "2019-12-27T18:11:19.117Z",
    "name": "name",
    "resource": {
      "models": [
        "string"
      ],
      "providers": [
        "string"
      ]
    }
  },
  "success": true
}
Returns Examples
{
  "result": {
    "id": "182bd5e5-6e1a-4fe4-a799-aa6d9a6ab26e",
    "action": {
      "type": "no-limit"
    },
    "actor": "actor",
    "condition": "condition",
    "created_at": "2019-12-27T18:11:19.117Z",
    "enabled": true,
    "modified_at": "2019-12-27T18:11:19.117Z",
    "name": "name",
    "resource": {
      "models": [
        "string"
      ],
      "providers": [
        "string"
      ]
    }
  },
  "success": true
}