Developer tools

PyPI API tokens: find, store, rotate, and use them safely with AI agents

PyPI API tokens start with pypi- and can be scoped to your whole account or to a single project. Authenticate uploads with the username __token__ and the token as the password. PyPI recommends Trusted Publishing, which mints project-scoped tokens that are valid for only 15 minutes, instead of long-lived tokens in CI.

PyPI key types

TypePrefixSecret?ScopeExpiry
API token
Project-scoped tokens can only be made once the project exists. Tokens are shown once.
pypi-YesEntire account (all projects) or a single projectLong-lived; no expiry is documented in the pages used
Trusted Publishing token (minted)
Issued automatically after PyPI verifies the CI provider's OIDC token; no manual token creation.
—YesBehaves like a project-scoped API tokenValid for 15 minutes from creation

How to create a PyPI key

  1. Sign in to your PyPI account and open the API tokens section of account settings (see the PyPI help page on API tokens).
  2. Choose the token scope - entire account, or one project.
  3. Create the token and copy it, including the pypi- prefix; it is shown once.

Limiting what the key can do

Scope a token to a single project wherever possible. Use an account-wide token only for the first upload of a new project, then switch to a project-scoped token. Where your CI provider supports it, use Trusted Publishing instead of storing a token.

How to rotate a PyPI key

  1. Create a new project-scoped token.
  2. Update the secret in your CI or .pypirc.
  3. Revoke the old token in your account settings.

How to revoke a PyPI key

  1. Open your PyPI account settings, find the token in the API tokens section and remove it.

If your PyPI key leaked

  1. Revoke the token in your PyPI account settings.
  2. Create a replacement scoped to one project, or move CI to Trusted Publishing.
  3. Check the project's release history for versions you did not upload.
  4. Note that GitHub and deps.dev report publicly exposed PyPI tokens to PyPI, and users are opted in by default.

Environment variables and CLI

PyPI's SDKs and docs read the key from TWINE_USERNAME, TWINE_PASSWORD or TWINE_REPOSITORY.

Twine (twine): Use the username __token__ and the API token as the password; keyring set https://upload.pypi.org/legacy/ __token__ can store it

Things that catch people out

  • The username is the literal string __token__, not your PyPI username, and the password includes the pypi- prefix.
  • A new project has no project-scoped token possible until it exists.
  • Token strings can be arbitrarily long because caveats may be added; do not assume a fixed length.
  • Old PYPI_API_TOKEN and TEST_PYPI_API_TOKEN secrets in guides are obsolete; revoke them if still present.

Recent incidents that put PyPI keys at risk

If you were affected by one of these, rotate your PyPI keys using the rotation steps above.

Using your PyPI 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 PyPI key, these intents fit:

IntentWhat BotPassword does
list_credentialsLists the credentials this agent is granted and what it may do with each. Never values.
request_accessAsks 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_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.
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.
capture_secretPlanned, not built yet: will read a newly created key straight from the page or command output into the vault.
store_secretSaves 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 get a PyPI API token?

Create one in the API tokens section of your PyPI account settings, choosing account-wide or single-project scope, and copy it when shown.

What username do I use with a PyPI API token?

Use __token__ as the username and the token, including the pypi- prefix, as the password.

Should I use an API token or Trusted Publishing?

PyPI and the Python Packaging guide recommend Trusted Publishing from CI, which mints project-scoped tokens valid for 15 minutes.

Do PyPI and TestPyPI use the same token?

No. You generate a separate API token for each of PyPI and TestPyPI.

Can an AI agent use my PyPI 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, store_secret, so the agent gets the result and never the value.

Stop pasting PyPI keys into agents

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