npm now supports only granular access tokens, created at npmjs.com under your profile picture → Access Tokens → Generate New Token. Legacy access tokens have been removed. Tokens are read-only or read-and-write, expire on a date you choose (at least one day ahead), and the full value is shown only once.
npm key types
| Type | Prefix | Secret? | Scope | Expiry |
|---|---|---|---|---|
| Granular access token Up to 1,000 per account, 50 organizations and 50 packages/scopes per token. A write token with Bypass two-factor authentication overrides 2FA for publishing; direct publishing with such tokens is being deprecated for removal in January 2027. | — | Yes | Read-only or read-and-write (publish, or stage-only) on chosen packages, scopes and organizations; optional IP allow-list | Chosen at creation, at least one day in the future; the docs state no maximum |
How to create a npm key
- Sign in to npmjs.com, click your profile picture and choose Access Tokens.
- Click Generate New Token and enter a Token name.
- Optionally set Allowed IP Ranges, package and organization permissions, and Bypass two-factor authentication (off by default).
- Choose an expiration (predefined or Custom).
- Review and click Generate Token, then copy it; the full value is shown only once.
Limiting what the key can do
Limit a token to specific packages and scopes (up to 50) with No access, Read-only, Read and write (publish and stage) or Read and write (stage only). Stage-only tokens stage versions for a maintainer to approve instead of publishing directly. Organization permissions are separate. A token can never exceed its creator's permissions.
How to rotate a npm key
- Generate a new granular token with the same permissions.
- Update CI secrets and
.npmrcentries that use the old token. - Delete the old token in Access Tokens or with
npm token delete <ID>.
A revocation can take up to an hour to take effect. For CI publishing, npm recommends trusted publishing over bypass-2FA tokens.
How to revoke a npm key
- On npmjs.com choose Access Tokens, click the x next to the token (or select several and Delete Selected Tokens) and confirm.
- Or run
npm token list, copy the token ID and runnpm token delete <ID>.
If your npm key leaked
- Delete the token at once from Access Tokens or with
npm token delete <ID>(it can take up to an hour to take effect). - Create a replacement with the narrowest package scope and a short expiry, or move CI to trusted publishing.
- Check your published packages for versions you did not release.
- Review the account for unexpected token or maintainer changes.
Environment variables and CLI
npm's SDKs and docs read the key from NPM_TOKEN.
npm CLI (npm): npm login authenticates interactively; automation uses a token in .npmrc as //registry.npmjs.org/:_authToken=..., which may reference an environment variable with ${VARIABLE_NAME}
Things that catch people out
- Only the first and last four characters of a token are shown after creation; copy it immediately.
- An unscoped
_authToken=line in.npmrcis a bad config; scope it to the registry host. - A Bypass two-factor authentication token weakens 2FA for publishing; do not enable it for packages that require enforced 2FA.
- Legacy tokens were removed in November 2025, so old classic tokens and guides no longer apply.
Common npm authentication errors
E_STAGE_REQUIRED
A stage-only token tried to publish directly.
Recent incidents that put npm keys at risk
If you were affected by one of these, rotate your npm keys using the rotation steps above.
- tensorlake 0.5.144 on npm shipped a Shai-Hulud variant with a wipe-on-revoke dead-man's switch 2026-10-08
- GhostAction returns: fake security workflows pushed to 30,000+ repos to steal Actions secrets 2026-10-07
- keyv, flat-cache and cache-manager npm releases carried a CI-secret stealer with a revocation trap 2026-08-04
- 32 @redhat-cloud-services npm packages compromised after a VS Code extension hijacked a GitHub account 2026-06-01
- 14 typosquatted OpenSearch and DevOps npm packages stole AWS, Vault, GitHub Actions and npm secrets 2026-05-28
- Nx Console 18.95.0 VS Code extension shipped a credential stealer after a TanStack infection 2026-05-18
Using your npm 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 npm key, these intents fit:
| Intent | What BotPassword does |
|---|---|
list_credentials | Lists the credentials this agent is granted and what it may do with each. Never values. |
request_access | Asks you for a grant, which you approve with your master password or decline in BotPassword on your desktop, never in the chat. The agent gets a receipt, never a value. Approval on your phone is planned. |
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. |
capture_secret | Planned, not built yet: will read a newly created key straight from the page or command output into the vault. |
store_secret | Saves a value the agent already holds as a new vault item. |
Set it up in your agent: Aider · Amp · Claude Code · Claude Desktop · Cline · OpenAI Codex CLI · all agents
Frequently asked questions
How do I create an npm access token?
On npmjs.com click your profile picture → Access Tokens → Generate New Token, set permissions and expiry, then copy the token when it is shown.
Why is my npm access token expired or revoked?
Granular tokens expire on the date chosen at creation and legacy tokens have been removed, so create a new granular token.
How do I revoke an npm token?
Delete it under Access Tokens on npmjs.com, or run npm token delete <ID> using the full token ID.
What should I use to publish from CI instead of a token?
npm recommends trusted publishing over bypass-2FA tokens for CI/CD publishing.
Can an AI agent use my npm 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.