CLI Authentication: OAuth Login, API Tokens, and Auth Profiles
How to log in to the Workers CLI with OAuth or API tokens, use named auth profiles, and authenticate CI jobs safely.
This page summarizes Cloudflare documentation on authenticating the Workers CLI (Wrangler) and the related cf CLI. A single Cloudflare user can belong to many accounts and zones; the CLI must know which credentials and account to use for each command.
Authentication methods: OAuth login vs API token
Section titled “Authentication methods: OAuth login vs API token”Wrangler can authenticate with an OAuth token from wrangler login or with an API token (and account ID) via environment variables—the pattern used in CI/CD.
When running Wrangler locally, authentication to the Cloudflare API happens via the wrangler login command, which initiates an interactive authentication flow. Since CI/CD environments are non-interactive, Wrangler requires a Cloudflare API token and account ID to authenticate with the Cloudflare API.
— Cloudflare, GitHub Actions
The legacy wrangler config flow (email plus global API key or token typed at a prompt) is deprecated for new setups:
The wrangler config command is no longer available for authenticating via an API token.
Use wrangler login / wrangler logout to manage OAuth authentication, or provide an API token via the CLOUDFLARE_API_TOKEN environment variable.
— Cloudflare, Deprecations
Older docs still describe CF_API_TOKEN, CF_ACCOUNT_ID, CF_EMAIL, and CF_API_KEY; current Wrangler CI guidance uses CLOUDFLARE_API_TOKEN and CLOUDFLARE_ACCOUNT_ID. Inline environment variables override stored OAuth credentials.
| Method | Best for | How you set it |
|---|---|---|
OAuth (wrangler login) |
Local development, interactive use | Browser or device flow; stored under ~/.config/.wrangler/ |
| API token + account ID | CI/CD, scripts, least-privilege tokens | CLOUDFLARE_API_TOKEN, CLOUDFLARE_ACCOUNT_ID |
| API key + email | Legacy only; not recommended | CLOUDFLARE_API_KEY, CLOUDFLARE_EMAIL (requires wrangler auth token --json to export) |
Granular permissions require an API token
Section titled “Granular permissions require an API token”The wrangler login OAuth flow does not currently support granular authorization.
To use granular permissions with Wrangler, authenticate with an account-owned API token.
— Cloudflare, Roles and permissions
Create a token in the dashboard with only the permissions your command needs, then export the variables before running Wrangler.
wrangler login: OAuth in the browser, remotely, or in a container
Section titled “wrangler login: OAuth in the browser, remotely, or in a container”wrangler login opens a browser OAuth flow by default and requests Cloudflare API access. Optional flags include --scopes (limit OAuth scopes), --browser=false (print the link only), and --callback-host / --callback-port (default localhost:8976).
Device flow when localhost callback fails
Section titled “Device flow when localhost callback fails”Containers, remote SSH, GitHub Codespaces, and similar environments often cannot reach localhost:8976. Use the OAuth 2.0 Device Authorization Grant:
npx wrangler login --deviceBecause –device has no callback server, those workarounds are no longer necessary.
— Cloudflare, Log in to Wrangler without a local callback server
Wrangler prints a verification URL and user code, polls for approval (about five minutes), and can open the browser for you. Use --browser=false to approve from another device. --device cannot be combined with --callback-host or --callback-port.
Containers: port mapping or device flow
Section titled “Containers: port mapping or device flow”If you use the default callback flow inside Docker, map port 8976 and listen on all interfaces:
docker run -p 8976:8976 <your-image>npx wrangler login --callback-host=0.0.0.0Otherwise, prefer wrangler login --device so you do not need port forwarding.
Store OAuth credentials in the OS keychain
Section titled “Store OAuth credentials in the OS keychain”By default, OAuth tokens live in a plaintext TOML file. Opt in to encrypted storage:
npx wrangler login --use-keyringWrangler can now store the OAuth credentials returned by wrangler login in an AES-256-GCM -encrypted file, with the encryption key held in your operating system keychain.
— Cloudflare, Store Wrangler’s OAuth credentials in your OS keychain
wrangler whoami shows where credentials are stored. Opt out with wrangler login --no-use-keyring. Per-command override: CLOUDFLARE_AUTH_USE_KEYRING=true|false. API tokens from the environment still take priority over stored OAuth.
OAuth access tokens from login start with cfoat_. Treat them as secrets; wrangler auth token can print the current token for other tools and refreshes OAuth tokens when expired.
CI and automation: environment variables and secrets
Section titled “CI and automation: environment variables and secrets”For GitHub Actions, GitLab CI, or any non-interactive runner:
Go to your CI/CD platform and add the following as secrets:
CLOUDFLARE_ACCOUNT_ID: Set to the Cloudflare account ID for the account on which you want to deploy your Worker. CLOUDFLARE_API_TOKEN: Set to the Cloudflare API token you generated.
— Cloudflare, GitHub Actions
We do not recommend storing Cloudflare credentials as plaintext.
For CI pipelines, use an internal or secret storage tool (such as Vault ↗︎).
— Cloudflare, Best practices
The cf CLI follows the same rule in automation: set CLOUDFLARE_API_TOKEN (and usually CLOUDFLARE_ACCOUNT_ID); cf auth login is for interactive use only.
Authentication profiles: multiple accounts on one machine Beta
Section titled “Authentication profiles: multiple accounts on one machine ”Authentication profiles are named OAuth logins you can bind to directories so Wrangler picks the right account without repeated wrangler login calls.
Use profiles when you work across more than one Cloudflare account on the same machine:
Agency and client work — keep a separate login for each client account and bind it to that client’s project directory.
— Cloudflare, Authentication profiles
How profile and account resolution works
Section titled “How profile and account resolution works”A profile combines (1) an OAuth login scoped to accounts you choose during login and (2) a directory binding so that directory and subdirectories use that profile.
Profile selection (highest priority first), from Authentication profiles:
CLOUDFLARE_API_TOKEN— overrides all profiles; you cannot create, activate, deactivate, or delete profiles while it is set.--profileon a single command (not supported onauth,login,logout, orwhoami).- Nearest activated ancestor directory (from
--configpath or cwd). - Default profile from
wrangler login/wrangler logout.
Account selection within the resolved profile:
account_idin the Wrangler config file orCLOUDFLARE_ACCOUNT_ID.- Otherwise, the account chosen at profile login.
If the command targets an account the profile cannot reach, Wrangler fails with an error naming the account and profile—it does not fall back to another account. Pair profiles with an explicit account_id in config to avoid deploying to the wrong account.
Profiles do not apply in CI or containers; those environments use CLOUDFLARE_API_TOKEN per Authentication profiles.
Create, bind, and switch profiles
Section titled “Create, bind, and switch profiles”-
Create a named profile and complete OAuth, choosing which accounts it may reach:
wrangler auth create work -
Bind the profile to a project directory (defaults to cwd):
wrangler auth activate work ~/projects/work -
List profiles and bindings:
wrangler auth list -
Run one command with another profile without changing bindings:
wrangler deploy --profile staging -
Remove a directory binding (
wrangler auth deactivate) or delete a profile (wrangler auth delete <name>).
The cf CLI mirrors this pattern with cf auth create, cf auth activate, and --profile; see Get started.
wrangler auth token: export credentials for scripts
Section titled “wrangler auth token: export credentials for scripts”wrangler auth token returns the active credential in priority order:
- API token from
CLOUDFLARE_API_TOKEN - API key and email from
CLOUDFLARE_API_KEYandCLOUDFLARE_EMAIL(JSON only, with--json) - OAuth token from
wrangler login(refreshed if expired)
wrangler auth token --jsonUse this when another tool needs the same credential Wrangler would use—never log or commit the output.
Cloudflare Access and local wrangler dev
Section titled “Cloudflare Access and local wrangler dev”When a Worker or remote bindings sit behind Cloudflare Access, Wrangler may run an interactive cloudflared access login flow if the Access policy allows user login. In CI or policies that only allow service tokens, set:
CLOUDFLARE_ACCESS_CLIENT_IDCLOUDFLARE_ACCESS_CLIENT_SECRET
Attach a Service Auth policy to the existing Access application; do not create a duplicate application for the same hostname. Details are in Local development.
Common mistakes
Section titled “Common mistakes”- Wrong
account_idinwrangler.toml— API calls route to another account; error code7003is described in Troubleshooting builds. - Expecting profiles in CI — set
CLOUDFLARE_API_TOKENinstead; it always wins over profiles. - OAuth when you need least privilege — use a scoped API token; OAuth does not support granular authorization today.
- Leaving
CLOUDFLARE_API_TOKENset locally — blocks profile management and forces token auth for every command. - Storing tokens in git or plain CI logs — use secret managers; see Best practices.
How to check this on your site
Section titled “How to check this on your site”- Run
wrangler whoamiin the project directory and confirm the email, account, and credential storage location match what you expect. - If you use profiles, run
wrangler auth listand verify the profile bound to that directory is the intended one. - From the same directory, run
wrangler auth token --jsonand confirm thetypefield matches OAuth,api_token, orapi_keyas intended (handle output as a secret). - In CI, confirm the job defines
CLOUDFLARE_API_TOKENandCLOUDFLARE_ACCOUNT_IDas platform secrets, not repository files, per GitHub Actions. - You can check automatically in Answerwell (our tool): https://answerwell.org/workspace
Report an error or ask a question
Section titled “Report an error or ask a question”If something on this page is wrong or unclear, email yimasynthetic@gmail.com with the page URL and the exact statement you are asking about, and we will correct or clarify it.