You can call open-source AI models without downloading every model or running your own GPU server. The Hugging Face Inference API gives you one path into hosted models, with code libraries (SDKs) for Python and JavaScript. Here is how to create a token, send a request, choose the right task, and get your integration ready for production.
Step 1: Create a Hugging Face Access Token
The first step is making a limited-access token for your app. A token lets the Hugging Face Inference API recognize your request and check what it is allowed to do.
Open your Hugging Face account settings, select Access Tokens, then choose New token. For a basic app, a read token can be enough if the model only needs read access. If you are using Inference Providers, create a fine-grained token instead, and turn on the permission to make calls to Inference Providers.
Give the token a clear name, such as support-bot-dev. Keep one token per app or environment. That way, you can delete a development token without breaking live traffic.
Copy the token once, then store it in an environment variable. Do not paste it into a source file, a notebook you plan to share, or a terminal command that your shell history will save. Our guide on AI media API security best practices covers the same habits for any API key.
export HF_TOKEN="hf_your_token_here"Python libraries read environment settings when they load. Set HF_TOKEN before importing huggingface_hub. On a deployment host, add the value through your secret manager instead of committing a local .env file.
If you use an automated build system (CI), use its secret store instead of keeping a long-lived token in your code. For local testing, a plain environment variable is enough. You can also log in with the Hugging Face command-line tool, but your app itself should still read its secret from the runtime environment, not from a saved login session.
Tip: Create separate tokens for local development, staging, and production. That way you only need to replace the one token tied to the environment that needs a change.
Milestone: At this point, you should have an account, a limited-access token, and a runtime that can read HF_TOKEN without ever printing or exposing its value.
Step 2: Send Your First Inference Request
Now send a small test request through the Hugging Face Inference API. Start with a single text model, since a short prompt makes any mistakes easy to spot.
Install the Python client:
pip install -U huggingface_hubThen use the Python client and pass your model ID. Use the exact repository name shown on the model page. The model must have an available inference provider if you're using serverless routing.
import os
# Send a request with HF_TOKEN and your model ID using the installed Python client.Replace the model name with one that supports chat completion. The model page and the Inference Playground are useful for checking the task before you write code. Start with a low token limit, then raise it after the request works.
The client can choose a provider for you. By default, it aims for the fastest available provider (the same as adding :fastest). You can add :cheapest to the model ID when price matters, :preferred to follow your saved provider order, or name a specific provider directly, like openai/gpt-oss-120b:groq.
If you already use another Python client, an OpenAI-compatible chat interface can reduce migration work. Change the base address and API key, then keep your existing message format. That compatibility currently applies to chat only. For image generation, embeddings, or speech, use the Hugging Face clients built for those tasks instead.
You can also call the service with JavaScript or curl. Each model page can generate starter code for Python, JavaScript, and curl, which is useful when your app does not use the native SDK.
curl https://api-inference.huggingface.co/models/your-model-id \ -X POST \ -H "Authorization: Bearer $HF_TOKEN" \ -H "Content-Type: application/json" \ -d '{"inputs":"Explain embeddings in two sentences."}'For image or audio tasks, send the file as binary data with the correct file type. Do not reuse a text-based request for an image endpoint. The exact shape of the request depends on the task, so it is safest to copy the example generated for your specific model and change one field at a time from there.
At Apiframe, we take a similar approach to model switching, but for media generation specifically, not general-purpose models. Our guide on how to choose an AI media API is worth a look if your project needs one integration across several media providers, instead of working directly with individual Hub models.
Milestone: By now you should have one successful request, a working model ID, and a clear sense of which request format your task needs.
Step 3: Choose a Model, Task, and Deployment Option
Picking a model is only half the decision. With the Hugging Face Inference API, you also need to match it to the right task and decide whether serverless access is enough for what you are building.
Start on the Hub’s model page. Filter for models with inference available, then check the task tag and input format. Common tasks include:
- Chat completion: generate a response from a list of messages.
- Text generation: continue or write text from a prompt.
- Embeddings: turn text into numbers (vectors) for search or retrieval.
- Text to image: generate an image from a written prompt.
- Text to video: generate video from a written prompt.
- Speech to text: write out what is said in an audio file.
- Computer vision: sort images into groups, find objects, or answer questions about an image.
Do not choose a model by its name alone. Check its license, input limits, supported languages, and any task-specific notes. Test the same prompt against two or three candidates, keeping the prompt fixed so you are comparing the models themselves and not your own prompt changes.
| Decision | Use this path | Watch for |
|---|---|---|
| Quick proof of concept | Serverless Inference Providers | Provider availability and request limits can vary. |
| Known chat model with minimal code change | Inference Endpoints | Managed service to deploy AI models and eliminate infrastructure complexity. |
| Steady production traffic | Hugging Face Enterprise | Advanced tools for deploying and managing state-of-the-art machine learning models. |
| Custom preprocessing or output logic | Hugging Face TGI | Built-in support for buffering, quantization, token streaming, and telemetry. |
| Several image, video, or music providers | Apiframe unified API | Confirm the needed model and media settings in the catalog. |
A managed deployment (Inference Endpoints) makes more sense once you need a fixed web address, more control over the exact model version running, or something that can scale up automatically. A custom handler lets you add steps before and after the model runs, extra software packages, or logging. Put that handler in the model’s repository and test it thoroughly before sending real traffic through it.
For a media-focused product, Apiframe offers a different path. Its API covers image, video, and music generation through a single developer-facing interface. Our AI video generation API guide shows what that looks like for video, and you can let your app switch between models by changing one value in the request. If you want to compare hosted-model platforms, our Replicate API guide covers another popular option.
That distinction is worth keeping in mind. Hugging Face is a strong choice when you specifically want open-source models or a hosted model from the Hub. A unified media API can save you integration work when a single product needs image, video, and music generation working together. Our guide on what a unified AI media API is explains the difference in more detail.
Decision rule: Use serverless access while you are still learning and testing. Move to a managed deployment once traffic, slow starts, or custom code make shared routing hard to control.
Step 4: Estimate Costs and Prepare for Production
Before putting the Hugging Face Inference API behind a user-facing feature, measure both the cost per request and how the service behaves when something fails. Free testing can easily hide what repeated production calls will actually cost.
Inference Providers uses pay-as-you-go billing, and Hugging Face states that it passes through the underlying provider’s rate without adding its own markup. Worth knowing up front: the free monthly credit is small, currently $0.10 for free accounts, and Hugging Face says this amount is subject to change. That is enough for a little testing on a cheap model, but not much more. Free accounts cannot continue past that credit with pay-as-you-go. The PRO plan ($9 a month) includes $2 of credit and can keep going with pay-as-you-go after that. Team and Enterprise accounts share a larger credit pool and can bill usage to the organization. Our pricing calculator guide for unified AI media generation walks through a similar cost-planning exercise that you can adapt.
Make a small cost sheet with these fields:
- Model ID and selected provider
- Requests per user action
- Average input and output size
- Expected daily requests
- Retry count under failure
- Idle time for a dedicated endpoint
Rate limits are a separate concern from cost. The Hub returns an HTTP 429 error when a request goes over a limit. These limits work on time windows, and how much you are allowed depends on your account or organization tier. Hugging Face documents the relevant response headers and reset timing in its rate-limit reference. If your monthly credits run out, you may also see a 402 error, so plan a clear message for users when that happens.
Use your SDK’s built-in retry support where it is available. For your own request code, only retry failures that are genuinely temporary. Read the reset information when it is provided, wait before retrying, and set a cap so one slow provider does not tie up every worker in your app.
Your production logs should capture the model ID, provider chosen, request ID, status code, duration, token usage when it is returned, and a redacted version of any error. Never log the full prompt if it might contain personal information, and keep your secrets out of the logs entirely.
Test the failure paths too. Send an invalid model ID in staging. Try a request with a missing token. Force a timeout in your client. Your app should return a clear, useful error message to the user instead of showing a raw error trace.
If your app is mostly focused on images, it is worth comparing free-credit limits and wait times against our guide to free AI image generation APIs. If you need several types of media, check whether a single API can cut down the number of billing dashboards and retry systems your team has to maintain. Our guide to unified AI media API pricing shows how that math works.
Milestone: You should now have a cost model, a retry policy, a plan for handling secrets, and logs that can explain a failed request without exposing any user data.
Milestone: You should now have a cost model, a retry policy, a plan for handling secrets, and logs that can explain a failed request without exposing any user data.
FAQ
What is the Hugging Face Inference API?
The Hugging Face Inference API is a hosted way to send input to machine learning models over the web or through code libraries, without downloading each model or running your own servers. Depending on the task, you can call chat, text, embedding, image, video, speech, or vision models.
How do I get a Hugging Face API token?
You get a token from your Hugging Face account settings under Access Tokens. Create a read token for basic access, or a fine-grained token with Inference Providers permission if you need routed inference calls. Store it in HF_TOKEN and keep it out of your source code, any browser-facing code, and your logs.
Can I use Hugging Face inference for free?
You can test it using a small monthly credit, currently $0.10 for free accounts. That covers limited testing, and once it runs out, free accounts cannot keep going with pay-as-you-go. PRO adds $2 a month for $9 and can continue with pay-as-you-go. For steady, ongoing traffic, check the current billing terms, estimate how retries affect your usage, and set a spending limit before building it into a user-facing feature.
What languages work with the Hugging Face Inference API?
It works with Python, JavaScript, curl, and other HTTP clients generally. The Python and JavaScript SDKs handle the common task formats for you, while raw HTTP gives you more manual control. Start with the generated code for your specific model, then adapt it to your app once you have confirmed the request and response format.
Should I use Inference Providers or Inference Endpoints?
Use Inference Providers for quick testing and workloads where shared routing is fine. Choose Inference Endpoints when you want a managed service that removes most of the infrastructure work. If your product needs image, video, and music generation behind one API, it is also worth comparing Apiframe against model-specific hosting like this. Our Getting Started with Apiframe guide shows how to try it.
Conclusion
Start with a fine-grained token, one small request, and a model that clearly supports your task. Add retries, cost tracking, and proper secret handling before you launch. If your plans include several types of AI media, take a look at our AI inference platforms guide and our roundup of AI media generation APIs to compare a unified API approach against hosting separate models yourself.