|
9 | 9 | content: Configure Snippets via API |
10 | 10 | --- |
11 | 11 |
|
| 12 | +import { APIRequest } from "~/components"; |
| 13 | + |
12 | 14 | You can create Snippets using the [Cloudflare API](/fundamentals/api/). |
13 | 15 |
|
14 | 16 | ## Required permissions |
@@ -48,7 +50,18 @@ The following table summarizes the available operations. |
48 | 50 |
|
49 | 51 | ### Create/update code snippet |
50 | 52 |
|
51 | | -To create or update a Snippet, use the following `PUT` request. The snippet is named `{snippet_name}` and the body contains the JavaScript code. |
| 53 | +To create or update a Snippet, use the following `PUT` request. The snippet is named `$SNIPPET_NAME` and the body contains the JavaScript code. |
| 54 | + |
| 55 | +TODO Fix example. |
| 56 | + |
| 57 | +<APIRequest |
| 58 | + path="/zones/{zone_id}/snippets/{snippet_name}" |
| 59 | + method="PUT" |
| 60 | + form={{ |
| 61 | + files: "@example.js", |
| 62 | + metadata: '{"main_module": "example.js"}', |
| 63 | + }} |
| 64 | +/> |
52 | 65 |
|
53 | 66 | ```bash |
54 | 67 | curl --request PUT https://api.cloudflare.com/client/v4/zones/{zone_id}/snippets/{snippet_name} \ |
@@ -90,19 +103,17 @@ When using this endpoint to create a new rule and keep existing rules, you must |
90 | 103 |
|
91 | 104 | 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. |
92 | 105 |
|
93 | | -```bash |
94 | | -curl --request PUT \ |
95 | | -"https://api.cloudflare.com/client/v4/zones/{zone_id}/snippets/snippet_rules" \ |
96 | | ---header "Authorization: Bearer <API_TOKEN>" \ |
97 | | ---header "Content-Type: application/json" \ |
98 | | ---data '{ |
99 | | - "rules": [ |
100 | | - { |
101 | | - "description": "Trigger snippet on specific cookie", |
102 | | - "enabled": true, |
103 | | - "expression": "http.cookie eq \"a=b\"", |
104 | | - "snippet_name": "snippet_name_01" |
105 | | - } |
106 | | - ] |
107 | | -}' |
108 | | -``` |
| 106 | +<APIRequest |
| 107 | + path="/zones/{zone_id}/snippets/snippet_rules" |
| 108 | + method="PUT" |
| 109 | + json={{ |
| 110 | + rules: [ |
| 111 | + { |
| 112 | + description: "Trigger snippet on specific cookie", |
| 113 | + enabled: true, |
| 114 | + expression: 'http.cookie eq "a=b"', |
| 115 | + snippet_name: "snippet_name_01", |
| 116 | + }, |
| 117 | + ], |
| 118 | + }} |
| 119 | +/> |
0 commit comments