Create a response header transform rule via API
Last updated Aug 25, 2026 Use the Rulesets API to create Response Header Transform Rules via API. Refer to the Rules examples gallery for common use cases.
If you are using Terraform, refer to Transform Rules configuration using Terraform.
Basic rule settings
When creating a response header transform rule via API, make sure you:
- Set the rule action to
rewrite. - Define the header modification parameters in the
action_parametersfield according to the operation to perform (set, add, or remove header). - Deploy the rule to the
http_response_headers_transformphase at the zone level.
Procedure
Follow this workflow to create a response header transform rule for a given zone via API:
- Use the List zone rulesets operation to check if there is already a ruleset for the
http_response_headers_transformphase at the zone level. - 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:
- kind:
zone - phase:
http_response_headers_transform - Use the Update a zone ruleset operation to add a response header transform 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: Set an HTTP response header to a static value
</summary>
The following example configures the rules of an existing phase ruleset (<code>$RULESET_ID</code>) to a single response header transform rule — setting an HTTP response header to a static value — 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": "set_resp_header_source",
"expression": "(starts_with(http.request.uri.path, \"/en/\"))",
"description": "My first response header transform rule",
"action": "rewrite",
"action_parameters": {
"headers": {
"X-Source": {
"operation": "set",
"value": "Cloudflare"
}
}
}
}
]
}'
{
"result": {
"id": "<RULESET_ID>",
"name": "Zone-level Response Headers Transform Ruleset",
"description": "Zone-level ruleset that will execute Response Header Transform Rules.",
"kind": "zone",
"version": "2",
"rules": [
{
"ref": "set_resp_header_source",
"id": "<RULE_ID>",
"version": "1",
"action": "rewrite",
"action_parameters": {
"headers": {
"X-Source": {
"operation": "set",
"value": "Cloudflare"
}
}
},
"expression": "(starts_with(http.request.uri.path, \"/en/\"))",
"description": "My first response header transform rule",
"last_updated": "2021-04-14T14:42:04.219025Z",
"ref": "<RULE_REF>"
}
],
"last_updated": "2021-04-14T14:42:04.219025Z",
"phase": "http_response_headers_transform"
},
"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: Set an HTTP response header to a dynamic value
</summary>
The following example configures the rules of an existing phase ruleset (<code>$RULESET_ID</code>) to a single response header transform rule — setting an HTTP response header to a dynamic value — 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": "set_resp_header_bot_score",
"expression": "(starts_with(http.request.uri.path, \"/en/\"))",
"description": "My first response header transform rule",
"action": "rewrite",
"action_parameters": {
"headers": {
"X-Bot-Score": {
"operation": "set",
"expression": "to_string(cf.bot_management.score)"
}
}
}
}
]
}'
{
"result": {
"id": "<RULESET_ID>",
"name": "Zone-level Response Headers Transform Ruleset",
"description": "Zone-level ruleset that will execute Response Header Transform Rules.",
"kind": "zone",
"version": "2",
"rules": [
{
"ref": "set_resp_header_bot_score",
"id": "<RULE_ID>",
"version": "1",
"action": "rewrite",
"action_parameters": {
"headers": {
"X-Bot-Score": {
"operation": "set",
"expression": "to_string(cf.bot_management.score)"
}
}
},
"expression": "(starts_with(http.request.uri.path, \"/en/\"))",
"description": "My first response header transform rule",
"last_updated": "2021-04-14T14:42:04.219025Z",
"ref": "<RULE_REF>"
}
],
"last_updated": "2021-04-14T14:42:04.219025Z",
"phase": "http_response_headers_transform"
},
"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 <code>set-cookie</code> HTTP response header with a static value
</summary>
The following example configures the rules of an existing phase ruleset (<code>$RULESET_ID</code>) to a single response header transform rule — adding a <code>set-cookie</code> HTTP response header with a static value — using the <a href="https://developers.cloudflare.com/api/resources/rulesets/methods/update/">Update a zone ruleset</a> operation. By configuring the rule with the <code>add</code> operation you will keep any existing <code>set-cookie</code> headers that may already exist in the response. 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": "add_resp_header_set_mycookie",
"expression": "(starts_with(http.request.uri.path, \"/en/\"))",
"description": "My first response header transform rule",
"action": "rewrite",
"action_parameters": {
"headers": {
"set-cookie": {
"operation": "add",
"value": "mycookie=custom_value"
}
}
}
}
]
}'
{
"result": {
"id": "<RULESET_ID>",
"name": "Zone-level Response Headers Transform Ruleset",
"description": "Zone-level ruleset that will execute Response Header Transform Rules.",
"kind": "zone",
"version": "2",
"rules": [
{
"ref": "add_resp_header_set_mycookie",
"id": "<RULE_ID>",
"version": "1",
"action": "rewrite",
"action_parameters": {
"headers": {
"set-cookie": {
"operation": "add",
"value": "mycookie=custom_value"
}
}
},
"expression": "(starts_with(http.request.uri.path, \"/en/\"))",
"description": "My first response header transform rule",
"last_updated": "2021-04-14T14:42:04.219025Z",
"ref": "<RULE_REF>"
}
],
"last_updated": "2021-04-14T14:42:04.219025Z",
"phase": "http_response_headers_transform"
},
"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: Remove an HTTP response header
</summary>
The following example sets the rules of an existing phase ruleset (<code>$RULESET_ID</code>) to a single response header transform rule — removing an HTTP response header — 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": "remove_resp_header_cf_connecting_ip",
"expression": "(starts_with(http.request.uri.path, \"/en/\"))",
"description": "My first response header transform rule",
"action": "rewrite",
"action_parameters": {
"headers": {
"cf-connecting-ip": {
"operation": "remove"
}
}
}
}
]
}'
{
"result": {
"id": "<RULESET_ID>",
"name": "Zone-level Response Headers Transform Ruleset",
"description": "Zone-level ruleset that will execute Response Header Transform Rules.",
"kind": "zone",
"version": "2",
"rules": [
{
"ref": "remove_resp_header_cf_connecting_ip",
"id": "<RULE_ID>",
"version": "1",
"action": "rewrite",
"action_parameters": {
"headers": {
"cf-connecting-ip": {
"operation": "remove"
}
}
},
"expression": "(starts_with(http.request.uri.path, \"/en/\"))",
"description": "My first response header transform rule",
"last_updated": "2021-04-14T14:42:04.219025Z",
"ref": "<RULE_REF>"
}
],
"last_updated": "2021-04-14T14:42:04.219025Z",
"phase": "http_response_headers_transform"
},
"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>
Required API token permissions
The API token used in API requests to manage Response Header Transform Rules must have at least the following permissions:
- Transform Rules > Edit
- Account Rulesets > Read
Was this helpful?
YesNo
On this page
{"@context":"https://schema.org","@type":"TechArticle","@id":"https://developers.cloudflare.com/rules/transform/response-header-modification/create-api/#page","headline":"Create a response header transform rule via API","description":"Create response header modification rules using the API.","url":"https://developers.cloudflare.com/rules/transform/response-header-modification/create-api/","inLanguage":"en","image":"https://developers.cloudflare.com/rules/transform/response-header-modification/create-api/og.png?v=49f2cab51be61ef5","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/"},"keywords":["Headers","Response modification"]}
description: Create response header modification rules in the dashboard. title: Create a response header transform rule in the dashboard image: https://developers.cloudflare.com/rules/transform/response-header-modification/create-dashboard/og.png?v=18cfb7ff688f733e
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.
Docs