From 2193f1b476a461e9f47545e1cb3bbaf71032a6a5 Mon Sep 17 00:00:00 2001 From: Denise Pena Date: Wed, 22 Jan 2025 16:08:05 -0600 Subject: [PATCH 1/2] Added info for account and user tokens --- .../api/how-to/create-via-api.mdx | 91 ++++++++++++++++++- 1 file changed, 89 insertions(+), 2 deletions(-) diff --git a/src/content/docs/fundamentals/api/how-to/create-via-api.mdx b/src/content/docs/fundamentals/api/how-to/create-via-api.mdx index 06c1fda17b2428a..d1372c2d1a4e84c 100644 --- a/src/content/docs/fundamentals/api/how-to/create-via-api.mdx +++ b/src/content/docs/fundamentals/api/how-to/create-via-api.mdx @@ -6,7 +6,7 @@ sidebar: --- -import { Render } from "~/components" +import { Render, Tabs, TabItem } from "~/components" Generate new API tokens on the fly via the API. Before you can do this, you must create an API token in the Cloudflare dashboard that can create subsequent tokens. @@ -24,7 +24,7 @@ Cloudflare also recommends limiting the use of the token via client IP address f ## Creating API tokens with the API -Once you create an API token that can create other tokens, you can now use it in the API. Refer to the [API schema docs](/api/resources/user/subresources/tokens/methods/create/) for more information. +You can create a user owned token or account owned token to use with the API. Refer to the [user owned token](/api/resources/user/subresources/tokens/methods/create/) or the [account owned token](/api/resources/accounts/subresources/tokens/methods/create/) API schema docs for more information. To create a token: @@ -130,6 +130,93 @@ Each parameter in the `in` and `not_in` objects must be in CIDR notation. For ex Combine the previous information to create a token as in the following example: + + +```bash +curl "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/tokens" \ +--header "Authorization: Bearer " \ +--header "Content-Type: application/json" \ +--data '{ + "name": "readonly token", + "policies": [ + { + "effect": "allow", + "resources": { + "com.cloudflare.api.account.zone.eb78d65290b24279ba6f44721b3ea3c4": "*", + "com.cloudflare.api.account.zone.22b1de5f1c0e4b3ea97bb1e963b06a43": "*" + }, + "permission_groups": [ + { + "id": "c8fed203ed3043cba015a93ad1616f1f", + "name": "Zone Read" + }, + { + "id": "82e64a83756745bbbb1c9c2701bf816b", + "name": "DNS Read" + } + ] + } + ], + "not_before": "2020-04-01T05:20:00Z", + "expires_on": "2020-04-10T00:00:00Z", + "condition": { + "request.ip": { + "in": [ + "199.27.128.0/21", + "2400:cb00::/32" + ], + "not_in": [ + "199.27.128.1/32" + ] + } + } +}' +``` + + +```bash +curl "https://api.cloudflare.com/client/v4/user/tokens" \ +--header "Authorization: Bearer " \ +--header "Content-Type: application/json" \ +--data '{ + "name": "readonly token", + "policies": [ + { + "effect": "allow", + "resources": { + "com.cloudflare.api.account.zone.eb78d65290b24279ba6f44721b3ea3c4": "*", + "com.cloudflare.api.account.zone.22b1de5f1c0e4b3ea97bb1e963b06a43": "*" + }, + "permission_groups": [ + { + "id": "c8fed203ed3043cba015a93ad1616f1f", + "name": "Zone Read" + }, + { + "id": "82e64a83756745bbbb1c9c2701bf816b", + "name": "DNS Read" + } + ] + } + ], + "not_before": "2020-04-01T05:20:00Z", + "expires_on": "2020-04-10T00:00:00Z", + "condition": { + "request.ip": { + "in": [ + "199.27.128.0/21", + "2400:cb00::/32" + ], + "not_in": [ + "199.27.128.1/32" + ] + } + } +}' +``` + + + ```bash curl "https://api.cloudflare.com/client/v4/user/tokens" \ --header "Authorization: Bearer " \ From a552a140f5a432c3756d7dad0b8f23763fa87b36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Denise=20Pe=C3=B1a?= <75506267+dcpena@users.noreply.github.com> Date: Wed, 22 Jan 2025 16:23:26 -0600 Subject: [PATCH 2/2] Edited example URL to be consistent with style guide --- src/content/docs/fundamentals/api/how-to/create-via-api.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/content/docs/fundamentals/api/how-to/create-via-api.mdx b/src/content/docs/fundamentals/api/how-to/create-via-api.mdx index d1372c2d1a4e84c..b3ea27fb775a722 100644 --- a/src/content/docs/fundamentals/api/how-to/create-via-api.mdx +++ b/src/content/docs/fundamentals/api/how-to/create-via-api.mdx @@ -133,7 +133,7 @@ Combine the previous information to create a token as in the following example: ```bash -curl "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/tokens" \ +curl "https://api.cloudflare.com/client/v4/accounts/{account_id}/tokens" \ --header "Authorization: Bearer " \ --header "Content-Type: application/json" \ --data '{