Cloud and hosting

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

Last checked 2026-10-25 against Cloudflare (API tokens and Global API Key)'s documentation

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.

Cloudflare (API tokens and Global API Key) key types

TypePrefixSecret?ScopeExpiry
Account API token
Not tied to a user. Can be managed by Super Administrators or the member who created it.
cfat_YesThe permission groups and resources you selectOptional expiration date set at creation
User API token
Tied to your user. Verify with the token verify endpoint.
—YesThe permission groups (Account, User, Zone) and resources you selectOptional TTL and client IP filtering at creation
Global API Key
One per user. New and rolled keys use the cfk_ prefix so credential scanners can detect leaks. Cloudflare says existing customers should migrate to API tokens.
cfk_YesEverything the user can do, including deleting zones and changing DNS recordsNo 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.

Open Cloudflare (API tokens and Global API Key)'s key settings

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.

Rolling invalidates the old token secret; the new one keeps the same access and permissions. To change the Global API Key, use Change in the API Keys section of the same page.

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 your Cloudflare (API tokens and Global API Key) 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 and CLI

Cloudflare (API tokens and Global API Key)'s SDKs and docs read the key from CLOUDFLARE_API_TOKEN, CLOUDFLARE_ACCOUNT_ID, CLOUDFLARE_API_KEY or CLOUDFLARE_EMAIL.

Wrangler (wrangler): wrangler login for browser sign-in, or set CLOUDFLARE_API_TOKEN for CI

Things that catch people out

  • 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.

Recent incidents that put Cloudflare (API tokens and Global API Key) keys at risk

If you were affected by one of these, rotate your Cloudflare (API tokens and Global API Key) keys using the rotation steps above.

Using your Cloudflare (API tokens and Global API Key) key with AI agents

A coding agent that can read your .env file or your shell history can read the key, and anything in its context can end up in logs, transcripts or a prompt-injected request. The safer pattern is to keep the key in a credential broker that performs the authenticated step for the agent and hands back only the result.

BotPassword works this way. For a Cloudflare (API tokens and Global API Key) key, these intents fit:

IntentWhat BotPassword does
list_credentialsLists the credentials this agent is granted and what it may do with each. Never values.
run_withRuns 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_requestMakes 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_loginLets 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.
rotatePartly 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_secretSaves a value the agent already holds as a new vault item.

Cloudflare (API tokens and Global API Key) also publishes its own MCP server. Whatever server the agent uses, keep the credential it authenticates with out of the agent's context.

Set it up in your agent: Aider · Amp · Claude Code · Claude Desktop · Cline · OpenAI Codex CLI · all agents

Frequently asked questions

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.

Stop pasting Cloudflare (API tokens and Global API Key) keys into agents

BotPassword is in early access. Request an invite and we'll email you when your spot opens.