---
title: Create a token validation rule
---

[Skip to content](#_top)

[API Reference](https://developers.cloudflare.com/api/go)

[Token Validation](https://developers.cloudflare.com/api/go/resources/token_validation)

[Rules](https://developers.cloudflare.com/api/go/resources/token_validation/subresources/rules)

Copy Markdown

Open in **Claude**Open in **ChatGPT**Open in **Cursor**

---

**Copy Markdown****View as Markdown**

# Create a token validation rule

client.TokenValidation.Rules.New(ctx, params) (\*[TokenValidationRule](<https://developers.cloudflare.com/api/go/resources/token_validation#(resource)%20token_validation.rules%20%3E%20(model)%20token_validation_rule%20%3E%20(schema)>), error)

POST/zones/{zone\_id}/token\_validation/rules

Creates a token validation rule for the zone.

##### Security

<details>

<summary>API Token</summary>



The preferred authorization scheme for interacting with the Cloudflare API. <a href="https://developers.cloudflare.com/fundamentals/api/get-started/create-token/">Create a token</a>.

**Example:**<code>Authorization: Bearer Sn3lZJTBX6kkg7OdcBUAxOO963GEIyGQqnFTOFYY</code>

</details>

<details>

<summary>API Email + API Key</summary>



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

**Example:**<code>X-Auth-Email: user@example.com</code>

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

**Example:**<code>X-Auth-Key: 144c9defac04969c7bfad8efaa8ea194</code>

</details>

##### Accepted Permissions (at least one required)

`Account API Gateway``Domain API Gateway`

##### ParametersExpand Collapse

<details>

<summary>

params RuleNewParams

</summary>

ZoneID param.Field\[string]

Path param: Identifier.

maxLength32

<a href="#(resource)%20token_validation.rules%20%3E%20(method)%20create%20%3E%20(params)%20default%20%3E%20(param)%20zone_id">Link to this property</a>

<details>

<summary>

Action param.Field\[<a href="https://developers.cloudflare.com/api/go/resources/token_validation/subresources/rules/methods/create#(resource)%20token_validation.rules%20%3E%20(method)%20create%20%3E%20(params)%20default%20%3E%20(param)%20action%20%3E%20(schema)">RuleNewParamsAction</a>]

Body param: Action to take on requests that match operations included in <code>selector</code> and fail <code>expression</code>.

</summary>

const RuleNewParamsActionLog <a href="https://developers.cloudflare.com/api/go/resources/token_validation/subresources/rules/methods/create#(resource)%20token_validation.rules%20%3E%20(method)%20create%20%3E%20(params)%20default%20%3E%20(param)%20action%20%3E%20(schema)">RuleNewParamsAction</a> = "log"

<a href="#(resource)%20token_validation.rules%20%3E%20(method)%20create%20%3E%20(params)%20default%20%3E%20(param)%20action%20%3E%20(schema)%20%3E%20(member)%200">Link to this property</a>

const RuleNewParamsActionBlock <a href="https://developers.cloudflare.com/api/go/resources/token_validation/subresources/rules/methods/create#(resource)%20token_validation.rules%20%3E%20(method)%20create%20%3E%20(params)%20default%20%3E%20(param)%20action%20%3E%20(schema)">RuleNewParamsAction</a> = "block"

<a href="#(resource)%20token_validation.rules%20%3E%20(method)%20create%20%3E%20(params)%20default%20%3E%20(param)%20action%20%3E%20(schema)%20%3E%20(member)%201">Link to this property</a>

</details>

<a href="#(resource)%20token_validation.rules%20%3E%20(method)%20create%20%3E%20(params)%20default%20%3E%20(param)%20action">Link to this property</a>

Description param.Field\[string]

Body param: A human-readable description that gives more details than <code>title</code>.

maxLength500

<a href="#(resource)%20token_validation.rules%20%3E%20(method)%20create%20%3E%20(params)%20default%20%3E%20(param)%20description">Link to this property</a>

Enabled param.Field\[bool]

Body param: Toggle rule on or off.

<a href="#(resource)%20token_validation.rules%20%3E%20(method)%20create%20%3E%20(params)%20default%20%3E%20(param)%20enabled">Link to this property</a>

Expression param.Field\[string]

Body param: Rule expression. Requests that fail to match this expression will be subject to <code>action</code>.

For details on expressions, see the <a href="https://developers.cloudflare.com/api-shield/security/jwt-validation/">Cloudflare Docs</a>.

<a href="#(resource)%20token_validation.rules%20%3E%20(method)%20create%20%3E%20(params)%20default%20%3E%20(param)%20expression">Link to this property</a>

<details>

<summary>

Selector param.Field\[<a href="https://developers.cloudflare.com/api/go/resources/token_validation/subresources/rules/methods/create#(resource)%20token_validation.rules%20%3E%20(method)%20create%20%3E%20(params)%20default%20%3E%20(param)%20selector%20%3E%20(schema)">RuleNewParamsSelector</a>]

Body param: Select operations covered by this rule.

For details on selectors, see the <a href="https://developers.cloudflare.com/api-shield/security/jwt-validation/">Cloudflare Docs</a>.

</summary>

<details>

<summary>

Exclude \[]RuleNewParamsSelectorExcludeOptional

Ignore operations that were otherwise included by <code>include</code>.

</summary>

OperationIDs \[]stringOptional

Excluded operation IDs.

<a href="#(resource)%20token_validation.rules%20%3E%20(method)%20create%20%3E%20(params)%20default%20%3E%20(param)%20selector%20%3E%20(schema)%20%3E%20(property)%20exclude%20%3E%20(items)%20%3E%20(property)%20operation_ids">Link to this property</a>

</details>

<a href="#(resource)%20token_validation.rules%20%3E%20(method)%20create%20%3E%20(params)%20default%20%3E%20(param)%20selector%20%3E%20(schema)%20%3E%20(property)%20exclude">Link to this property</a>

<details>

<summary>

Include \[]RuleNewParamsSelectorIncludeOptional

Select all matching operations.

</summary>

Host \[]stringOptional

Included hostnames.

<a href="#(resource)%20token_validation.rules%20%3E%20(method)%20create%20%3E%20(params)%20default%20%3E%20(param)%20selector%20%3E%20(schema)%20%3E%20(property)%20include%20%3E%20(items)%20%3E%20(property)%20host">Link to this property</a>

</details>

<a href="#(resource)%20token_validation.rules%20%3E%20(method)%20create%20%3E%20(params)%20default%20%3E%20(param)%20selector%20%3E%20(schema)%20%3E%20(property)%20include">Link to this property</a>

</details>

<a href="#(resource)%20token_validation.rules%20%3E%20(method)%20create%20%3E%20(params)%20default%20%3E%20(param)%20selector">Link to this property</a>

Title param.Field\[string]

Body param: A human-readable name for the rule.

maxLength50

<a href="#(resource)%20token_validation.rules%20%3E%20(method)%20create%20%3E%20(params)%20default%20%3E%20(param)%20title">Link to this property</a>

</details>

[Link to this property](<#(resource)%20token_validation.rules%20%3E%20(method)%20create%20%3E%20(params)%20default>)

##### ReturnsExpand Collapse

<details>

<summary>

type TokenValidationRule struct{…}

A Token Validation rule that can enforce security policies using JWT Tokens.

</summary>

<details>

<summary>

Action TokenValidationRuleAction

Action to take on requests that match operations included in <code>selector</code> and fail <code>expression</code>.

</summary>

One of the following:

const TokenValidationRuleActionLog TokenValidationRuleAction = "log"

<a href="#(resource)%20token_validation.rules%20%3E%20(model)%20token_validation_rule%20%3E%20(schema)%20%3E%20(property)%20action%20%3E%20(member)%200">Link to this property</a>

const TokenValidationRuleActionBlock TokenValidationRuleAction = "block"

<a href="#(resource)%20token_validation.rules%20%3E%20(model)%20token_validation_rule%20%3E%20(schema)%20%3E%20(property)%20action%20%3E%20(member)%201">Link to this property</a>

</details>

<a href="#(resource)%20token_validation.rules%20%3E%20(model)%20token_validation_rule%20%3E%20(schema)%20%3E%20(property)%20action">Link to this property</a>

Description string

A human-readable description that gives more details than <code>title</code>.

maxLength500

<a href="#(resource)%20token_validation.rules%20%3E%20(model)%20token_validation_rule%20%3E%20(schema)%20%3E%20(property)%20description">Link to this property</a>

Enabled bool

Toggle rule on or off.

<a href="#(resource)%20token_validation.rules%20%3E%20(model)%20token_validation_rule%20%3E%20(schema)%20%3E%20(property)%20enabled">Link to this property</a>

Expression string

Rule expression. Requests that fail to match this expression will be subject to <code>action</code>.

For details on expressions, see the <a href="https://developers.cloudflare.com/api-shield/security/jwt-validation/">Cloudflare Docs</a>.

<a href="#(resource)%20token_validation.rules%20%3E%20(model)%20token_validation_rule%20%3E%20(schema)%20%3E%20(property)%20expression">Link to this property</a>

<details>

<summary>

Selector TokenValidationRuleSelector

Select operations covered by this rule.

For details on selectors, see the <a href="https://developers.cloudflare.com/api-shield/security/jwt-validation/">Cloudflare Docs</a>.

</summary>

<details>

<summary>

Exclude \[]TokenValidationRuleSelectorExcludeOptional

Ignore operations that were otherwise included by <code>include</code>.

</summary>

OperationIDs \[]stringOptional

Excluded operation IDs.

<a href="#(resource)%20token_validation.rules%20%3E%20(model)%20token_validation_rule%20%3E%20(schema)%20%3E%20(property)%20selector%20%3E%20(property)%20exclude%20%3E%20(items)%20%3E%20(property)%20operation_ids">Link to this property</a>

</details>

<a href="#(resource)%20token_validation.rules%20%3E%20(model)%20token_validation_rule%20%3E%20(schema)%20%3E%20(property)%20selector%20%3E%20(property)%20exclude">Link to this property</a>

<details>

<summary>

Include \[]TokenValidationRuleSelectorIncludeOptional

Select all matching operations.

</summary>

Host \[]stringOptional

Included hostnames.

<a href="#(resource)%20token_validation.rules%20%3E%20(model)%20token_validation_rule%20%3E%20(schema)%20%3E%20(property)%20selector%20%3E%20(property)%20include%20%3E%20(items)%20%3E%20(property)%20host">Link to this property</a>

</details>

<a href="#(resource)%20token_validation.rules%20%3E%20(model)%20token_validation_rule%20%3E%20(schema)%20%3E%20(property)%20selector%20%3E%20(property)%20include">Link to this property</a>

</details>

<a href="#(resource)%20token_validation.rules%20%3E%20(model)%20token_validation_rule%20%3E%20(schema)%20%3E%20(property)%20selector">Link to this property</a>

Title string

A human-readable name for the rule.

maxLength50

<a href="#(resource)%20token_validation.rules%20%3E%20(model)%20token_validation_rule%20%3E%20(schema)%20%3E%20(property)%20title">Link to this property</a>

ID stringOptional

UUID.

maxLength36

minLength36

<a href="#(resource)%20token_validation.rules%20%3E%20(model)%20token_validation_rule%20%3E%20(schema)%20%3E%20(property)%20id">Link to this property</a>

CreatedAt TimeOptional

formatdate-time

<a href="#(resource)%20token_validation.rules%20%3E%20(model)%20token_validation_rule%20%3E%20(schema)%20%3E%20(property)%20created_at">Link to this property</a>

LastUpdated TimeOptional

formatdate-time

<a href="#(resource)%20token_validation.rules%20%3E%20(model)%20token_validation_rule%20%3E%20(schema)%20%3E%20(property)%20last_updated">Link to this property</a>

</details>

[Link to this property](<#(resource)%20token_validation.rules%20%3E%20(model)%20token_validation_rule%20%3E%20(schema)>)

### Create a token validation rule

Go

HTTPTypeScriptPythonGoTerraform

```
package main

import (
  "context"
  "fmt"

  "github.com/cloudflare/cloudflare-go"
  "github.com/cloudflare/cloudflare-go/option"
  "github.com/cloudflare/cloudflare-go/token_validation"
)

func main() {
  client := cloudflare.NewClient(
    option.WithAPIToken("Sn3lZJTBX6kkg7OdcBUAxOO963GEIyGQqnFTOFYY"),
  )
  tokenValidationRule, err := client.TokenValidation.Rules.New(context.TODO(), token_validation.RuleNewParams{
    ZoneID: cloudflare.F("023e105f4ecef8ad9ca31a8372d0c353"),
    Action: cloudflare.F(token_validation.RuleNewParamsActionLog),
    Description: cloudflare.F("Long description for Token Validation Rule"),
    Enabled: cloudflare.F(true),
    Expression: cloudflare.F(`is_jwt_valid("52973293-cb04-4a97-8f55-e7d2ad1107dd") or is_jwt_valid("46eab8d1-6376-45e3-968f-2c649d77d423")`),
    Selector: cloudflare.F(token_validation.RuleNewParamsSelector{

    }),
    Title: cloudflare.F("Example Token Validation Rule"),
  })
  if err != nil {
    panic(err.Error())
  }
  fmt.Printf("%+v\n", tokenValidationRule.ID)
}
```

200 example

```
{
  "errors": [
    {
      "code": 1000,
      "message": "message",
      "documentation_url": "documentation_url",
      "source": {
        "pointer": "pointer"
      }
    }
  ],
  "messages": [
    {
      "code": 1000,
      "message": "message",
      "documentation_url": "documentation_url",
      "source": {
        "pointer": "pointer"
      }
    }
  ],
  "result": {
    "action": "log",
    "description": "Long description for Token Validation Rule",
    "enabled": true,
    "expression": "is_jwt_valid(\"52973293-cb04-4a97-8f55-e7d2ad1107dd\") or is_jwt_valid(\"46eab8d1-6376-45e3-968f-2c649d77d423\")",
    "selector": {
      "exclude": [
        {
          "operation_ids": [
            "f9c5615e-fe15-48ce-bec6-cfc1946f1bec",
            "56828eae-035a-4396-ba07-51c66d680a04"
          ]
        }
      ],
      "include": [
        {
          "host": [
            "v1.example.com",
            "v2.example.com"
          ]
        }
      ]
    },
    "title": "Example Token Validation Rule",
    "id": "f174e90a-fafe-4643-bbbc-4a0ed4fc8415",
    "created_at": "2014-01-01T05:20:00.12345Z",
    "last_updated": "2014-01-01T05:20:00.12345Z"
  },
  "success": true
}
```

##### Returns Examples

200 example

```
{
  "errors": [
    {
      "code": 1000,
      "message": "message",
      "documentation_url": "documentation_url",
      "source": {
        "pointer": "pointer"
      }
    }
  ],
  "messages": [
    {
      "code": 1000,
      "message": "message",
      "documentation_url": "documentation_url",
      "source": {
        "pointer": "pointer"
      }
    }
  ],
  "result": {
    "action": "log",
    "description": "Long description for Token Validation Rule",
    "enabled": true,
    "expression": "is_jwt_valid(\"52973293-cb04-4a97-8f55-e7d2ad1107dd\") or is_jwt_valid(\"46eab8d1-6376-45e3-968f-2c649d77d423\")",
    "selector": {
      "exclude": [
        {
          "operation_ids": [
            "f9c5615e-fe15-48ce-bec6-cfc1946f1bec",
            "56828eae-035a-4396-ba07-51c66d680a04"
          ]
        }
      ],
      "include": [
        {
          "host": [
            "v1.example.com",
            "v2.example.com"
          ]
        }
      ]
    },
    "title": "Example Token Validation Rule",
    "id": "f174e90a-fafe-4643-bbbc-4a0ed4fc8415",
    "created_at": "2014-01-01T05:20:00.12345Z",
    "last_updated": "2014-01-01T05:20:00.12345Z"
  },
  "success": true
}
```