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

Source: https://botpassword.com/keys/hugging-face
Last checked: 2026-11-08

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.

## Key types

| Type | Prefix | Secret | Scope | Expiry |
|---|---|---|---|---|
| Fine-grained token | `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

1. Sign in and open **Settings → Access Tokens** (`huggingface.co/settings/tokens`).
2. Click **New token**.
3. Choose a role and a name, then create it and copy the value.

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

1. On the Access Tokens tab, click **Manage** next to the token and choose refresh.
2. Update the token in every app that uses it.

## How to revoke a Hugging Face key

1. Open the Access Tokens tab of your settings.
2. Click **Manage** and delete or refresh the token.

## If the key leaked

1. Delete or refresh the token from the [Access Tokens](https://huggingface.co/settings/tokens) tab of your settings.
2. 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.
3. Check the repositories the token could write to for changes you did not make.
4. For CI, switch to Trusted Publishers so no long-lived Hugging Face token is stored as a CI secret.
5. 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

- `HF_TOKEN`

## Gotchas

- `HUGGING_FACE_HUB_TOKEN` is deprecated; use `HF_TOKEN`.
- `HF_TOKEN` overrides a token stored on the machine.
- Organizations that require fine-grained tokens reject read/write tokens with a 403.

## Using this key with AI agents (BotPassword)

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

## FAQ

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

## Sources

- https://huggingface.co/docs/hub/security-tokens
- https://huggingface.co/docs/huggingface_hub/package_reference/environment_variables
- https://huggingface.co/docs/hub/hf-mcp-server
- https://docs.github.com/en/code-security/secret-scanning/introduction/supported-secret-scanning-patterns
