|
| 1 | +--- |
| 2 | +title: Configure via API |
| 3 | +pcx_content_type: how-to |
| 4 | +sidebar: |
| 5 | + order: 3 |
| 6 | +head: |
| 7 | + - tag: title |
| 8 | + content: Configure Snippets via API |
| 9 | +--- |
| 10 | + |
| 11 | +You can create Snippets using the [Cloudflare API](/fundamentals/api/). |
| 12 | + |
| 13 | +## Required permissions |
| 14 | + |
| 15 | +The [API token](/fundamentals/api/get-started/create-token/) used in API requests to manage Snippets must have at least the following permission: |
| 16 | + |
| 17 | +- _Zone_ > _Snippets_ > _Edit_ |
| 18 | + |
| 19 | +:::note |
| 20 | +A token with this permission is only valid for the Snippets endpoints described in this page. You cannot use it to interact with the `http_request_snippets` phase via [Rulesets API](/ruleset-engine/rulesets-api/). |
| 21 | +::: |
| 22 | + |
| 23 | +## Endpoints |
| 24 | + |
| 25 | +To obtain the complete endpoint, append the Snippets endpoints listed below to the Cloudflare API base URL: |
| 26 | + |
| 27 | +```txt |
| 28 | +https://api.cloudflare.com/client/v4 |
| 29 | +``` |
| 30 | + |
| 31 | +The `{zone_id}` argument is the [zone ID](/fundamentals/setup/find-account-and-zone-ids/) (a hexadecimal string). You can find this value in the Cloudflare dashboard. |
| 32 | + |
| 33 | +The following table summarizes the available operations. |
| 34 | + |
| 35 | +| Operation | Verb + Endpoint | |
| 36 | +| ------------------------------------------ | ------------------------------------------------------ | |
| 37 | +| List all code snippets | `GET /zones/{zone_id}/snippets` | |
| 38 | +| Create/update code snippet | `PUT /zones/{zone_id}/snippets/{snippet_name}` | |
| 39 | +| Delete code snippet | `DELETE /zones/{zone_id}/snippets/{snippet_name}` | |
| 40 | +| List snippet rules | `GET /zones/{zone_id}/snippets/snippet_rules` | |
| 41 | +| Create/update/delete snippet rules | `PUT /zones/{zone_id}/snippets/snippet_rules` | |
| 42 | + |
| 43 | +## Example API calls |
| 44 | + |
| 45 | +### Create/update code snippet |
| 46 | + |
| 47 | +To create or update a Snippet, use the following `PUT` request. The snippet is named `{snippet_name}` and the body contains the JavaScript code. |
| 48 | + |
| 49 | +```bash |
| 50 | +curl --request PUT https://api.cloudflare.com/client/v4/zones/{zone_id}/snippets/{snippet_name} \ |
| 51 | +--header "Authorization: Bearer <API_TOKEN>" \ |
| 52 | +--header "Content-Type: multipart/form-data" \ |
| 53 | + |
| 54 | +--form metadata='{"main_module":"example.js"}' |
| 55 | +``` |
| 56 | + |
| 57 | +The required body parameters are: |
| 58 | + |
| 59 | +- `files`: The file with your JavaScript code. |
| 60 | +- `metadata`: Object containing `main_module`, which must match the filename of the uploaded file. |
| 61 | + |
| 62 | +To make this example work, save your JavaScript code in a file named `example.js`, and then execute `curl` command with a `PUT` request from the folder where `example.js` is located. |
| 63 | + |
| 64 | +```json title="Example response" |
| 65 | +{ |
| 66 | + "errors": [], |
| 67 | + "messages": [], |
| 68 | + "success": true, |
| 69 | + "result": { |
| 70 | + "created_on": "2023-07-24-00:00:00", |
| 71 | + "modified_on": "2023-07-24-00:00:00", |
| 72 | + "snippet_name": "snippet_name_01" |
| 73 | + } |
| 74 | +} |
| 75 | +``` |
| 76 | + |
| 77 | +### Create/update/delete snippet rules |
| 78 | + |
| 79 | +:::caution |
| 80 | +When using this endpoint to create a new rule and keep existing rules, you must include all rules in the request body. Omitting an existing rule will delete the corresponding rule. |
| 81 | +::: |
| 82 | + |
| 83 | +Once you have created a code snippet, you can link it to rules. This is done via the following `PUT` request to the `snippet_rules` endpoint. |
| 84 | + |
| 85 | +```bash |
| 86 | +curl --request PUT \ |
| 87 | +"https://api.cloudflare.com/client/v4/zones/{zone_id}/snippets/snippet_rules" \ |
| 88 | +--header "Authorization: Bearer <API_TOKEN>" \ |
| 89 | +--header "Content-Type: application/json" \ |
| 90 | +--data '{ |
| 91 | + "rules": [ |
| 92 | + { |
| 93 | + "description": "Trigger snippet on specific cookie", |
| 94 | + "enabled": true, |
| 95 | + "expression": "http.cookie eq \"a=b\"", |
| 96 | + "snippet_name": "snippet_name_01" |
| 97 | + } |
| 98 | + ] |
| 99 | +}' |
| 100 | +``` |
0 commit comments