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
| Type | Prefix | Secret? | Scope | Expiry |
|---|---|---|---|---|
| API token Project-scoped tokens can only be made once the project exists. Tokens are shown once. | pypi- | Yes | Entire account (all projects) or a single project | Long-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. | — | Yes | Behaves like a project-scoped API token | Valid for 15 minutes from creation |
How to create a PyPI key
- Sign in to your PyPI account and open the API tokens section of account settings (see the PyPI help page on API tokens).
- Choose the token scope - entire account, or one project.
- 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
- Create a new project-scoped token.
- Update the secret in your CI or
.pypirc. - Revoke the old token in your account settings.
How to revoke a PyPI key
- Open your PyPI account settings, find the token in the API tokens section and remove it.
If your PyPI key leaked
- Revoke the token in your PyPI account settings.
- Create a replacement scoped to one project, or move CI to Trusted Publishing.
- Check the project's release history for versions you did not upload.
- 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 thepypi-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_TOKENandTEST_PYPI_API_TOKENsecrets 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.
- GhostAction returns: fake security workflows pushed to 30,000+ repos to steal Actions secrets 2026-10-07
- 32 @redhat-cloud-services npm packages compromised after a VS Code extension hijacked a GitHub account 2026-06-01
- LiteLLM 1.82.7 and 1.82.8 on PyPI carried a credential stealer after CI release secrets leaked 2026-03-24
- Trivy release, trivy-action and setup-trivy tags hijacked to steal CI/CD secrets 2026-03-21
- GhostAction: malicious GitHub Actions workflows stole 3,325 CI secrets from 817 repositories 2025-09-05
- PyPI phishing via pypj.org captured maintainer logins and led to malicious num2words releases 2025-07-28
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:
| 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. |
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 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.