Skip to content

How to Create a Hugging Face API Token | Complete Guide

🔑 How to Create a Hugging Face API Token

Section titled “🔑 How to Create a Hugging Face API Token”

Learn how to create and manage Hugging Face API tokens to access thousands of open-source AI models, datasets, and spaces on the Hugging Face Hub.


A Hugging Face API token (also called Access Token) allows you to:

  • Access and download models from Hugging Face Hub
  • Use the Inference API for model predictions
  • Upload and manage your own models and datasets
  • Authenticate with Hugging Face services
  • Access private or gated models and datasets

Before creating an API token, you need:

  1. Hugging Face Account → Sign up at https://huggingface.co/join
  2. Email Verification → Verify your email address
  3. Internet Connection → For API access

🔹 Step-by-Step: Creating a Hugging Face API Token

Section titled “🔹 Step-by-Step: Creating a Hugging Face API Token”
  1. Go to Hugging Face
  2. Click “Sign in” in the top-right corner
  3. Log in with your credentials
  1. Click on your profile picture (top-right corner)
  2. Select “Settings” from the dropdown menu
  3. Click on “Access Tokens” in the left sidebar

Or go directly to: https://huggingface.co/settings/tokens

  1. Click “New token” button

  2. Fill in the token details:

    • Name: Give it a descriptive name (e.g., “My Python App”, “Production API”)
    • Role: Choose the permission level:
      • Read → Only read access (recommended for most uses)
      • Write → Allows uploading models and datasets
      • Admin → Full access (use with caution)
  3. Click “Generate a token”

⚠️ IMPORTANT: Save your token immediately!

  1. Copy the token that appears
  2. Store it securely in a password manager or environment variable
  3. You can view it again later, but it’s best to save it now

Example token format:

hf_AbCdEfGhIjKlMnOpQrStUvWxYz1234567890

  • ✅ Download public models and datasets
  • ✅ Access Inference API
  • ✅ Use private models you have access to
  • ❌ Cannot upload or modify resources
  • ✅ All Read permissions
  • ✅ Upload models and datasets
  • ✅ Create and manage repositories
  • ✅ Push changes to your repos
  • ✅ All Write permissions
  • ✅ Delete repositories
  • ✅ Manage organization settings
  • ⚠️ Use with extreme caution

  1. Go to Access Tokens
  2. You’ll see all your tokens with:
    • Name
    • Role (Read/Write/Admin)
    • Created date
    • Last used timestamp
    • Actions (Rename, Revoke)
  1. Click the pencil icon next to the token
  2. Enter a new name
  3. Click “Save”
  1. Click the trash icon next to the token
  2. Confirm the deletion

⚠️ Warning: Revoking a token immediately stops all applications using it.


  • Use Read tokens by default (only use Write when necessary)
  • Store tokens in environment variables or secrets managers
  • Use different tokens for different projects
  • Revoke tokens you no longer need
  • Use .env files for local development
  • Rotate tokens periodically
  • Hardcode tokens in source code
  • Commit tokens to Git repositories
  • Share tokens publicly
  • Use Admin tokens unless absolutely necessary
  • Reuse the same token across all projects

Section titled “Method 1: Environment Variable (Recommended)”

Linux/macOS:

Terminal window
export HF_TOKEN="hf_your-token-here"
# or
export HUGGING_FACE_HUB_TOKEN="hf_your-token-here"

Windows CMD:

Terminal window
set HF_TOKEN="hf_your-token-here"

Windows PowerShell:

Terminal window
$env:HF_TOKEN="hf_your-token-here"

Create a .env file:

HF_TOKEN=hf_your-token-here

Load it in Python:

from dotenv import load_dotenv
import os
load_dotenv()
token = os.getenv("HF_TOKEN")
Terminal window
huggingface-cli login

This will prompt you to enter your token and save it locally.


from transformers import pipeline
import os
# Set your token
token = os.getenv("HF_TOKEN")
# Use a model with authentication
classifier = pipeline(
"sentiment-analysis",
model="distilbert-base-uncased-finetuned-sst-2-english",
use_auth_token=token
)
result = classifier("I love Hugging Face!")
print(result)
import requests
import os
API_URL = "https://api-inference.huggingface.co/models/gpt2"
headers = {"Authorization": f"Bearer {os.getenv('HF_TOKEN')}"}
def query(payload):
response = requests.post(API_URL, headers=headers, json=payload)
return response.json()
output = query({"inputs": "The answer to the universe is"})
print(output)
from huggingface_hub import hf_hub_download
import os
token = os.getenv("HF_TOKEN")
# Download a specific file from a model
file_path = hf_hub_download(
repo_id="bert-base-uncased",
filename="config.json",
use_auth_token=token
)
print(f"Downloaded to: {file_path}")
Terminal window
curl https://api-inference.huggingface.co/models/gpt2 \
-H "Authorization: Bearer $HF_TOKEN" \
-H "Content-Type: application/json" \
-d '{"inputs": "Hello, Hugging Face!"}'

🔹 Using Tokens with Hugging Face Libraries

Section titled “🔹 Using Tokens with Hugging Face Libraries”
Terminal window
# Core library
pip install huggingface_hub
# For model inference
pip install transformers torch
# For datasets
pip install datasets

Option 1: Pass token directly

from huggingface_hub import HfApi
api = HfApi(token="hf_your-token-here")

Option 2: Use environment variable

from huggingface_hub import HfApi
import os
api = HfApi(token=os.getenv("HF_TOKEN"))

Option 3: Use CLI login (token stored automatically)

from huggingface_hub import HfApi
api = HfApi() # Automatically uses saved token

If you have access to a private model:

from transformers import AutoModel
import os
model = AutoModel.from_pretrained(
"username/private-model",
use_auth_token=os.getenv("HF_TOKEN")
)

Some models require explicit access approval:

  1. Visit the model page (e.g., LLaMA models)
  2. Click “Request access”
  3. Fill out the form and wait for approval
  4. Once approved, use your token to access:
from transformers import AutoTokenizer
import os
tokenizer = AutoTokenizer.from_pretrained(
"meta-llama/Llama-2-7b-hf",
use_auth_token=os.getenv("HF_TOKEN")
)

Hugging Face provides a free Inference API with rate limits:

  • Requests per day: Limited (varies by model)
  • Rate limiting: May experience delays under heavy load
  • Higher rate limits
  • Priority access
  • Faster inference

Check pricing: https://huggingface.co/pricing


Solution:

  • Verify the token is copied correctly
  • Ensure the token hasn’t been revoked
  • Check for extra spaces or newlines

Issue 2: “Access Denied” (401/403 Error)

Section titled “Issue 2: “Access Denied” (401/403 Error)”

Solution:

  • Token may not have required permissions
  • Model may be private or gated
  • Request access to gated models if needed

Solution:

  • You’ve hit the free tier limit
  • Wait for the limit to reset
  • Consider upgrading to Hugging Face Pro

Solution:

  • Ensure environment variable is set correctly
  • Run huggingface-cli login to save token locally
  • Check .env file is being loaded

With a Write token, you can upload your own content:

from huggingface_hub import HfApi
import os
api = HfApi(token=os.getenv("HF_TOKEN"))
api.upload_file(
path_or_fileobj="path/to/model.bin",
path_in_repo="model.bin",
repo_id="username/my-model",
repo_type="model"
)
from huggingface_hub import create_repo
import os
create_repo(
repo_id="username/my-new-model",
token=os.getenv("HF_TOKEN"),
private=False
)

If you’re part of an organization:

  1. Go to Organization Settings
  2. Select your organization
  3. Navigate to “Access Tokens”
  4. Create organization-level tokens

Terminal window
huggingface-cli login
Terminal window
huggingface-cli whoami
Terminal window
huggingface-cli logout
Terminal window
huggingface-cli upload username/model-name ./model_folder


You now know how to: ✅ Create and manage Hugging Face API tokens
✅ Use different permission levels appropriately
✅ Authenticate with Hugging Face services
✅ Access models, datasets, and APIs
✅ Upload your own content
✅ Troubleshoot common issues

Start building with the world’s largest AI community! 🤗