Hugging Face User Access Tokens are created at huggingface.co/settings/tokens and come as fine-grained, read or write tokens. The huggingface_hub library reads the token from the HF_TOKEN environment variable; Hugging Face recommends fine-grained tokens for production.
Hugging Face key types
| Type | Prefix | Secret? | Scope | Expiry |
|---|---|---|---|---|
| Fine-grained token Recommended for production because the impact of a leak is smaller. | hf_ | Yes | Specific resources, such as chosen models or organizations | — |
| Read token | hf_ | Yes | Read access to repositories you can read | — |
| Write token | hf_ | Yes | Read plus write access to repositories you can write to | — |
How to create a Hugging Face key
- Sign in and open Settings → Access Tokens (
huggingface.co/settings/tokens). - Click New token.
- Choose a role and a name, then create it and copy the value.
Open Hugging Face's key settings
Limiting what the key can do
Use a fine-grained token limited to the models or organizations an app needs. Create one token per app or usage so you can invalidate one without affecting others. For CI/CD, Hugging Face's Trusted Publishers exchange your CI provider's OIDC identity for a short-lived Hub token instead of storing a secret.
How to rotate a Hugging Face key
- On the Access Tokens tab, click Manage next to the token and choose refresh.
- Update the token in every app that uses it.
How to revoke a Hugging Face key
- Open the Access Tokens tab of your settings.
- Click Manage and delete or refresh the token.
If your Hugging Face key leaked
- Delete or refresh the token from the Access Tokens tab of your settings.
- Create a replacement, preferably a fine-grained token limited to the repositories the app needs, and replace it in every app that used the old one.
- Check the repositories the token could write to for changes you did not make.
- For CI, switch to Trusted Publishers so no long-lived Hugging Face token is stored as a CI secret.
- If you found someone else's leaked token, invalidate it with
POST https://huggingface.co/api/credentials/revoke; the owner is notified by email. Hugging Face is also a GitHub secret scanning partner for user access tokens and organization API tokens.
Environment variables and CLI
Hugging Face's SDKs and docs read the key from HF_TOKEN.
Hugging Face CLI (hf): Stores the token in $HF_HOME/token by default; HF_TOKEN overrides the stored token
Things that catch people out
HUGGING_FACE_HUB_TOKENis deprecated; useHF_TOKEN.HF_TOKENoverrides a token stored on the machine.- Organizations that require fine-grained tokens reject read/write tokens with a 403.
Common Hugging Face authentication errors
Your token has been revoked by the organization administrator, you can no longer access organization resources. Please contact them for more information.
An Enterprise organization administrator revoked the token; HTTP 403.
Recent incidents that put Hugging Face keys at risk
If you were affected by one of these, rotate your Hugging Face keys using the rotation steps above.
- VS Code and Open VSX extensions leaked 550+ secrets, including tokens that could publish updates 2025-10-15
Using your Hugging Face 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 Hugging Face key, these intents fit:
| Intent | What BotPassword does |
|---|---|
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. |
store_secret | Saves a value the agent already holds as a new vault item. |
capture_secret | Planned, not built yet: will read a newly created key straight from the page or command output into the vault. |
Hugging Face also publishes its own MCP server. Whatever server the agent uses, keep the credential it authenticates with out of the agent's context.
Set it up in your agent: Aider · Amp · Claude Code · Claude Desktop · Cline · OpenAI Codex CLI · all agents
Frequently asked questions
Where do I get a Hugging Face token?
Go to huggingface.co/settings/tokens and click New token.
What is the Hugging Face token environment variable?
HF_TOKEN. The older HUGGING_FACE_HUB_TOKEN is deprecated.
How do I revoke a leaked Hugging Face token?
Delete or refresh it from the Access Tokens tab of your settings.
Which Hugging Face token type should I use in production?
Hugging Face recommends fine-grained tokens for production, scoped to the specific models or organizations the app needs, so a leak exposes less.
Can I revoke a Hugging Face token I found that is not mine?
Yes. Send it to POST https://huggingface.co/api/credentials/revoke; no rights over the owning account are needed. The endpoint always answers 202 Accepted, and the owner is notified by email.
Can an AI agent use my Hugging Face 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 run_with, http_request, store_secret, so the agent gets the result and never the value.