Skip to content

Conversation

@ascorbic
Copy link
Contributor

@ascorbic ascorbic commented Dec 17, 2025

Fixes #10095.

Adds a new wrangler auth token command that retrieves the current authentication credentials for use with other tools and scripts. This is similar to gh auth token in the GitHub CLI.

The command supports all Wrangler authentication methods:

  • OAuth token from wrangler login (automatically refreshed if expired)
  • API token from CLOUDFLARE_API_TOKEN environment variable
  • API key/email from CLOUDFLARE_API_KEY + CLOUDFLARE_EMAIL (requires --json flag)

Basic usage:

wrangler auth token

JSON output with token type:

wrangler auth token --json
# Returns: { "type": "oauth", "token": "..." }
# Or: { "type": "api_token", "token": "..." }
# Or: { "type": "api_key", "key": "...", "email": "..." }

Implementation notes:

  • Outputs raw token value (no banner) for easy scripting via printBanner: false
  • Errors with helpful message if using API key/email without --json (no single token to return)
  • Refactored internal LocalState to localState for consistency
  • Added getOAuthTokenFromLocalState() function that only handles OAuth tokens from local state
  • Added JSDoc documentation for auth priority behavior in getAuthFromEnv()

Human Review Checklist:

  • Verify the --json output format is appropriate for all three auth types
  • Verify the error message for API key/email without --json is helpful
  • Verify the auth namespace is the appropriate location for this command

Devin PR requested by [email protected]

Link to Devin run: https://app.devin.ai/sessions/d407c26dd1da4edf905944a95e90226a

Add a new command `wrangler auth token` that retrieves the current OAuth token,
refreshing it if necessary. This is similar to `gh auth token` in the GitHub CLI
and allows users to easily retrieve their authentication token for use with other
tools and scripts.

Fixes #10095

Co-Authored-By: [email protected] <[email protected]>
@changeset-bot
Copy link

changeset-bot bot commented Dec 17, 2025

🦋 Changeset detected

Latest commit: e1e8955

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@devin-ai-integration
Copy link
Contributor

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR. Add '(aside)' to your comment to have me ignore it.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment and CI monitoring

@claude
Copy link
Contributor

claude bot commented Dec 17, 2025

Claude finished @ascorbic's task —— View job


Changeset Review

All changesets look good

The changeset file .changeset/auth-token-command.md passes all validation checks:

  • Version Type: Correctly set to minor (new CLI command)
  • Changelog Quality: Clear description with usage examples for the new feature
  • Markdown Headers: No h1/h2/h3 headers present
  • Format: Follows the recommended format with title and explanatory body
  • Examples: Includes practical usage examples with bash code blocks

@pkg-pr-new
Copy link

pkg-pr-new bot commented Dec 17, 2025

create-cloudflare

npm i https://pkg.pr.new/create-cloudflare@11682

@cloudflare/kv-asset-handler

npm i https://pkg.pr.new/@cloudflare/kv-asset-handler@11682

miniflare

npm i https://pkg.pr.new/miniflare@11682

@cloudflare/pages-shared

npm i https://pkg.pr.new/@cloudflare/pages-shared@11682

@cloudflare/unenv-preset

npm i https://pkg.pr.new/@cloudflare/unenv-preset@11682

@cloudflare/vite-plugin

npm i https://pkg.pr.new/@cloudflare/vite-plugin@11682

@cloudflare/vitest-pool-workers

npm i https://pkg.pr.new/@cloudflare/vitest-pool-workers@11682

@cloudflare/workers-editor-shared

npm i https://pkg.pr.new/@cloudflare/workers-editor-shared@11682

@cloudflare/workers-utils

npm i https://pkg.pr.new/@cloudflare/workers-utils@11682

wrangler

npm i https://pkg.pr.new/wrangler@11682

commit: e1e8955

Copy link
Contributor Author

@ascorbic ascorbic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Tested locally.

@ascorbic ascorbic marked this pull request as ready for review December 17, 2025 12:48
@ascorbic ascorbic requested a review from a team as a code owner December 17, 2025 12:48
@devin-ai-integration
Copy link
Contributor

Thanks for testing and reviewing! The PR is ready for merge whenever you're ready.

@ascorbic
Copy link
Contributor Author

(aside) This does need docs. I'll add them if everyone agrees this is the right API

- Refactor getAuthToken to getOAuthTokenFromLocalState (only handles OAuth)
- Rename LocalState to localState throughout user.ts
- Add --json flag to return structured output with token type
- Support API key/email auth with --json flag
- Add JSDoc documentation for auth priority behavior
- Update changeset to reflect all auth types
- Update tests (24 tests passing)

Co-Authored-By: [email protected] <[email protected]>
devin-ai-integration bot added a commit to cloudflare/cloudflare-docs that referenced this pull request Dec 18, 2025
Add documentation for the new `wrangler auth token` command that retrieves
authentication credentials for use with other tools and scripts.

Documents:
- Basic usage
- --json flag for structured output with token type
- Support for OAuth, API token, and API key/email auth types

Related to cloudflare/workers-sdk#11682

Co-Authored-By: [email protected] <[email protected]>
@ascorbic ascorbic requested a review from vicb December 18, 2025 10:52
Copy link
Contributor

@vicb vicb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Matt!

@github-project-automation github-project-automation bot moved this from Untriaged to Approved in workers-sdk Dec 18, 2025
@ascorbic ascorbic merged commit b993d95 into main Dec 18, 2025
40 of 41 checks passed
@ascorbic ascorbic deleted the devin/1765967006-auth-token-command branch December 18, 2025 14:11
@github-project-automation github-project-automation bot moved this from Approved to Done in workers-sdk Dec 18, 2025
@claude claude bot mentioned this pull request Dec 18, 2025
@karalabe
Copy link

Ty ❤️

AmirSa12 pushed a commit to AmirSa12/workers-sdk that referenced this pull request Jan 5, 2026
* feat(wrangler): add `wrangler auth token` command

Add a new command `wrangler auth token` that retrieves the current OAuth token,
refreshing it if necessary. This is similar to `gh auth token` in the GitHub CLI
and allows users to easily retrieve their authentication token for use with other
tools and scripts.

Fixes cloudflare#10095

Co-Authored-By: [email protected] <[email protected]>

* fix: output auth token without preamble and update snapshots

Co-Authored-By: [email protected] <[email protected]>

* fix: use printBanner: false for proper preamble suppression

Co-Authored-By: [email protected] <[email protected]>

* docs: add usage example to changeset per review

Co-Authored-By: [email protected] <[email protected]>

* feat(wrangler): add --json flag and refactor auth token command

- Refactor getAuthToken to getOAuthTokenFromLocalState (only handles OAuth)
- Rename LocalState to localState throughout user.ts
- Add --json flag to return structured output with token type
- Support API key/email auth with --json flag
- Add JSDoc documentation for auth priority behavior
- Update changeset to reflect all auth types
- Update tests (24 tests passing)

Co-Authored-By: [email protected] <[email protected]>

* chore: trigger CI re-run for PR description validation

Co-Authored-By: [email protected] <[email protected]>

---------

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Support retrieving the auth token (akin to gh auth token)

3 participants