How to beginner · 3 min read

How to use Hugging Face dedicated endpoints

Quick answer
Use Hugging Face dedicated endpoints by sending HTTP POST requests to the model-specific URL with your API key in the Authorization header. Use Python's requests library or Hugging Face's huggingface_hub SDK to interact with these endpoints securely and efficiently.

PREREQUISITES

  • Python 3.8+
  • Hugging Face API key
  • pip install requests huggingface_hub

Setup

Install the required Python packages and set your Hugging Face API key as an environment variable for secure access.

bash
pip install requests huggingface_hub

Step by step

Use Python to call a Hugging Face dedicated endpoint by sending a POST request with your input data and API key.

python
import os
import requests

API_URL = "https://api-inference.huggingface.co/models/gpt2"
API_TOKEN = os.environ["HF_API_KEY"]

headers = {"Authorization": f"Bearer {API_TOKEN}"}

def query(payload):
    response = requests.post(API_URL, headers=headers, json=payload)
    response.raise_for_status()
    return response.json()

if __name__ == "__main__":
    data = {"inputs": "Can you explain how to use Hugging Face dedicated endpoints?"}
    output = query(data)
    print(output)
output
[{'generated_text': 'Can you explain how to use Hugging Face dedicated endpoints? Hugging Face provides dedicated endpoints for models that allow you to send requests directly to a hosted model for inference.'}]

Common variations

You can also use the huggingface_hub Python SDK for easier integration, or switch models by changing the endpoint URL.

python
from huggingface_hub import InferenceClient
import os

client = InferenceClient(token=os.environ["HF_API_KEY"])

response = client.text_generation(
    model="gpt2",
    inputs="Explain Hugging Face dedicated endpoints."
)
print(response)
output
[{'generated_text': 'Explain Hugging Face dedicated endpoints. Hugging Face offers dedicated endpoints to access models via API calls for inference.'}]

Troubleshooting

If you receive a 401 Unauthorized error, verify your API key is set correctly in the environment variable HF_API_KEY. For 503 errors, the model might be overloaded or unavailable; retry after some time.

Key Takeaways

  • Use the Hugging Face model-specific URL with your API key in the Authorization header for dedicated endpoints.
  • The requests library or huggingface_hub SDK are the recommended ways to interact with these endpoints in Python.
  • Set your API key securely in environment variables to avoid exposing credentials in code.
  • Switch models by changing the endpoint URL or model parameter in the SDK calls.
  • Handle common HTTP errors by checking API key validity and model availability.
Verified 2026-04 · gpt2
Verify ↗