Hugging Face Inference API
api-inference.huggingface.co · AI
Run inference on 100,000+ open-source AI models — text generation, summarisation, translation, image classification, speech recognition, and more. Free tier with rate limits; $9/mo Pro for higher throughput.
Authentication
Bearer Token
Free API token from huggingface.co/settings/tokens. Pass as Authorization: Bearer YOUR_TOKEN.
Sample Requests
POST
Text generation
Generate text using Mistral 7B.
https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.2
Hover any highlighted part to learn what it does
Headers — extra info sent with the request
| Content-Type | application/json |
| Authorization | Bearer YOUR_TOKEN |
Request Body — data you're sending
{
"inputs": "What is the capital of France?"
}
curl -X POST "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.2" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
-H "Content-Type: application/json" \
-d '{"inputs":"What is the capital of France?"}'import requests
headers = {
"Content-Type": "application/json",
"Authorization": "Bearer YOUR_ACCESS_TOKEN"
}
data = {
"inputs": "What is the capital of France?"
}
response = requests.post(
"https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.2",
headers=headers,
json=data,
)
print(response.json())const url = 'https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.2';
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer YOUR_ACCESS_TOKEN'
},
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
"inputs": "What is the capital of France?"
}),
});
const data = await response.json();
console.log(data);package main
import (
"fmt"
"io"
"net/http"
"bytes"
"encoding/json"
)
func main() {
targetURL := "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.2"
jsonData, _ := json.Marshal({"inputs":"What is the capital of France?"})
req, _ := http.NewRequest("POST", targetURL, bytes.NewBuffer(jsonData))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer YOUR_ACCESS_TOKEN")
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, _ := client.Do(req)
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
fmt.Println(string(body))
}require "net/http"
require "json"
uri = URI("https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.2")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = uri.scheme == "https"
req = Net::HTTP::Post.new(uri)
req["Content-Type"] = "application/json"
req["Authorization"] = "Bearer YOUR_ACCESS_TOKEN"
req["Content-Type"] = "application/json"
req.body = "{\"inputs\":\"What is the capital of France?\"}"
res = http.request(req)
puts JSON.parse(res.body)<?php
$url = "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.2";
$opts = ["http" => [
"method" => "POST",
"header" => implode("\r\n", [
"Content-Type: application/json",
"Authorization: Bearer YOUR_ACCESS_TOKEN",
"Content-Type: application/json"
]),
"content" => json_encode({"inputs":"What is the capital of France?"}),
]];
$ctx = stream_context_create($opts);
$res = file_get_contents($url, false, $ctx);
print_r(json_decode($res, true));
POST
Sentiment analysis
Classify sentiment of a text.
https://api-inference.huggingface.co/models/distilbert-base-uncased-finetuned-sst-2-english
Hover any highlighted part to learn what it does
Headers — extra info sent with the request
| Content-Type | application/json |
| Authorization | Bearer YOUR_TOKEN |
Request Body — data you're sending
{
"inputs": "This product is amazing!"
}
curl -X POST "https://api-inference.huggingface.co/models/distilbert-base-uncased-finetuned-sst-2-english" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
-H "Content-Type: application/json" \
-d '{"inputs":"This product is amazing!"}'import requests
headers = {
"Content-Type": "application/json",
"Authorization": "Bearer YOUR_ACCESS_TOKEN"
}
data = {
"inputs": "This product is amazing!"
}
response = requests.post(
"https://api-inference.huggingface.co/models/distilbert-base-uncased-finetuned-sst-2-english",
headers=headers,
json=data,
)
print(response.json())const url = 'https://api-inference.huggingface.co/models/distilbert-base-uncased-finetuned-sst-2-english';
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer YOUR_ACCESS_TOKEN'
},
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
"inputs": "This product is amazing!"
}),
});
const data = await response.json();
console.log(data);package main
import (
"fmt"
"io"
"net/http"
"bytes"
"encoding/json"
)
func main() {
targetURL := "https://api-inference.huggingface.co/models/distilbert-base-uncased-finetuned-sst-2-english"
jsonData, _ := json.Marshal({"inputs":"This product is amazing!"})
req, _ := http.NewRequest("POST", targetURL, bytes.NewBuffer(jsonData))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer YOUR_ACCESS_TOKEN")
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, _ := client.Do(req)
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
fmt.Println(string(body))
}require "net/http"
require "json"
uri = URI("https://api-inference.huggingface.co/models/distilbert-base-uncased-finetuned-sst-2-english")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = uri.scheme == "https"
req = Net::HTTP::Post.new(uri)
req["Content-Type"] = "application/json"
req["Authorization"] = "Bearer YOUR_ACCESS_TOKEN"
req["Content-Type"] = "application/json"
req.body = "{\"inputs\":\"This product is amazing!\"}"
res = http.request(req)
puts JSON.parse(res.body)<?php
$url = "https://api-inference.huggingface.co/models/distilbert-base-uncased-finetuned-sst-2-english";
$opts = ["http" => [
"method" => "POST",
"header" => implode("\r\n", [
"Content-Type: application/json",
"Authorization: Bearer YOUR_ACCESS_TOKEN",
"Content-Type: application/json"
]),
"content" => json_encode({"inputs":"This product is amazing!"}),
]];
$ctx = stream_context_create($opts);
$res = file_get_contents($url, false, $ctx);
print_r(json_decode($res, true));Postman Setup Guide
- Create a free account at huggingface.co
- Go to Settings → Access Tokens → New Token (Read)
- Set Authorization: Bearer YOUR_TOKEN
- Replace the model name in the URL with any model from huggingface.co/models
- First request may take 20s to load model — retry if you get a 503