Safeguard
Supply Chain•

Should a PyPI API token be account-wide or project scoped?

Limit every PyPI upload token to a single project. See why account-wide tokens are risky in CI, and how this compares with trusted publishing.

Safeguard Security Team
3 min read

Project scoped. PyPI lets you create a token for an entire account or limit a token to a specific project, and its help page recommends keeping CI tokens to the minimum projects necessary. An account-wide token that leaks lets an attacker publish to every package you maintain.

The two scopes

ScopeWhat a leaked token can doWhen to use
Entire accountUpload to any project you can publishOnly to create the first project token
One projectUpload to that project onlyNormal CI publishing

A common pattern follows from this. Use the account-wide token once, to publish a new project for the first time, then create a project scoped token and delete the wide one.

Where tokens leak

  • CI logs and debug output
  • Shared environment variable files
  • Developer laptops and old shell history
  • Forks and pull request builds that expose secrets

PyPI notes that when a token linked to an account is posted on a public website, it is automatically revoked. Do not rely on that alone, because a token can leak somewhere that is not public.

A cleaner option: no long-lived token

Trusted publishing lets a CI system prove its identity and receive a short lived credential, so there is no secret to leak. If your CI supports it, it is the stronger choice. See PyPI trusted publisher setup with GitHub Actions. The Python Packaging User Guide recommends trusted publishing from supported platforms, with a manual upload tool as the fallback.

Steps to tighten what you have

  1. List every token on the account and where it is used.
  2. Delete any you cannot place.
  3. Replace account-wide tokens with one token per project.
  4. Store each in your CI secret store, scoped to the one repository and, where possible, one protected environment.
  5. Turn on two factor sign in for every maintainer.
  6. Switch to trusted publishing when you can, and delete the tokens afterward.

What to check after a suspected leak

Look at the release history of each project the token could reach. Any release you did not publish, or published at an odd hour, needs investigation. Compare file hashes with your build output. If you find a bad release, tell the PyPI administrators through the contact on the help page, and tell your users through your own channels with the affected version numbers.

Quick check

What if I think a token leaked?

Delete it in your account settings, create a scoped replacement, and check recent releases for anything you did not publish.

Is a project scoped token enough on its own?

It limits the damage but is still a long-lived secret. Trusted publishing avoids storing one.

Safeguard's secrets related policies and gates can block risky changes in a pull request, and a free scan is available.

Sources

Last reviewed: 19 October 2026.

Never miss an update

Weekly insights on software supply chain security, delivered to your inbox.

Self-healing security runs on Safeguard.

Your first fix PR is minutes away.

No sales call required, even your agent can complete the purchase over MCP.