Getting started overview
To begin using the Hugging Face Inference API, developers must complete a series of steps: account registration on the Hugging Face Hub, generation of a personal API access token, and then making an authenticated request to a hosted model. The primary method for interacting with the Hugging Face API is via the huggingface_hub Python library or direct HTTP requests. This section provides a summarized workflow:
- Create a Hugging Face Hub account: This is the initial step to access all Hugging Face services, including the Inference API.
- Generate an API token: Access tokens are used to authenticate requests to the Inference API and other Hugging Face services.
- Install the Python client library: The
huggingface_hublibrary simplifies interaction with the Inference API. - Make your first API call: Execute a basic inference request to a public model, such as a text classification model.
The following table provides a quick reference for these initial steps:
| Step | What to Do | Where |
|---|---|---|
| 1. Account Creation | Register on the Hugging Face Hub. | Hugging Face account creation page |
| 2. API Token Generation | Create a new access token in your profile settings. | Hugging Face API tokens settings |
| 3. SDK Installation | Install the huggingface_hub Python library. |
Terminal/Command Prompt |
| 4. First Request | Send a text classification request. | Python environment |
Create an account and get keys
Access to the Hugging Face Inference API requires an account on the Hugging Face Hub and an associated API token. This token serves as the primary authentication mechanism for programmatic access.
1. Create a Hugging Face Hub account
Navigate to the Hugging Face account registration page and follow the prompts to create a new account. This process typically involves providing an email address, creating a username, and setting a password. Account creation is free and provides access to the Hugging Face ecosystem, including the ability to host models and datasets.
2. Generate an API access token
Once your account is active, you can generate an API token. This token is a string that authenticates your requests to the Hugging Face API. Treat your API token as sensitive information, similar to a password, and avoid exposing it in public repositories or client-side code.
- Log in to your Hugging Face account.
- Navigate to your Hugging Face API tokens settings page.
- Click the "New token" button.
- Provide a descriptive name for your token (e.g., "MyFirstAPIApp").
- Select the appropriate role for the token. For initial API calls, "read" access is sufficient for public models, but "write" or "admin" may be needed for specific tasks like uploading models or datasets.
- Click "Generate a token" to create and reveal your new API token. Copy this token immediately as it will not be shown again.
API Token Best Practices
- Environment Variables: Store your API token in an environment variable rather than hardcoding it directly into your application code. This practice enhances security and portability. For example, you might set
HF_TOKEN="hf_YOUR_TOKEN_HERE"in your shell environment. - Least Privilege: Create tokens with the minimum necessary permissions (e.g., "read" only if you only intend to download models or perform inference on public models).
- Revocation: Regularly review and revoke any tokens that are no longer in use or if you suspect they have been compromised.
Your first request
This section demonstrates how to make a basic text classification request using the Hugging Face Inference API with the Python client library. Ensure you have Python installed (Python downloads page) and your API token from the previous step.
1. Install the Hugging Face Hub library
Open your terminal or command prompt and execute the following command to install the required Python package:
pip install huggingface_hub
This command installs the huggingface_hub library, which provides convenient functions for interacting with the Hugging Face API.
2. Authenticate your session
Before making requests, authenticate your session using your API token. The recommended way is to log in through the library, which stores the token locally.
from huggingface_hub import login
# Replace 'hf_YOUR_TOKEN_HERE' with your actual token
login(token="hf_YOUR_TOKEN_HERE")
# Alternatively, if your token is in an environment variable (recommended):
# import os
# login(token=os.environ.get("HF_TOKEN"))
For persistent authentication, you can also run huggingface-cli login in your terminal and paste your token when prompted. This will store your token in a configuration file.
3. Make an inference request (text classification)
Now, you can send an inference request to a pre-trained model. We will use a public text classification model for this example.
from huggingface_hub import InferenceClient
# Initialize the InferenceClient. It will automatically use the logged-in token.
client = InferenceClient()
# Define the model to use (e.g., a sentiment analysis model)
model_id = "distilbert-base-uncased-finetuned-sst-2-english"
# Define the input text for inference
input_text = "I love using the Hugging Face API for my projects!"
# Perform text classification
try:
# For text classification, the 'text_classification' task is typically used
# The client's 'text_classification' method sends the request to the Inference API
response = client.text_classification(input_text, model=model_id)
print("Text Classification Result:")
for label_info in response:
print(f" Label: {label_info['label']}, Score: {label_info['score']:.4f}")
except Exception as e:
print(f"An error occurred during inference: {e}")
This code snippet initializes an InferenceClient, specifies a sentiment analysis model, and then sends a sentence for classification. The response will contain predicted labels and their associated confidence scores.
Expected Output
The output for the example above should resemble the following, indicating a positive sentiment:
Text Classification Result:
Label: POSITIVE, Score: 0.9998
Label: NEGATIVE, Score: 0.0002
Common next steps
After successfully making your first API call, consider these next steps to further explore the Hugging Face ecosystem:
- Explore other Inference API tasks: The Inference API supports various tasks beyond text classification, including text generation, summarization, translation, image classification, and more. Refer to the Hugging Face Inference API detailed parameters documentation for a complete list and specific parameters for each task.
- Discover models on the Hugging Face Hub: Browse thousands of pre-trained models on the Hugging Face Models page. You can filter by task, language, library, and more to find models suitable for your specific use case.
- Fine-tune a model: If a pre-trained model doesn't fully meet your needs, you can fine-tune it on your own dataset. Hugging Face provides tools and guides for this, often leveraging their
transformerslibrary. The Hugging Face Transformers training documentation offers detailed instructions. - Deploy models to Hugging Face Spaces: For demonstrating your models or creating interactive demos, Hugging Face Spaces allows you to host web applications powered by your models. Learn more about Hugging Face Spaces.
- Utilize Hugging Face Datasets: Explore the Hugging Face Datasets library to find, share, and load datasets for machine learning training and evaluation.
- Manage billing and usage: Monitor your API usage and manage your billing settings. The free tier has usage limits, and exceeding them may require upgrading to a paid plan. Details are available on the Hugging Face pricing page.
Troubleshooting the first call
When encountering issues with your first Hugging Face API request, consider the following common problems and solutions:
- Invalid API Token:
- Symptom: You receive an authentication error (e.g., HTTP 401 Unauthorized). If using the
huggingface_hublibrary, it might raise anHTTPErrorwith a 401 status. - Solution: Double-check that your API token is correctly copied and pasted. Ensure there are no leading or trailing spaces. Verify that the token has the necessary permissions (at least "read" access for public inference). Regenerate the token if uncertain.
- Symptom: You receive an authentication error (e.g., HTTP 401 Unauthorized). If using the
- Incorrect Model ID:
- Symptom: The API returns an error indicating the model was not found or is inaccessible (e.g., HTTP 404 Not Found, or a specific error message about the model not existing).
- Solution: Verify the exact spelling of the
model_id. Ensure the model is public or that your token has access to a private model. Browse the Hugging Face Hub models to confirm the model's existence and accessibility.
- Rate Limiting:
- Symptom: Requests start failing with an HTTP 429 Too Many Requests status.
- Solution: The free tier of the Inference API has usage limits. If you are making many rapid requests, you might hit these limits. Implement retries with exponential backoff. If sustained higher usage is needed, consider upgrading to a paid plan as outlined on the Hugging Face pricing page.
- Network Connectivity Issues:
- Symptom: Requests time out or fail with network-related errors.
- Solution: Check your internet connection. Ensure no firewalls or proxies are blocking outgoing connections to
api-inference.huggingface.co.
- Incorrect Task Parameters:
- Symptom: The API returns an error about invalid input data or parameters, even if the model ID is correct.
- Solution: Review the Hugging Face Inference API detailed parameters documentation for the specific task you are attempting (e.g., text generation, summarization). Ensure your input format and parameters match the expected schema for that task.
- Python Environment Issues:
- Symptom: The
huggingface_hublibrary is not found, or imports fail. - Solution: Ensure you have correctly installed the library using
pip install huggingface_hub. Verify that you are running your script with the same Python environment where the library was installed.
- Symptom: The