|
| 1 | +--- |
| 2 | +title: Create a rule via API |
| 3 | +pcx_content_type: how-to |
| 4 | +sidebar: |
| 5 | + order: 4 |
| 6 | +head: |
| 7 | + - tag: title |
| 8 | + content: Create a Cache Response Rule via API |
| 9 | +--- |
| 10 | + |
| 11 | +import { Details, APIRequest } from "~/components"; |
| 12 | + |
| 13 | +Use the [Rulesets API](/ruleset-engine/rulesets-api/) to create a Cache Response Rule via API. To configure the Cloudflare API, refer to the [API documentation](/fundamentals/api/get-started/). |
| 14 | + |
| 15 | +## Basic rule settings |
| 16 | + |
| 17 | +When creating a Cache Response Rule via API, make sure you: |
| 18 | + |
| 19 | +- Set the rule action to one of the [available actions](/cache/how-to/cache-response-rules/settings/#available-actions). |
| 20 | +- Define the parameters in the `action_parameters` field according to the [settings](/cache/how-to/cache-response-rules/settings/) you wish to configure for matching responses. |
| 21 | +- Deploy the rule to the `http_response_cache_settings` phase entry point ruleset. |
| 22 | + |
| 23 | +## Procedure |
| 24 | + |
| 25 | +1. Use the [List zone rulesets](/api/resources/rulesets/methods/list/) method to check if a ruleset already exists for the `http_response_cache_settings` phase. |
| 26 | +2. If the phase ruleset does not exist, create it using the [Create a zone ruleset](/api/resources/rulesets/methods/create/) operation. In the new ruleset properties, set the following values: |
| 27 | + - kind: `zone` |
| 28 | + - phase: `http_response_cache_settings` |
| 29 | +3. Use the [Update a zone ruleset](/api/resources/rulesets/methods/update/) operation to add rules to the ruleset. Alternatively, include the rules in the [Create a zone ruleset](/api/resources/rulesets/methods/create/) request mentioned in the previous step. |
| 30 | + |
| 31 | +## Example requests |
| 32 | + |
| 33 | +These examples demonstrate all the available actions in Cache Response Rules using request and response matching criteria. Using these examples directly will cause any existing rules in the phase to be replaced. |
| 34 | + |
| 35 | +<Details header="Example: Strip response headers from JS files before caching"> |
| 36 | + |
| 37 | +<APIRequest |
| 38 | + path="/zones/{zone_id}/rulesets/phases/http_response_cache_settings/entrypoint" |
| 39 | + method="PUT" |
| 40 | + json={{ |
| 41 | + rules: [ |
| 42 | + { |
| 43 | + expression: 'http.request.uri.path.extension eq "js"', |
| 44 | + description: "Strip caching headers from JS files", |
| 45 | + action: "set_cache_settings", |
| 46 | + action_parameters: { |
| 47 | + strip_etags: true, |
| 48 | + strip_set_cookie: true, |
| 49 | + strip_last_modified: true |
| 50 | + } |
| 51 | + } |
| 52 | + ] |
| 53 | + }} |
| 54 | + roles={false} |
| 55 | +/> |
| 56 | + |
| 57 | +</Details> |
| 58 | + |
| 59 | +<Details header="Example: Set static cache tags on API responses"> |
| 60 | + |
| 61 | +<APIRequest |
| 62 | + path="/zones/{zone_id}/rulesets/phases/http_response_cache_settings/entrypoint" |
| 63 | + method="PUT" |
| 64 | + json={{ |
| 65 | + rules: [ |
| 66 | + { |
| 67 | + expression: 'http.request.uri.path starts_with "/api/"', |
| 68 | + description: "Tag API responses for targeted purging", |
| 69 | + action: "set_cache_tags", |
| 70 | + action_parameters: { |
| 71 | + operation: "set", |
| 72 | + values: ["api-response", "dynamic-content"] |
| 73 | + } |
| 74 | + } |
| 75 | + ] |
| 76 | + }} |
| 77 | + roles={false} |
| 78 | +/> |
| 79 | + |
| 80 | +</Details> |
| 81 | + |
| 82 | +<Details header="Example: Add cache tags from a response header using an expression"> |
| 83 | + |
| 84 | +<APIRequest |
| 85 | + path="/zones/{zone_id}/rulesets/phases/http_response_cache_settings/entrypoint" |
| 86 | + method="PUT" |
| 87 | + json={{ |
| 88 | + rules: [ |
| 89 | + { |
| 90 | + expression: 'any(http.response.headers.names[*] == "Surrogate-Keys")', |
| 91 | + description: "Extract cache tags from alternative CDN response header", |
| 92 | + action: "set_cache_tags", |
| 93 | + action_parameters: { |
| 94 | + operation: "add", |
| 95 | + expression: 'split(http.response.headers["Surrogate-Keys"][0], ",", 1)' |
| 96 | + } |
| 97 | + } |
| 98 | + ] |
| 99 | + }} |
| 100 | + roles={false} |
| 101 | +/> |
| 102 | + |
| 103 | +</Details> |
| 104 | + |
| 105 | +<Details header="Example: Override cache-control with max-age "> |
| 106 | + |
| 107 | +<APIRequest |
| 108 | + path="/zones/{zone_id}/rulesets/phases/http_response_cache_settings/entrypoint" |
| 109 | + method="PUT" |
| 110 | + json={{ |
| 111 | + rules: [ |
| 112 | + { |
| 113 | + expression: "http.response.code eq 200", |
| 114 | + description: "Override cache-control for successful responses", |
| 115 | + action: "set_cache_control", |
| 116 | + action_parameters: { |
| 117 | + "max-age": { |
| 118 | + operation: "set", |
| 119 | + value: 3600, |
| 120 | + cloudflare_only: true |
| 121 | + }, |
| 122 | + } |
| 123 | + } |
| 124 | + ] |
| 125 | + }} |
| 126 | + roles={false} |
| 127 | +/> |
| 128 | + |
| 129 | +</Details> |
| 130 | + |
| 131 | +<Details header="Example: Set private directive with qualifiers"> |
| 132 | + |
| 133 | +<APIRequest |
| 134 | + path="/zones/{zone_id}/rulesets/phases/http_response_cache_settings/entrypoint" |
| 135 | + method="PUT" |
| 136 | + json={{ |
| 137 | + rules: [ |
| 138 | + { |
| 139 | + expression: 'http.request.uri.path starts_with "/user/"', |
| 140 | + description: "Mark user content as private", |
| 141 | + action: "set_cache_control", |
| 142 | + action_parameters: { |
| 143 | + "private": { |
| 144 | + operation: "set", |
| 145 | + qualifiers: ["X-User-Id", "X-Session-Token"] |
| 146 | + }, |
| 147 | + "no-cache": { |
| 148 | + operation: "set" |
| 149 | + } |
| 150 | + } |
| 151 | + } |
| 152 | + ] |
| 153 | + }} |
| 154 | + roles={false} |
| 155 | +/> |
| 156 | + |
| 157 | +</Details> |
| 158 | + |
| 159 | +<Details header="Example: Set immutable for static font assets"> |
| 160 | + |
| 161 | +<APIRequest |
| 162 | + path="/zones/{zone_id}/rulesets/phases/http_response_cache_settings/entrypoint" |
| 163 | + method="PUT" |
| 164 | + json={{ |
| 165 | + rules: [ |
| 166 | + { |
| 167 | + expression: 'http.request.uri.path.extension in {"woff2" "woff" "ttf"}', |
| 168 | + description: "Mark fonts as immutable", |
| 169 | + action: "set_cache_control", |
| 170 | + action_parameters: { |
| 171 | + "immutable": { |
| 172 | + operation: "set" |
| 173 | + }, |
| 174 | + "max-age": { |
| 175 | + operation: "set", |
| 176 | + value: 31536000 |
| 177 | + } |
| 178 | + } |
| 179 | + } |
| 180 | + ] |
| 181 | + }} |
| 182 | + roles={false} |
| 183 | +/> |
| 184 | + |
| 185 | +</Details> |
| 186 | + |
| 187 | +<Details header="Example: Multiple rules — strip headers, tag responses, and set cache control"> |
| 188 | + |
| 189 | +<APIRequest |
| 190 | + path="/zones/{zone_id}/rulesets/phases/http_response_cache_settings/entrypoint" |
| 191 | + method="PUT" |
| 192 | + json={{ |
| 193 | + rules: [ |
| 194 | + { |
| 195 | + expression: 'http.response.code eq 200 and http.request.uri.path.extension eq "html"', |
| 196 | + description: "Strip tracking headers from HTML responses", |
| 197 | + action: "set_cache_settings", |
| 198 | + action_parameters: { |
| 199 | + strip_etags: true, |
| 200 | + strip_set_cookie: true |
| 201 | + } |
| 202 | + }, |
| 203 | + { |
| 204 | + expression: 'http.request.uri.path starts_with "/products/"', |
| 205 | + description: "Tag product pages for purging", |
| 206 | + action: "set_cache_tags", |
| 207 | + action_parameters: { |
| 208 | + operation: "add", |
| 209 | + values: ["product-catalog", "storefront"] |
| 210 | + } |
| 211 | + }, |
| 212 | + { |
| 213 | + expression: "http.response.code eq 200", |
| 214 | + description: "Set cache control for all 200 responses", |
| 215 | + action: "set_cache_control", |
| 216 | + action_parameters: { |
| 217 | + "s-maxage": { |
| 218 | + operation: "set", |
| 219 | + value: 86400, |
| 220 | + cloudflare_only: true |
| 221 | + }, |
| 222 | + "must-revalidate": { |
| 223 | + operation: "set" |
| 224 | + } |
| 225 | + } |
| 226 | + } |
| 227 | + ] |
| 228 | + }} |
| 229 | + roles={false} |
| 230 | +/> |
| 231 | + |
| 232 | +</Details> |
| 233 | + |
| 234 | +## Required API token permissions |
| 235 | + |
| 236 | +The API token used in API requests to manage Cache Response Rules must have the following permissions: |
| 237 | + |
| 238 | +- *Zone* > *Cache Rules* > *Edit* |
| 239 | +- *Account Rulesets* > *Edit* |
| 240 | +- *Account Filter Lists* > *Edit* |
0 commit comments