You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Once you [create your API token](/fundamentals/api/get-started/create-token/), all API requests are authorized in the same way. Cloudflare uses the [RFC standard](https://tools.ietf.org/html/rfc6750#section-2.1)`Authorization: Bearer <API_TOKEN>` interface. An example request is shown below.
9
11
10
12
{/* <!-- The following example uses an invalid API key to demonstrate what those keys look like --> */}
Never send or store your API token secret in plaintext. Also be sure not to check it into code repositories, especially public ones.
18
20
21
+
Consider defining [environment variables](#environment-variables) for the zone or account ID, as well as for authentication credentials (for example, the API token).
22
+
19
23
To format JSON output for readability in the command line, you can use a tool like `jq`, a command-line JSON processor. For more information on obtaining and installing `jq`, refer to [Download jq](https://stedolan.github.io/jq/download/).
20
24
21
25
The following example will format the curl JSON output using `jq`:
Every Cloudflare API element is fixed to a version number. The latest version is Version 4. The stable base URL for all Version 4 HTTPS endpoints is: `https://api.cloudflare.com/client/v4/`
31
35
@@ -39,14 +43,15 @@ For specific guidance on making API calls, refer to the following resources:
39
43
40
44
Several Cloudflare endpoints have optional query parameters to filter incoming results, such as [List Zones](/api/resources/zones/methods/list/).
41
45
42
-
When adding those query parameters, make sure you enclose the URL in quotes `'` (just like the header values), or the API call might error.
46
+
When adding those query parameters, make sure you enclose the URL in double quotes `""` (just like the header values), or the API call might error.
You can enclose strings using either single quotes (`''`) or double quotes (`""`). However, using single quotes prevents variable substitution in shells like `bash`. In the previous example, this would mean that the `$ACCOUNT_ID` and `$CLOUDFLARE_API_TOKEN`[environment variables](#environment-variables) would not be replaced with their values.
54
+
50
55
### Pagination
51
56
52
57
Sometimes there will be too many results to display via the default page size, for example you might receive the following:
@@ -63,7 +68,7 @@ There are two query parameter options, which can be combined to paginate across
63
68
-`page=x` enables you to select a specific page.
64
69
-`per_page=xx` enables you to adjust the number of results displayed on a page. If you select too many, you may get a timeout.
65
70
66
-
An example might be `https://api.cloudflare.com/client/v4/zones/zone-identifier/dns_records?per_page=100&page=2`.
71
+
An example might be `https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records?per_page=100&page=2`.
67
72
68
73
Other options are:
69
74
@@ -78,7 +83,7 @@ Recent versions of Windows 10 and 11 [already include the curl tool](https://cur
78
83
79
84
### Using a Command Prompt window
80
85
81
-
To use the Cloudflare API with curl on a Command Prompt window, you must use double quotes (`"`) as string delimiters instead of single quotes (`'`).
86
+
To use the Cloudflare API with curl on a Command Prompt window, you must use double quotes (`"`) as string delimiters.
82
87
83
88
A typical `PATCH` request will be similar to the following:
84
89
@@ -110,10 +115,10 @@ PowerShell has specific cmdlets (`Invoke-RestMethod` and `ConvertFrom-Json`) for
110
115
The following example uses the `Invoke-RestMethod` cmdlet:
The command assumes that the environment variables `ZONE_ID`, `CLOUDFLARE_EMAIL`, and `CLOUDFLARE_API_KEY` have been previously defined. For more information, refer to [Environment variables](#environment-variables).
133
+
127
134
By default, the output will only contain the first level of the JSON object hierarchy (in the above example, the content of objects such as `hosts` and `certificates` is not shown). To show additional levels and format the output like the `jq` tool, you can use the `ConvertFrom-Json` cmdlet specifying the desired maximum depth (by default, `2`):
To escape a double quote (`"`) character in a request body (specified with `-d` or `--data`), prepend it with another double quote (`"`) or a backslash (`\`). You must escape double quotes even when using single quotes (`'`) as string delimiters.
@@ -184,7 +191,98 @@ To break a single command in two or more lines, use a backtick (`` ` ``) charact
You can define environment variables for values that repeat between commands, such as the zone or account ID. The lifetime of an environment variable can be the current shell session, all future sessions of the current user, or even all future sessions of all users on the machine you are defining them.
202
+
203
+
You can also use environment variables for keeping authentication credentials (API token, API key, and email) and reusing them in different commands. However, make sure you define these values in the smallest possible scope (either the current shell session only or all new sessions for the current user).
204
+
205
+
The procedure for setting and referencing environment variables depends on your platform and shell.
206
+
207
+
### Define an environment variable
208
+
209
+
<TabssyncKey="LinuxPowershellCmd"> <TabItemlabel="Linux and macOS">
210
+
211
+
To define a `ZONE_ID` environment variable for the current shell session, run the following command:
212
+
213
+
```sh
214
+
export ZONE_ID='f2ea6707005a4da1af1b431202e96ac5'
215
+
```
216
+
217
+
To define the variable for all new shell sessions for the current user, add the command above at the end of your shell configuration file (for example, `~/.bashrc` for the `bash` shell and `~/.zshrc` for the `zsh` shell).
218
+
219
+
</TabItem> <TabItemlabel="PowerShell">
220
+
221
+
To define a `ZONE_ID` environment variable for the current PowerShell session, run the following command:
222
+
223
+
```powershell
224
+
$Env:ZONE_ID='f2ea6707005a4da1af1b431202e96ac5'
225
+
```
226
+
227
+
To define the environment variable for all new PowerShell sessions of the current user, set the variable in your PowerShell profile. You can get the path to your PowerShell profile by running `echo $PROFILE`.
228
+
229
+
Alternatively, set the variable for all new PowerShell sessions of the current user using the `SetEnvironmentVariable()` method of the `System.Environment` class. For example:
To define a `ZONE_ID` environment variable for the current Command Prompt session, run the following command:
240
+
241
+
```txt frame="terminal"
242
+
set ZONE_ID=f2ea6707005a4da1af1b431202e96ac5
243
+
```
244
+
245
+
To define an environment variable for all future Command Prompt sessions of the current user, run the following command:
246
+
247
+
```txt frame="terminal"
248
+
setx ZONE_ID f2ea6707005a4da1af1b431202e96ac5
249
+
```
250
+
251
+
Running this command will not affect the current window. You will need to either run the `set` command or close and start a new Command Prompt window.
252
+
253
+
</TabItem> </Tabs>
254
+
255
+
### Reference an environment variable
256
+
257
+
<TabssyncKey="LinuxPowershellCmd"> <TabItemlabel="Linux and macOS">
258
+
259
+
When referencing an environment variable in a command, add a `$` prefix to the variable name (for example, `$ZONE_ID`). Make sure that the full string referencing the variable is either unquoted (if it does not contain spaces) or enclosed in double quotes (`""`).
When referencing an environment variable in a command, add an `$Env:` prefix to the variable name (for example, `$Env:ZONE_ID`). Make sure that the full string referencing the variable is either unquoted or enclosed in double quotes (`""`).
0 commit comments