Hub documentation
Trusted Publishers
Trusted Publishers
Push to the Hub from CI without storing an HF_TOKEN secret.
Your CI job proves its identity to Hugging Face using a short-lived OpenID Connect (OIDC) token from your CI provider, and gets back a short-lived Hugging Face token in exchange. No HF token to store as a secret or rotate.
| Personal Access Token | Trusted Publisher | |
|---|---|---|
| Lifetime | Until revoked | 1 hour |
| Storage | CI secret | Nothing to store |
| Rotation | Manual | Automatic, every run |
| If leaked | Valid until you revoke | At most ~1 hour, and scoped to one repo |
Trusted Publishers are the same idea as PyPI’s Trusted Publishers and npm’s Trusted Publishing.
Quick example: publish a model from GitHub Actions
You maintain acme/awesome-model on the HF Hub and want CI in an externally-hosted acme/awesome-model-training repository (on GitHub, GitLab, or any OIDC-compliant provider) to push new checkpoints — no HF_TOKEN secret.
1. Configure the trusted publisher on the Hub
On https://huggingface.co/acme/awesome-model/settings, open Trusted Publishers and add:
- Provider: GitHub Actions
- Claims (all must match for the exchange to succeed):
repository=acme/awesome-model-trainingbranch=mainworkflow=publish.yml
repositoryalone scopes the publisher to a GitHub repo. Addbranchand/orworkflowto also pin it to a branch or workflow file — recommended.
2. Add the workflow to your GitHub repo
.github/workflows/publish.yml:
name: Publish to Hugging Face
on:
push:
branches: [main]
workflow_dispatch:
jobs:
publish:
runs-on: ubuntu-latest
permissions:
id-token: write # required so the job can request an OIDC token
contents: read
steps:
- uses: actions/checkout@v4
- name: Install the hf CLI
run: |
curl -LsSf https://hf.co/cli/install.sh | bash
echo "$HOME/.local/bin" >> "$GITHUB_PATH"
- name: Upload checkpoint
env:
# The HF repo to publish to. For non-model repos, prefix accordingly:
# datasets/acme/awesome-dataset, spaces/acme/awesome-space, kernels/acme/awesome-kernel
HF_OIDC_RESOURCE: acme/awesome-model
run: hf upload acme/awesome-model ./checkpoint . --commit-message "Publish from ${GITHUB_SHA::7}"That’s it. On GitHub Actions, the hf CLI (huggingface_hub>=1.19.0) detects the provider, performs the exchange, and uses the resulting token automatically. You only set HF_OIDC_RESOURCE.
Publishing to several repos in one run (e.g. a model and a dataset)? Set
HF_OIDC_RESOURCEper step so each token is scoped to the repo that step pushes to.
Other CI providers
The hf CLI mints the ID token natively on GitHub Actions only for now. On GitLab, CircleCI, Bitbucket, or any other provider, mint the ID token yourself (see Supported CI providers) and pass it via HF_OIDC_ID_TOKEN — the CLI exchanges it directly:
# GitLab CI (.gitlab-ci.yml)
publish:
id_tokens:
HF_ID_TOKEN:
aud: https://huggingface.co
script:
- curl -LsSf https://hf.co/cli/install.sh | bash
- export PATH="$HOME/.local/bin:$PATH"
- HF_OIDC_ID_TOKEN="$HF_ID_TOKEN" HF_OIDC_RESOURCE="acme/awesome-model" hf upload acme/awesome-model ./checkpoint .Complete working examples:
- GitHub Actions —
github.com/coyotte508/publish-to-hf - GitLab CI —
gitlab.com/coyotte508/publish-to-hf
Three flavors: repo publishers, CI/CD identities, and service account publishers
| Flavor | Configured on | What you get | Use it to… |
|---|---|---|---|
| Repo trusted publisher | A repo’s Settings → Trusted Publishers | A token with write access to that one repo (or read-only access) | Publish a model, dataset, Space, kernel, or bucket from CI, or pull a private one |
| Account CI/CD identity | Your account’s Authentication settings → CI/CD Access | A read-only token with the gated-repos scope, plus optional capabilities | Read gated repos you have access to, use your rate limits, and optionally call Inference Providers or request access to gated repos from CI |
| Service account trusted publisher | An organization’s Settings → Service Accounts → service account → Trusted publishers | A token acting as the service account, with the gated-repos scope plus optional capabilities | Act as a service account from an organization workload, without storing a long-lived token |
All tokens expire after 60 minutes. You need the Write role on a Hub repo to manage its trusted publishers.
Accessing gated repos from CI
If you only need to read gated repos (e.g. download a model from a job), add a CI/CD identity on your account under Authentication settings → CI/CD Access instead of on a specific repo, then use your username as the resource.
On GitHub Actions, the hf CLI does the exchange for you, just set HF_OIDC_RESOURCE:
- name: Download a gated model
env:
HF_OIDC_RESOURCE: your-hf-username
run: hf download acme/gated-modelOn other providers, mint the ID token yourself (see Other CI providers) and pass it via HF_OIDC_ID_TOKEN:
# $ID_TOKEN is the OIDC token your provider minted (e.g. $HF_ID_TOKEN on GitLab)
HF_OIDC_ID_TOKEN="$ID_TOKEN" HF_OIDC_RESOURCE="your-hf-username" hf download acme/gated-modelThe resulting token can read gated repos you have access to and uses your account’s rate limits. It cannot write anything, and cannot read your private repos. To pull a private repo from CI, use a read-only repo publisher instead.
Need the token itself (for
curl,git clone, or a tool that readsHF_TOKEN)?hf auth tokenperforms the exchange and prints the short-lived token to stdout:- name: Get a short-lived HF token env: HF_OIDC_RESOURCE: your-hf-username # or a repo, e.g. acme/awesome-model run: echo "HF_TOKEN=$(hf auth token)" >> "$GITHUB_ENV"This works with both CI/CD identities and repo publishers — the token is scoped to whatever
HF_OIDC_RESOURCEpoints at.
Read-only repo publishers
By default, a repo trusted publisher mints tokens that can push to the repo. If a workload only needs to pull a repo (for example, to download a private model or dataset from a CI job or an external platform that issues its own OIDC tokens), tick Read-only when adding the publisher. Exchanged tokens can then read the repo’s content but cannot write to it, and the entry shows a Read-only badge in the list. This works for models, datasets, Spaces, kernels, and buckets.
The exchange itself is unchanged. Point HF_OIDC_RESOURCE (or resource) at the repo.
permissions:
id-token: write # required so the job can request an OIDC token
steps:
- name: Download a private model
env:
HF_OIDC_RESOURCE: acme/private-model
run: hf download acme/private-modelLike other publisher settings, the permission is fixed at creation. To change it, delete the publisher and add it again.
Optional capabilities for account tokens
Tokens from a CI/CD identity or a service account trusted publisher are read-only by default. When adding the identity or publisher, you can opt into extra capabilities:
- Allow Inference Providers calls — see Calling Inference Providers from CI.
- Allow requesting access to gated repos — exchanged tokens can submit access requests to gated repos on the account’s behalf, and the entry shows a Gated access requests badge in the list. Nothing is granted automatically: the gated repo’s owner still approves or rejects each request. Once a request is accepted, the token can read the repo.
Repo publishers aren’t eligible for these capabilities. Capabilities are fixed at creation; to change them, delete the identity or publisher and add it again.
Calling Inference Providers from CI
To let CI/CD identity tokens call Inference Providers, tick Allow Inference Providers calls when adding the identity — exchanged tokens then carry the inference-api scope in addition to gated-repos, and the entry shows an Inference badge in the list.
Inference usage is billed to your account, exactly as if you had made the requests yourself — so pin the claims tightly (repository and branch and/or workflow) before enabling it. For service account publishers, set X-HF-Bill-To to bill your organization (see Service account trusted publishers).
- name: Run inference from CI
env:
HF_OIDC_RESOURCE: your-hf-username
run: |
export HF_TOKEN="$(hf auth token)"
python inference_script.pyService account trusted publishers
Service accounts are part of the Enterprise and Enterprise Plus plans.
Organization admins can configure trusted publishers on a service account. Any workload whose platform issues OIDC tokens (GitHub Actions, GitLab CI, Kubernetes, cloud providers, …) can then exchange its ID token for a short-lived token acting as the service account — no long-lived service account token to store or rotate.
An exchanged token acts as the service account, within its scopes. Like any organization member, these tokens can read the organization’s gated repos without an access request. Since service accounts are organization admins, this also includes repos gated for organization members. Only add trusted publishers for workloads you trust with that access, and pin the claims tightly. For least privilege, use a repo trusted publisher instead.
- In your organization settings, open Service Accounts, select the service account, and open the Trusted publishers tab.
- Click Add trusted publisher, pick the provider (or enter a custom issuer URL), and set the claims that must match. Optionally enable extra capabilities.
- In your workload, exchange the ID token using the service account’s username as the resource. The tab shows the exact
resource=…value to use.
HF_OIDC_ID_TOKEN="$ID_TOKEN" HF_OIDC_RESOURCE="<service-account-username>" hf download acme/gated-modelOn GitHub Actions, setting HF_OIDC_RESOURCE is enough, as for CI/CD identities.
Things to know:
- Exchanged tokens are read-only and can’t access private repos, including your organization’s own. Use a service account token for those.
- Exchanged tokens can also read gated repos of other organizations once an access request for the service account is accepted, plus any optional capabilities enabled on the publisher.
- With Allow Inference Providers calls, requests are billed to the service account itself unless you bill them to your organization with the
X-HF-Bill-To: <org>header (bill_toin thehuggingface_hub/huggingface.jsclients). - Exchanged tokens are subject to your organization’s Network Security IP restrictions. Your organization’s SSO doesn’t apply to them.
- Removing a publisher stops new exchanges, but tokens already issued stay valid until they expire. Deleting the service account revokes its tokens immediately.
- The issuer must be reachable over public HTTPS so the Hub can fetch its OIDC discovery document. For self-hosted Kubernetes clusters, publish the cluster’s service account issuer discovery document and keys publicly.
- Tokens obtained through an exchange can’t add or remove trusted publishers.
You can also manage these publishers via the API, with GET and POST on /api/organizations/{org}/service-accounts/{serviceAccountId}/trusted-publishers, and DELETE on /api/organizations/{org}/service-accounts/{serviceAccountId}/trusted-publishers/{publisherId}.
Supported CI providers
The settings UI ships with presets for the providers below, but any OIDC-compliant provider works (AWS, GCP, Buildkite, your own IdP, …).
| Provider | Issuer | How to get the ID token |
|---|---|---|
| GitHub Actions | https://token.actions.githubusercontent.com | Set permissions: id-token: write, then call the metadata endpoint with audience=https://huggingface.co. Docs. |
| GitLab CI | https://gitlab.com (or your self-hosted URL) | Declare id_tokens: { HF_ID_TOKEN: { aud: https://huggingface.co } } on the job; read $HF_ID_TOKEN. Docs. |
| CircleCI | https://oidc.circleci.com/org/<org-uuid> | Use $CIRCLE_OIDC_TOKEN_V2 (v2 lets you set the audience in project settings). Docs. |
| Bitbucket Pipelines | https://api.bitbucket.org/2.0/workspaces/<workspace>/pipelines-config/identity/oidc | Set oidc: true on the step; read $BITBUCKET_STEP_OIDC_TOKEN. Docs. |
Once you have the ID token, the exchange call is identical across providers — only the claims you configure on the Hub side differ.
How it works
- Your CI provider mints a short-lived OIDC ID token describing the job (which repo, which branch, which workflow, …).
- Your workflow
POSTs that token tohttps://huggingface.co/oauth/token, along with aresource(the repo or username it wants to access). - The Hub checks the token’s signature and claims against the publishers you configured for that resource, and returns a Hugging Face token.
┌──────────┐ 1. mint ID token ┌──────────┐ 2. exchange ┌────────────┐
│ CI job │ ─────────────────▶ │ CI OIDC │ ──────────────▶│ huggingface│
│ │ │ issuer │ │ /oauth/ │
│ │ ◀──────────────────────────────────────────────│ token │
└──────────┘ 3. short-lived HF token (valid 1 h) └────────────┘API reference
Endpoint, request, and response
Endpoint: POST https://huggingface.co/oauth/token with Content-Type: application/json.
No client authentication is needed — the OIDC ID token authenticates the request. The exchange follows RFC 8693 — OAuth 2.0 Token Exchange.
Request body:
| Field | Required | Value |
|---|---|---|
grant_type | yes | urn:ietf:params:oauth:grant-type:token-exchange |
subject_token_type | yes | urn:ietf:params:oauth:token-type:id_token |
subject_token | yes | The raw OIDC ID token (JWT) from your CI provider. Its aud claim must be https://huggingface.co. |
resource | yes | A Hub repo (namespace/name, datasets/namespace/name, spaces/namespace/name, kernels/namespace/name, buckets/namespace/name) or a Hub username (no slash) for a user-scoped token. Use a service account’s username for a token acting as that service account. |
Success response:
{
"access_token": "hf_jwt_…", // "hf_oauth_…" for user resources
"token_type": "bearer",
"expires_in": 3600,
"issued_token_type": "urn:ietf:params:oauth:token-type:access_token"
}Errors — 400 Bad Request with an OAuth-style body:
error | Why |
|---|---|
invalid_request | Missing/malformed parameter, or bad resource format. |
invalid_grant | Repo or user not found; no publisher matches this issuer; configured claims don’t match; signature or audience check failed; account locked. |
When the hf CLI performs the exchange, a failure surfaces the error code along with a (Request ID: …) — include that Request ID when reporting an issue so we can trace the exchange in our logs.
Security model
- Tokens are short-lived. 60 minutes from the moment of exchange — the clock only starts when you call the endpoint, not when the workflow starts. There’s no refresh token; long jobs should re-exchange.
- Repo tokens are repo-scoped. A token for
acme/awesome-modelcannot touchacme/anything-else. Pushes are attributed to a synthetic[OIDC]system user, with a reference to the originating issuer and subject. Tokens from a read-only publisher can only read that repo. - User tokens are read-only.
gated-reposscope by default — no writes, no private repos, no account management. The optionalinference-apiscope adds Inference Providers calls (billed to your account), and the optionalrequest-gated-reposscope lets the token submit gated-repo access requests (each one still approved by the repo owner), but still no write or private-repo access. - Service account tokens follow the same model, except that, like any organization member, they read the organization’s gated repos without an access request (including repos gated for organization members, since the service account is an admin). They remain subject to the organization’s IP restrictions.
- Claims are matched exactly, or by prefix where a provider preset needs it (GitHub’s
workflow_ref). No regex. - Name claims are pinned to IDs on first use. The first successful exchange records the immutable ID behind each name claim (e.g.
repository→repository_id), and later exchanges must match it. Someone who re-creates a deleted repo or org under the same name can’t use your publisher. If you re-create your own CI repo, delete and re-add the publisher. - Audit logs. Adding or removing a publisher is logged, and successful exchanges update last used time.
See also
- User Access Tokens — the right choice for humans and one-off scripts
- Service Accounts — organization-owned identities for automation
- OAuth / Sign in with HF — the same
/oauth/tokenendpoint, used for interactive flows - Managing Spaces with GitHub Actions
- GitHub Actions integration for the Hub