Create a rule via API (2)

Last updated Sep 14, 2026 Use the Rulesets API to create a cache rule via API. To configure Cloudflare’s API refer to the API documentation.

Basic rule settings

When creating a cache rule via API, make sure you:

  • Set the rule action to set_cache_settings.
  • Define the parameters in the action_parameters field according to the settings you wish to override for matching requests.
  • Deploy the rule to the http_request_cache_settings phase entry point ruleset.

Procedure

  1. Use the List zone rulesets method to obtain the list of rules already present in the http_request_cache_settings phase entry point ruleset.
  2. If the phase ruleset does not exist, create it using the Create a zone ruleset operation. In the new ruleset properties, set the following values:
  3. kind: zone
  4. phase: http_request_cache_settings
  5. Use the Update a zone ruleset operation to add a cache rule to the list of ruleset rules. Alternatively, include the rule in the Create a zone ruleset request mentioned in the previous step.
  6. (Optional) To update an existing cache rule, use the Update a zone ruleset rule operation. For an example, refer to the section below.

Example requests

These examples are setting all the Cache Rules of a zone to a single rule, since using these examples directly will cause any existing rules to be deleted.

<details>

<summary>

Example: Cache everything for example.com

</summary>

Update a zone rulesetbash

curl "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/rulesets/$RULESET_ID" \
    --request PUT \
    --header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
    --json '{
        "rules": [
                {
                        "expression": "(http.host eq \"example.com\")",
                        "description": "cache everything for example.com",
                        "action": "set_cache_settings",
                        "action_parameters": {
                                "cache": true
                        }
                }
        ]
    }'

</details>

<details>

<summary>

Example: Extend read timeout for Android clients

</summary>

Update a zone rulesetbash

curl "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/rulesets/$RULESET_ID" \
    --request PUT \
    --header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
    --json '{
        "rules": [
                {
                        "expression": "(http.user_agent contains \"Android\")",
                        "description": "extend read timeout for android clients",
                        "action": "set_cache_settings",
                        "action_parameters": {
                                "cache": true,
                                "read_timeout": 300
                        }
                }
        ]
    }'

</details>

<details>

<summary>

Example: Disable Cache Reserve for frequently updated assets

</summary>

Update a zone rulesetbash

curl "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/rulesets/$RULESET_ID" \
    --request PUT \
    --header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
    --json '{
        "rules": [
                {
                        "expression": "(starts_with(http.request.uri, \"/feed/\"))",
                        "description": "disable cache reserve for frequently updated assets",
                        "action": "set_cache_settings",
                        "action_parameters": {
                                "cache": true,
                                "cache_reserve": {
                                        "enabled": false
                                }
                        }
                }
        ]
    }'

</details>

<details>

<summary>

Example: Turn on Origin Range Requests for large media files

</summary>

Update a zone entry point rulesetbash

curl "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/rulesets/phases/http_request_cache_settings/entrypoint" \
    --request PUT \
    --header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
    --json '{
        "rules": [
                {
                        "expression": "(http.request.uri.path.extension in {\"mp4\" \"mov\" \"m4v\"})",
                        "description": "turn on origin range requests for large media files",
                        "action": "set_cache_settings",
                        "action_parameters": {
                                "cache": true,
                                "origin_range_requests": {
                                        "mode": "on"
                                }
                        }
                }
        ]
    }'

</details>

<details>

<summary>

Example: Turn off default Origin Range Requests

</summary>

Update a zone entry point rulesetbash

curl "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/rulesets/phases/http_request_cache_settings/entrypoint" \
    --request PUT \
    --header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
    --json '{
        "rules": [
                {
                        "expression": "true",
                        "description": "turn off default Origin Range Requests",
                        "action": "set_cache_settings",
                        "action_parameters": {
                                "origin_range_requests": {
                                        "mode": "off"
                                }
                        }
                }
        ]
    }'

</details>

<details>

<summary>

Example: Turn off default cache TTLs

</summary>

Update a zone rulesetbash

curl "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/rulesets/$RULESET_ID" \
    --request PUT \
    --header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
    --json '{
        "rules": [
                {
                        "expression": "(http.host eq \"example.com\")",
                        "description": "turn off default cache ttls",
                        "action": "set_cache_settings",
                        "action_parameters": {
                                "cache": true,
                                "edge_ttl": {
                                        "mode": "bypass_by_default"
                                }
                        }
                }
        ]
    }'

</details>

<details>

<summary>

Example: Cache expected Vary responses

</summary>

Update a zone entry point rulesetbash

curl "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/rulesets/phases/http_request_cache_settings/entrypoint" \
    --request PUT \
    --header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
    --json '{
        "rules": [
                {
                        "expression": "(http.host eq \"example.com\")",
                        "description": "cache expected vary responses",
                        "action": "set_cache_settings",
                        "action_parameters": {
                                "cache": true,
                                "vary": {
                                        "default": {
                                                "action": "bypass"
                                        },
                                        "headers": {
                                                "accept": {
                                                        "action": "normalize",
                                                        "media_types": [
                                                                "text/html",
                                                                "application/json"
                                                        ]
                                                },
                                                "accept-language": {
                                                        "action": "normalize",
                                                        "languages": [
                                                                "en",
                                                                "fr",
                                                                "de"
                                                        ]
                                                }
                                        }
                                }
                        }
                }
        ]
    }'

</details>

<details>

<summary>

Example: Update the position of an existing rule

</summary>

Update a zone ruleset rulebash

curl "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/rulesets/$RULESET_ID/rules/$RULE_ID" \
    --request PATCH \
    --header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
    --json '{
        "expression": "(http.host eq \"example.com\")",
        "description": "cache everything for example.com",
        "action": "set_cache_settings",
        "action_parameters": {
                "cache": true
        },
        "enabled": true,
        "position": {
                "before": "da5e8e506c8e7877fe06cdf4c41add54"
        }
    }'

</details>

Required API token permissions

The API token used in API requests to manage Cache Rules must have the following permissions:

  • Zone > Cache Rules > Edit
  • Account Rulesets > Edit
  • Account Filter Lists > Edit

Was this helpful?

YesNo

On this page

Create a rule via API (2) - 图1Docs

{"@context":"https://schema.org","@type":"TechArticle","@id":"https://developers.cloudflare.com/cache/how-to/cache-rules/create-api/#page","headline":"Create a rule via API","description":"Create cache rules using the Rulesets API.","url":"https://developers.cloudflare.com/cache/how-to/cache-rules/create-api/","inLanguage":"en","image":"https://developers.cloudflare.com/cache/how-to/cache-rules/create-api/og.png?v=d3fa7ad28b3262fe","dateModified":"2026-09-14","publisher":{"@type":"Organization","name":"Cloudflare","description":"One platform for your apps, agents, and workforce. Build, secure, and scale without managing infrastructure","url":"https://www.cloudflare.com/","sameAs":["https://github.com/cloudflare","https://www.linkedin.com/company/cloudflare","https://x.com/cloudflare"],"logo":{"@type":"ImageObject","url":"https://developers.cloudflare.com/logo.svg"},"address":{"@type":"PostalAddress","streetAddress":"101 Townsend St","addressLocality":"San Francisco","addressRegion":"CA","postalCode":"94107","addressCountry":"US"},"contactPoint":[{"@type":"ContactPoint","contactType":"Customer Support","url":"https://support.cloudflare.com/","availableLanguage":["English"]},{"@type":"ContactPoint","contactType":"Sales","url":"https://www.cloudflare.com/contact/","availableLanguage":["English"]}]},"isPartOf":{"@type":"WebSite","@id":"https://developers.cloudflare.com/#website","name":"Cloudflare Docs","url":"https://developers.cloudflare.com/"}}


description: Create cache rules in the Cloudflare dashboard. title: Create a rule in the dashboard image: https://developers.cloudflare.com/cache/how-to/cache-rules/create-dashboard/og.png?v=46012c70eae91f05


Skip to content

Documentation Index
Fetch the complete documentation index at: https://developers.cloudflare.com/cache/llms.txt
Use this file to discover all available pages before exploring further.