|
| 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 set all the Cache Response Rules of a zone to the rules included in the request. 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: Set max-age and remove stale-if-error"> |
| 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 | + "stale-if-error": { |
| 123 | + operation: "remove" |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + ] |
| 128 | + }} |
| 129 | + roles={false} |
| 130 | +/> |
| 131 | + |
| 132 | +</Details> |
| 133 | + |
| 134 | +<Details header="Example: Set private directive with qualifiers"> |
| 135 | + |
| 136 | +<APIRequest |
| 137 | + path="/zones/{zone_id}/rulesets/phases/http_response_cache_settings/entrypoint" |
| 138 | + method="PUT" |
| 139 | + json={{ |
| 140 | + rules: [ |
| 141 | + { |
| 142 | + expression: 'http.request.uri.path starts_with "/user/"', |
| 143 | + description: "Mark user content as private", |
| 144 | + action: "set_cache_control", |
| 145 | + action_parameters: { |
| 146 | + "private": { |
| 147 | + operation: "set", |
| 148 | + qualifiers: ["X-User-Id", "X-Session-Token"] |
| 149 | + }, |
| 150 | + "no-cache": { |
| 151 | + operation: "set" |
| 152 | + } |
| 153 | + } |
| 154 | + } |
| 155 | + ] |
| 156 | + }} |
| 157 | + roles={false} |
| 158 | +/> |
| 159 | + |
| 160 | +</Details> |
| 161 | + |
| 162 | +<Details header="Example: Set immutable for static font assets"> |
| 163 | + |
| 164 | +<APIRequest |
| 165 | + path="/zones/{zone_id}/rulesets/phases/http_response_cache_settings/entrypoint" |
| 166 | + method="PUT" |
| 167 | + json={{ |
| 168 | + rules: [ |
| 169 | + { |
| 170 | + expression: 'http.request.uri.path.extension in {"woff2" "woff" "ttf"}', |
| 171 | + description: "Mark fonts as immutable", |
| 172 | + action: "set_cache_control", |
| 173 | + action_parameters: { |
| 174 | + "immutable": { |
| 175 | + operation: "set" |
| 176 | + }, |
| 177 | + "max-age": { |
| 178 | + operation: "set", |
| 179 | + value: 31536000 |
| 180 | + } |
| 181 | + } |
| 182 | + } |
| 183 | + ] |
| 184 | + }} |
| 185 | + roles={false} |
| 186 | +/> |
| 187 | + |
| 188 | +</Details> |
| 189 | + |
| 190 | +<Details header="Example: Multiple rules — strip headers, tag responses, and set cache control"> |
| 191 | + |
| 192 | +<APIRequest |
| 193 | + path="/zones/{zone_id}/rulesets/phases/http_response_cache_settings/entrypoint" |
| 194 | + method="PUT" |
| 195 | + json={{ |
| 196 | + rules: [ |
| 197 | + { |
| 198 | + expression: 'http.response.code eq 200 and http.request.uri.path.extension eq "html"', |
| 199 | + description: "Strip tracking headers from HTML responses", |
| 200 | + action: "set_cache_settings", |
| 201 | + action_parameters: { |
| 202 | + strip_etags: true, |
| 203 | + strip_set_cookie: true |
| 204 | + } |
| 205 | + }, |
| 206 | + { |
| 207 | + expression: 'http.request.uri.path starts_with "/products/"', |
| 208 | + description: "Tag product pages for purging", |
| 209 | + action: "set_cache_tags", |
| 210 | + action_parameters: { |
| 211 | + operation: "add", |
| 212 | + values: ["product-catalog", "storefront"] |
| 213 | + } |
| 214 | + }, |
| 215 | + { |
| 216 | + expression: "http.response.code eq 200", |
| 217 | + description: "Set cache control for all 200 responses", |
| 218 | + action: "set_cache_control", |
| 219 | + action_parameters: { |
| 220 | + "s-maxage": { |
| 221 | + operation: "set", |
| 222 | + value: 86400, |
| 223 | + cloudflare_only: true |
| 224 | + }, |
| 225 | + "must-revalidate": { |
| 226 | + operation: "set" |
| 227 | + } |
| 228 | + } |
| 229 | + } |
| 230 | + ] |
| 231 | + }} |
| 232 | + roles={false} |
| 233 | +/> |
| 234 | + |
| 235 | +</Details> |
| 236 | + |
| 237 | +## Required API token permissions |
| 238 | + |
| 239 | +The API token used in API requests to manage Cache Response Rules must have the following permissions: |
| 240 | + |
| 241 | +- *Zone* > *Cache Rules* > *Edit* |
| 242 | +- *Account Rulesets* > *Edit* |
| 243 | +- *Account Filter Lists* > *Edit* |
0 commit comments