Configure JWT validation via the API
Last updated Oct 6, 2026 Use the Cloudflare API to configure JWT validation. A token configuration defines how Cloudflare finds and validates JWTs. You then use a WAF custom rule or a token validation rule to act on the results.
Token configurations
A token configuration defines the JSON Web Key Set (JWKS) used to validate JSON Web Tokens (JWTs). It also defines where Cloudflare finds JWTs in requests.
Note
Each zone supports up to 32 token configurations by default, with up to 16 keys per configuration. Contact your account team if you need a different allocation.
Token configurations require the following information:
| Field name | Description | Example | Notes |
|---|---|---|---|
title |
A human-readable name for the configuration that allows you to quickly identify the purpose of the configuration. | Production JWT configuration | Limited to 50 characters. |
description |
A human-readable description that gives more details than title which serves as a means to allow customers to better document the use of the configuration. |
This configuration checks the JWT in the authorization header. | Limited to 500 characters. |
token_sources |
A list of possible locations where then JWT can be found on the request. | http.request.headers[\"authorization\"][0] <br> http.request.cookies[\"Authorization\"][0] |
Refer to the information below. |
token_type |
This specifies the type of token to validate. | jwt |
Only jwt is currently supported. |
credentials |
This describes the cryptographic keys that should be used to validate JWTs. Each key must be a JSON Web Key (JWK). | Refer to the example below. | Refer to the information below. |
Token sources
Each item must be a Ruleset Engine expression that resolves to a string.
Currently supported fields are http.request.headers and http.request.cookies.
You can set up to four token sources. The sources use OR logic: a valid token from any one source is sufficient. This supports clients using different token locations, including during a migration. If a request contains values in multiple configured sources, Cloudflare evaluates them one at a time and stops after the first valid token. Cloudflare does not combine token values or require multiple sources to be valid.
A source can contain an unprefixed JWT. It can also use the exact Bearer prefix, with a capital B, no colon, and one space after Bearer.
Refer to the Ruleset Engine documentation for details on working with Ruleset Engine fields.
Credentials
API Shield supports asymmetric RSA and elliptic curve keys and symmetric hash-based message authentication code (HMAC) keys:
| Key type | Supported algorithms | Requirements |
|---|---|---|
| RSA | RS256, RS384, RS512, PS256, PS384, and PS512 |
RSA keys must be 2,048, 3,072, or 4,096 bits. |
| EC | ES256 and ES384 |
Use curve P-256 with ES256 and curve P-384 with ES384. |
| HMAC | HS256, HS384, and HS512 |
Use a symmetric secret of at least 32, 48, or 64 bytes, respectively. |
Provide an alg value for every JWK. Each JWK must also have a kid. The effective algorithm and key ID must match the alg and kid values in the JWT header.
For compatibility with identity providers that omit alg, API Shield defaults RSA keys to RS256, P-256 EC keys to ES256, and P-384 EC keys to ES384. Because an RSA key does not identify its signing algorithm, specify alg explicitly if the identity provider uses another supported RSA algorithm. HMAC keys must always specify alg.
For an HMAC key, set kty to oct. Set k to the raw symmetric credential encoded with unpadded Base64url. The decoded credential must meet the minimum length for its algorithm.
Caution
For new JWT deployments, Cloudflare recommends asymmetric, public-key algorithms such as RSA or elliptic curve algorithms. API Shield then needs only the public verification key, while the issuer retains the private signing key.
HMAC uses a shared credential. Anyone with that credential can sign and validate JWTs. Treat it as a secret and do not expose it in source code or logs. Cloudflare never stores symmetric credentials in plaintext. API responses do not include the credential.
Cloudflare will remove any fields that are unnecessary from each key and will drop keys that we do not support.
It is highly recommended to validate the output of the API call to check that the resulting keys appear as intended.
Token configuration JSON object
The example below shows a JSON object with all of the information necessary to create a token configuration using the Cloudflare API. If you would like to create JWKs for testing, refer to mkjwk JSON Web Key Generator ↗︎.
Examplejson
{
"title": "Production JWT configuration",
"description": "This configuration checks the JWT in the authorization header or cookie.",
"token_sources": [
"http.request.headers[\"authorization\"][0]",
"http.request.cookies[\"Authorization\"][0]"
],
"token_type": "jwt",
"credentials": {
"keys": [
{
"kty": "EC",
"use": "sig",
"crv": "P-256",
"kid": "93UrzmNu1mqXs5cZcvCPkTlMHB2Jya30vSTkiBb0vhU",
"x": "QG3VFVwUX4IatQvBy7sqBvvmticCZ-eX5-nbtGKBOfI",
"y": "A3PXCshn7XcG7Ivvd2K_DerW4LHAlIVKdqhrUnczTD0",
"alg": "ES256"
}
]
}
}
Symmetric key example
The following example configures JWT validation with an HS256 symmetric key. Replace <BASE64URL_ENCODED_SECRET> with an unpadded Base64url-encoded credential containing at least 32 decoded bytes.
HS256 token configurationjson
{
"title": "Production HMAC JWT configuration",
"description": "This configuration checks the JWT in the authorization header.",
"token_sources": ["http.request.headers[\"authorization\"][0]"],
"token_type": "jwt",
"credentials": {
"keys": [
{
"kty": "oct",
"alg": "HS256",
"kid": "production-hmac-key",
"k": "<BASE64URL_ENCODED_SECRET>"
}
]
}
}
The response includes kty, alg, and kid, but does not include k:
Symmetric key responsejson
{
"credentials": {
"keys": [
{
"kty": "oct",
"alg": "HS256",
"kid": "production-hmac-key"
}
]
}
}
Create a token configuration using the Cloudflare API
Use cURL or any other API client tool to send the new configuration to Cloudflare’s API to enable JWT validation. Make sure to replace {zone_id} with the relevant zone ID and add your authentication credentials header.
Example using cURLbash
curl "https://api.cloudflare.com/client/v4/zones/{zone_id}/token_validation/config" \
--header 'Content-Type: application/json' \
--data '{
"title": "Production JWT configuration",
"description": "This configuration checks the JWT in the authorization header or cookie.",
"token_sources": [
"http.request.headers[\"authorization\"][0]",
"http.request.cookies[\"Authorization\"][0]"
],
"token_type": "jwt",
"credentials": {
"keys": [
{
"kty": "EC",
"use": "sig",
"crv": "P-256",
"kid": "93UrzmNu1mqXs5cZcvCPkTlMHB2Jya30vSTkiBb0vhU",
"x": "QG3VFVwUX4IatQvBy7sqBvvmticCZ-eX5-nbtGKBOfI",
"y": "A3PXCshn7XcG7Ivvd2K_DerW4LHAlIVKdqhrUnczTD0",
"alg": "ES256"
}
]
}
}'
The response will be in a Cloudflare v4 response envelope and the result contains the created configuration. Note the returned ID. You can use it to reference the token configuration in JWT claim fields or token validation rules.
Example responsejson
{
"result": {
"id": "d5902294-00c3-4aed-b517-57e752e9cd58",
"token_type": "JWT",
"title": "Production JWT configuration",
"description": "This configuration checks the JWT in the authorization header or cookie.",
"token_sources": [
"http.request.headers[\"authorization\"][0]",
"http.request.cookies[\"Authorization\"][0]"
],
"credentials": {
"keys": [
{
"x": "QG3VFVwUX4IatQvBy7sqBvvmticCZ-eX5-nbtGKBOfI",
"y": "A3PXCshn7XcG7Ivvd2K_DerW4LHAlIVKdqhrUnczTD0",
"alg": "ES256",
"crv": "P-256",
"kid": "93UrzmNu1mqXs5cZcvCPkTlMHB2Jya30vSTkiBb0vhU",
"kty": "EC"
}
]
},
"created_at": "2023-11-08T16:45:17.236841Z",
"last_updated": "2023-11-08T16:45:17.236841Z"
},
"success": true,
"errors": [],
"messages": []
}
If API Shield defaults an omitted algorithm, the response includes the effective algorithm in result. The messages array also contains one message for each defaulted key:
Example message for a defaulted algorithmjson
{
"code": 110003,
"message": "keys[0].alg was omitted and defaulted to \"RS256\" (kid \"key-1\")"
}
Inspect both result and messages to confirm the effective credentials.
Act on validation results
After you create a token configuration, Cloudflare checks every request in the zone for a JWT at the configured token sources and validates any token it finds. You do not need a token validation rule or an operation in Endpoint Management for validation. Rules determine how Cloudflare acts on the results.
Note
For new security policies, Cloudflare recommends using WAF custom rules to act on JWT validation results. Existing API JWT validation rules remain supported.
WAF custom rules support zone-wide policies and policies scoped to selected endpoints. They can also combine JWT validation results and verified claims with other security signals, such as attack score. For equivalent WAF custom rule expressions, refer to WAF custom rule examples.
To reference a custom claim in a rule expression, you can use lookup_json_* functions like lookup_json_string() with your token configuration ID and the claim name. For a complete claim example, refer to Issue challenge for admin user in JWT claim based on attack score. For all available fields and standard claims, refer to the JWT validation fields reference.
Token validation rules
Token validation rules enforce a security policy using existing token configurations and operations in Endpoint Management.
Token validation rules can be configured using the Cloudflare API or dashboard.
| Field name | Description | Example | Notes |
|---|---|---|---|
title |
A human-readable name allowing you to quickly identify it. | JWT validation on v1 and v2.example.com |
Limited to 50 characters. |
description |
A human-readable description that gives more details than title and helps to document it. |
Log requests without a valid authorization header. |
Limited to 500 characters. |
action |
The Firewall Action taken on requests that do not meet expression. |
log |
Possible: log or block |
enabled |
Enable or disable the rule. | true |
Possible: true or false |
expression |
The rule's security policy. | is_jwt_valid ("00170473-ec24-410e-968a-9905cf0a7d03") |
Make sure to escape any quotes when creating rules using the Cloudflare API. <br> Refer to Define a security policy below. |
selector |
Configure what operations are covered by this rule. | Refer to Applying a rule to operations below. |
Selectors
Selectors control to which operations from Endpoint Management Cloudflare applies the action of a token validation rule.
If you only need enforcement on specific hostnames or subdomains of your apex domain, use the hostname in a selector to include matching operations in the JWT validation rule.
If you need to exclude endpoints from enforcement, use the endpoint's operation ID in a selector. For example, you can exclude an endpoint that issues or refreshes JWTs.
To find the operation ID, refer to Endpoint Management or use the Cloudflare API.
Define a security policy
Note
A request must also match an operation covered by this rule to trigger an action.
Refer to Apply a rule to operations for more information.
A token validation rule's expression defines a security policy that a request must meet.
For example, the expression is_jwt_valid("51231d16-01f1-48e3-93f8-91c99e81288e") or is_jwt_valid("fddfc39e-3686-4683-ab23-bf917da6bb43") will trigger if an incoming request does not have at least one valid authentication token.
These expressions are similar to expressions used in Ruleset Engine, with a few key differences:
- The token validation rule actions trigger if the expression evaluates
false, as opposed to Ruleset expressions. - The token validation rules can use dedicated functions that reference token configurations.
Operators such as or, and, eq, and more are usable in expressions in the same way as in expressions used in Ruleset Engine.
The following functions can be used to interact with JWT Tokens on a request:
is_jwt_valid(token_configuration_id)— Returns true if the request has a valid token according to the token configuration with the IDtoken_configuration_id.is_jwt_present(token_configuration_id)— Returns true if the request has a token as configured in the token configuration with the IDtoken_configuration_id.
These functions are only available in token validation rules. They are not available in WAF custom rules.
Common use cases
Refer to the following example use cases to understand which security policy to use. For most use cases, Cloudflare recommends requiring a valid token across your API and excluding any paths that are used to establish or refresh tokens using selectors.
Require a token
The is_jwt_present("51231d16-01f1-48e3-93f8-91c99e81288e") expression will trigger an action if a request is missing a JWT.
It can be combined with a log action in the token validation rule to log requests that are missing an authentication header.
Require a valid token
The is_jwt_valid("51231d16-01f1-48e3-93f8-91c99e81288e") expression will trigger an action if a request does not have a valid JWT.
It can be combined with a block action in the token validation rule to block requests with no or invalid credentials.
Require at least one of two possible tokens
The is_jwt_valid("51231d16-01f1-48e3-93f8-91c99e81288e") or is_jwt_valid("fddfc39e-3686-4683-ab23-bf917da6bb43") expressions will trigger an action if a request does not have at least one valid token.
This can occur when your API accepts tokens from two different identity providers.
Require a valid token but ignore requests without a token
The is_jwt_valid("51231d16-01f1-48e3-93f8-91c99e81288e") or not is_jwt_present("51231d16-01f1-48e3-93f8-91c99e81288e") expressions will trigger an action if a request has an invalid token, ignoring requests with no tokens at all.
Apply a rule to operations
Only one token validation rule can apply to an operation. If an operation is covered by multiple rules, then the rule with highest precedence will take effect.
You can configure which operations JWT validation is enforced on using the selector field.
Note
Selectors will also apply to new operations. New operations that match an existing selector will automatically be covered by that token validation rule.
For example, the following selector will apply a rule to all operations in v1.example.com and v2.example.com, except for two operations on these hosts:
