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

Source: https://botpassword.com/keys/pypi
Last checked: 2026-10-25

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.

## Key types

| Type | Prefix | Secret | Scope | Expiry |
|---|---|---|---|---|
| API token | `pypi-` | Yes | Entire account (all projects) or a single project | Long-lived; no expiry is documented in the pages used |
| Trusted Publishing token (minted) | — | Yes | Behaves like a project-scoped API token | Valid 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 the 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

- `TWINE_USERNAME`
- `TWINE_PASSWORD`
- `TWINE_REPOSITORY`

## Gotchas

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

## Using this key with AI agents (BotPassword)

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

## FAQ

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

## Sources

- https://docs.pypi.org/api/secrets/
- https://docs.pypi.org/trusted-publishers/
- https://packaging.python.org/en/latest/guides/publishing-package-distribution-releases-using-github-actions-ci-cd-workflows/
- https://twine.readthedocs.io/en/stable/
