Create an origin rule via API

Last updated Aug 25, 2026 Use the Rulesets API to create origin rules via API.

Basic rule settings

When creating an origin rule via API, make sure you:

  • Set the rule action to route.
  • Define the parameters in the action_parameters field according to the type of origin override.
  • Deploy the rule to the http_request_origin phase at the zone level.

Procedure

Follow this workflow to create an origin rule for a given zone via API:

  1. Use the List zone rulesets operation to check if there is already a ruleset for the http_request_origin phase at the zone level.
  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_origin
  5. Use the Update a zone ruleset operation to add an origin rule to the list of ruleset rules. Alternatively, include the rule in the Create a zone ruleset request mentioned in the previous step.

Make sure your API token has the required permissions to perform the API operations.

Example requests

<details>

<summary>

Example: Add a rule that overrides the <code>Host</code> header of incoming requests and the resolved DNS record

</summary>

The following example sets the rules of an existing phase ruleset (<code>$RULESET_ID</code>) to a single origin rule — overriding the <code>Host</code> header of incoming requests and the resolved DNS record — using the <a href="https://developers.cloudflare.com/api/resources/rulesets/methods/update/">Update a zone ruleset</a> operation. The response will contain the complete definition of the ruleset you updated.

<details>

<summary>

Required API token permissions

</summary>

At least one of the following <a href="https://developers.cloudflare.com/fundamentals/api/reference/permissions/">token permissions</a> is required:

  • <code>Response Compression Write</code>
  • <code>Config Settings Write</code>
  • <code>Dynamic URL Redirects Write</code>
  • <code>Cache Settings Write</code>
  • <code>Custom Errors Write</code>
  • <code>Origin Write</code>
  • <code>Managed headers Write</code>
  • <code>Zone Transform Rules Write</code>
  • <code>Mass URL Redirects Write</code>
  • <code>Magic Firewall Write</code>
  • <code>L4 DDoS Managed Ruleset Write</code>
  • <code>HTTP DDoS Managed Ruleset Write</code>
  • <code>Sanitize Write</code>
  • <code>Transform Rules Write</code>
  • <code>Select Configuration Write</code>
  • <code>Bot Management Write</code>
  • <code>Zone WAF Write</code>
  • <code>Account WAF Write</code>
  • <code>Account Rulesets Write</code>
  • <code>Logs Write</code>
  • <code>Logs Write</code>

</details>

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": [
                {
                        "ref": "hr_app_overrides",
                        "expression": "starts_with(http.request.uri.path, \"/hr-app/\")",
                        "description": "Origin rule for the company HR application",
                        "action": "route",
                        "action_parameters": {
                                "host_header": "hr-server.example.com",
                                "origin": {
                                        "host": "hr-server.example.com"
                                }
                        }
                }
        ]
    }'
{
    "result": {
        "id": "<RULESET_ID>",
        "name": "Origin Rules ruleset",
        "description": "Zone-level ruleset that will execute origin rules.",
        "kind": "zone",
        "version": "2",
        "rules": [
            {
                "ref": "hr_app_overrides",
                "id": "<RULE_ID>",
                "version": "1",
                "action": "route",
                "action_parameters": {
                    "host_header": "hr-server.example.com",
                    "origin": {
                        "host": "hr-server.example.com"
                    }
                },
                "expression": "starts_with(http.request.uri.path, \"/hr-app/\")",
                "description": "Origin rule for the company HR application",
                "last_updated": "2022-06-03T14:42:04.219025Z",
                "ref": "<RULE_REF>"
            }
        ],
        "last_updated": "2022-06-03T14:42:04.219025Z",
        "phase": "http_request_origin"
    },
    "success": true,
    "errors": [],
    "messages": []
}

Use the <code>ref</code> field to get stable rule IDs across updates when using Terraform. Adding this field prevents Terraform from recreating the rule on changes. For more information, refer to <a href="https://developers.cloudflare.com/terraform/troubleshooting/rule-id-changes/#how-to-keep-the-same-rule-id-between-modifications">Troubleshooting</a> in the Terraform documentation.

</details>

<details>

<summary>

Example: Add a rule that overrides the port of incoming requests

</summary>

The following example sets the rules of an existing phase ruleset (<code>$RULESET_ID</code>) to a single origin rule — overriding the port of incoming requests — using the <a href="https://developers.cloudflare.com/api/resources/rulesets/methods/update/">Update a zone ruleset</a> operation. The response will contain the complete definition of the ruleset you updated.

<details>

<summary>

Required API token permissions

</summary>

At least one of the following <a href="https://developers.cloudflare.com/fundamentals/api/reference/permissions/">token permissions</a> is required:

  • <code>Response Compression Write</code>
  • <code>Config Settings Write</code>
  • <code>Dynamic URL Redirects Write</code>
  • <code>Cache Settings Write</code>
  • <code>Custom Errors Write</code>
  • <code>Origin Write</code>
  • <code>Managed headers Write</code>
  • <code>Zone Transform Rules Write</code>
  • <code>Mass URL Redirects Write</code>
  • <code>Magic Firewall Write</code>
  • <code>L4 DDoS Managed Ruleset Write</code>
  • <code>HTTP DDoS Managed Ruleset Write</code>
  • <code>Sanitize Write</code>
  • <code>Transform Rules Write</code>
  • <code>Select Configuration Write</code>
  • <code>Bot Management Write</code>
  • <code>Zone WAF Write</code>
  • <code>Account WAF Write</code>
  • <code>Account Rulesets Write</code>
  • <code>Logs Write</code>
  • <code>Logs Write</code>

</details>

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": [
                {
                        "ref": "calendar_app_change_port",
                        "expression": "starts_with(http.request.uri.path, \"/team/calendar/\")",
                        "description": "Origin rule for the team calendar application",
                        "action": "route",
                        "action_parameters": {
                                "origin": {
                                        "port": 8081
                                }
                        }
                }
        ]
    }'
{
    "result": {
        "id": "<RULESET_ID>",
        "name": "Origin Rules ruleset",
        "description": "Zone-level ruleset that will execute origin rules.",
        "kind": "zone",
        "version": "2",
        "rules": [
            {
                "ref": "calendar_app_change_port",
                "id": "<RULE_ID>",
                "version": "1",
                "action": "route",
                "action_parameters": {
                    "origin": {
                        "port": 8081
                    }
                },
                "expression": "starts_with(http.request.uri.path, \"/team/calendar/\")",
                "description": "Origin rule for the team calendar application",
                "last_updated": "2022-06-03T14:42:04.219025Z",
                "ref": "<RULE_REF>"
            }
        ],
        "last_updated": "2022-06-03T14:42:04.219025Z",
        "phase": "http_request_origin"
    },
    "success": true,
    "errors": [],
    "messages": []
}

Use the <code>ref</code> field to get stable rule IDs across updates when using Terraform. Adding this field prevents Terraform from recreating the rule on changes. For more information, refer to <a href="https://developers.cloudflare.com/terraform/troubleshooting/rule-id-changes/#how-to-keep-the-same-rule-id-between-modifications">Troubleshooting</a> in the Terraform documentation.

</details>

<details>

<summary>

Example: Add a rule that overrides the SNI value of incoming requests

</summary>

The following example sets the rules of an existing phase ruleset (<code>$RULESET_ID</code>) to a single origin rule — overriding the SNI value of incoming requests addressed at <code>admin.example.com</code> — using the <a href="https://developers.cloudflare.com/api/resources/rulesets/methods/update/">Update a zone ruleset</a> operation.

<details>

<summary>

Required API token permissions

</summary>

At least one of the following <a href="https://developers.cloudflare.com/fundamentals/api/reference/permissions/">token permissions</a> is required:

  • <code>Response Compression Write</code>
  • <code>Config Settings Write</code>
  • <code>Dynamic URL Redirects Write</code>
  • <code>Cache Settings Write</code>
  • <code>Custom Errors Write</code>
  • <code>Origin Write</code>
  • <code>Managed headers Write</code>
  • <code>Zone Transform Rules Write</code>
  • <code>Mass URL Redirects Write</code>
  • <code>Magic Firewall Write</code>
  • <code>L4 DDoS Managed Ruleset Write</code>
  • <code>HTTP DDoS Managed Ruleset Write</code>
  • <code>Sanitize Write</code>
  • <code>Transform Rules Write</code>
  • <code>Select Configuration Write</code>
  • <code>Bot Management Write</code>
  • <code>Zone WAF Write</code>
  • <code>Account WAF Write</code>
  • <code>Account Rulesets Write</code>
  • <code>Logs Write</code>
  • <code>Logs Write</code>

</details>

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": [
                {
                        "ref": "override_sni_for_admin",
                        "expression": "http.host eq \"admin.example.com\"",
                        "description": "SNI Override for the admin area",
                        "action": "route",
                        "action_parameters": {
                                "sni": {
                                        "value": "sni.example.com"
                                }
                        }
                }
        ]
    }'

Use the <code>ref</code> field to get stable rule IDs across updates when using Terraform. Adding this field prevents Terraform from recreating the rule on changes. For more information, refer to <a href="https://developers.cloudflare.com/terraform/troubleshooting/rule-id-changes/#how-to-keep-the-same-rule-id-between-modifications">Troubleshooting</a> in the Terraform documentation.

</details>


Required API token permissions

The API token used in API requests to manage origin rules must have at least the following permission:

  • Zone > Origin Rules > Edit

Was this helpful?

YesNo

On this page

Create an origin rule via API - 图1Docs

{"@context":"https://schema.org","@type":"TechArticle","@id":"https://developers.cloudflare.com/rules/origin-rules/create-api/#page","headline":"Create an origin rule via API","description":"Create origin rules using the Rulesets API.","url":"https://developers.cloudflare.com/rules/origin-rules/create-api/","inLanguage":"en","image":"https://developers.cloudflare.com/rules/origin-rules/create-api/og.png?v=a67823ed4de098cc","dateModified":"2026-08-25","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 origin rules in the Cloudflare dashboard. title: Create an origin rule in the dashboard image: https://developers.cloudflare.com/rules/origin-rules/create-dashboard/og.png?v=9d89164c92d2dc20


Skip to content

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