Skip to content
AnswerWell geo Documentation

Research · 7 min read · Answerwell

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)

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

Containers, remote SSH, GitHub Codespaces, and similar environments often cannot reach localhost:8976. Use the OAuth 2.0 Device Authorization Grant:

npx wrangler login --device

Because –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.

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

Otherwise, 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-keyring

Wrangler 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

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:

  1. CLOUDFLARE_API_TOKEN — overrides all profiles; you cannot create, activate, deactivate, or delete profiles while it is set.
  2. --profile on a single command (not supported on auth, login, logout, or whoami).
  3. Nearest activated ancestor directory (from --config path or cwd).
  4. Default profile from wrangler login / wrangler logout.

Account selection within the resolved profile:

  1. account_id in the Wrangler config file or CLOUDFLARE_ACCOUNT_ID.
  2. 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.

  1. Create a named profile and complete OAuth, choosing which accounts it may reach:

    wrangler auth create work
  2. Bind the profile to a project directory (defaults to cwd):

    wrangler auth activate work ~/projects/work
  3. List profiles and bindings:

    wrangler auth list
  4. Run one command with another profile without changing bindings:

    wrangler deploy --profile staging
  5. 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:

  1. API token from CLOUDFLARE_API_TOKEN
  2. API key and email from CLOUDFLARE_API_KEY and CLOUDFLARE_EMAIL (JSON only, with --json)
  3. OAuth token from wrangler login (refreshed if expired)
wrangler auth token --json

Use this when another tool needs the same credential Wrangler would use—never log or commit the output.

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

  • Wrong account_id in wrangler.toml — API calls route to another account; error code 7003 is described in Troubleshooting builds.
  • Expecting profiles in CI — set CLOUDFLARE_API_TOKEN instead; 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_TOKEN set 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.
  1. Run wrangler whoami in the project directory and confirm the email, account, and credential storage location match what you expect.
  2. If you use profiles, run wrangler auth list and verify the profile bound to that directory is the intended one.
  3. From the same directory, run wrangler auth token --json and confirm the type field matches OAuth, api_token, or api_key as intended (handle output as a secret).
  4. In CI, confirm the job defines CLOUDFLARE_API_TOKEN and CLOUDFLARE_ACCOUNT_ID as platform secrets, not repository files, per GitHub Actions.
  5. You can check automatically in Answerwell (our tool): https://answerwell.org/workspace

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.