# Cloudflare (API tokens and Global API Key) API keys: find, store, rotate, and use them safely with AI agents

Source: https://botpassword.com/keys/cloudflare
Last checked: 2026-10-25

Cloudflare API tokens are created at My Profile → API Tokens (user tokens) or Manage Account → API Tokens (account tokens) with Create Token. The token secret is shown only once. The legacy Global API Key grants everything you can do and Cloudflare does not recommend it for new use.

## Key types

| Type | Prefix | Secret | Scope | Expiry |
|---|---|---|---|---|
| Account API token | `cfat_` | Yes | The permission groups and resources you select | Optional expiration date set at creation |
| User API token | — | Yes | The permission groups (Account, User, Zone) and resources you select | Optional TTL and client IP filtering at creation |
| Global API Key | `cfk_` | Yes | Everything the user can do, including deleting zones and changing DNS records | No expiry, time-window or IP-range limits |

## How to create a Cloudflare (API tokens and Global API Key) key

1. Go to **My Profile** → **API Tokens** (user token) or **Manage Account** → **API Tokens** (account token).
2. Select **Create Token** and pick a template or build a custom token.
3. Name the token and set permissions (permission group and Read or Edit access level).
4. Select the resources (specific zones or accounts) and optionally set Client IP Address Filtering and TTL.
5. Select **Continue to summary**, then **Create Token**, and copy the secret immediately; it is shown only once.

## Limiting what the key can do

Cloudflare tokens are scoped by permission group (Account, User or Zone), access level (`Read` for read-only, `Edit` for full create/read/update/delete/list) and resource (specific zones or accounts). You can add IP address filtering and a TTL. The Global API Key cannot be narrowed.

## How to rotate a Cloudflare (API tokens and Global API Key) key

1. Go to **My Profile** → **API Tokens**.
2. Next to the token, open the three-dot menu and select **Roll**.
3. Select **Confirm** to generate the new secret, then update your applications.

## How to revoke a Cloudflare (API tokens and Global API Key) key

1. Roll the token to invalidate the current secret, or delete the token from the API Tokens page.
2. For the Global API Key, select **Change** in the API Keys section to replace it.

## If the key leaked

1. Roll the token (or Change the Global API Key) immediately.
2. Update every application and CI secret that used it.
3. Review the account audit log for activity during the exposure.

## Environment variables

- `CLOUDFLARE_API_TOKEN`
- `CLOUDFLARE_ACCOUNT_ID`
- `CLOUDFLARE_API_KEY`
- `CLOUDFLARE_EMAIL`

## Gotchas

- The token secret is shown only once at creation.
- `CLOUDFLARE_API_KEY` is paired with `CLOUDFLARE_EMAIL` and is the older authentication method for the Global API Key.
- The Global API Key has the same permissions you do and you can have only one per user, which makes rotating it in production harder.
- Verify a user token by calling `https://api.cloudflare.com/client/v4/user/tokens/verify` with a Bearer header; a valid token returns status `active`.

## Using this key with AI agents (BotPassword)

- `list_credentials`: Lists the credentials this agent is granted and what it may do with each. Never values.
- `run_with`: Runs a command with the key injected as an environment variable or a 0600 temporary file, never on the command line, and redacts the key from the output.
- `http_request`: Makes an authenticated API call after checking the destination against the hosts this key may be sent to. The response comes back with credential-shaped values redacted, and one that holds the key itself is refused.
- `cli_login`: Lets git, Docker, the AWS CLI and kubectl log in through bpw's credential helper, so the token is fetched when the tool needs it and not written into its config. The helper hands the value to the tool, so the grant is always disclosing. Other tools use run_with; OAuth device logins are planned.
- `rotate`: Partly built: creates a new key, stores it as pending, verifies it, promotes it, then revokes the old key after a grace period. Today it works for Cloudflare tokens and AWS IAM keys you have set up, and an agent can start it only under a grant with autopilot; other providers cannot be rotated through their API yet.
- `store_secret`: Saves a value the agent already holds as a new vault item.

## FAQ

### Where do I find my Cloudflare API token?

You cannot view it after creation. Go to My Profile → API Tokens, create a new token, and copy the secret then, or roll an existing token to get a new secret.

### What is the difference between a Cloudflare API token and the Global API Key?

A token can be limited by permission, resource, IP and expiry. The Global API Key has full access to your account's resources with none of those limits, and Cloudflare recommends migrating to tokens.

### How do I roll a Cloudflare API token?

Go to My Profile → API Tokens, open the three-dot menu next to the token, select Roll and confirm. The old secret stops working.

### Which environment variable does Wrangler use?

`CLOUDFLARE_API_TOKEN`, with `CLOUDFLARE_ACCOUNT_ID` optional for Workers operations.

### Can an AI agent use my Cloudflare (API tokens and Global API Key) key without seeing it?

Yes, if the key never enters the agent's context. Instead of pasting the key into a chat or leaving it in a `.env` file the agent can read, keep it in a credential broker that injects it into the command or request on the agent's behalf. BotPassword does this with `list_credentials`, `run_with`, `http_request`, so the agent gets the result and never the value.

## Sources

- https://developers.cloudflare.com/fundamentals/api/get-started/create-token/
- https://developers.cloudflare.com/fundamentals/api/get-started/keys/
- https://developers.cloudflare.com/fundamentals/api/get-started/account-owned-tokens/
- https://developers.cloudflare.com/fundamentals/api/how-to/roll-token/
- https://developers.cloudflare.com/workers/wrangler/system-environment-variables/
- https://developers.cloudflare.com/agents/model-context-protocol/mcp-servers-for-cloudflare/
