DEV Community

Alex Spinov
Alex Spinov

Posted on

Hugging Face Has a Free Inference API — Here's How to Use 200K+ AI Models

Hugging Face offers a free Inference API that gives you access to 200,000+ models — text generation, image generation, classification, translation, embeddings, and more. No GPU needed.

Getting Your API Token

Sign up at huggingface.co → Settings → Access Tokens → Create new token.

Installation

npm install @huggingface/inference
Enter fullscreen mode Exit fullscreen mode

Text Generation

import { HfInference } from "@huggingface/inference";

const hf = new HfInference("your-token");

const result = await hf.textGeneration({
  model: "mistralai/Mistral-7B-Instruct-v0.3",
  inputs: "Explain web scraping in simple terms:",
  parameters: { max_new_tokens: 200, temperature: 0.7 }
});
console.log(result.generated_text);
Enter fullscreen mode Exit fullscreen mode

Chat Completion

const response = await hf.chatCompletion({
  model: "meta-llama/Llama-3.2-3B-Instruct",
  messages: [
    { role: "system", content: "You are a helpful coding assistant." },
    { role: "user", content: "Write a Python web scraper for news headlines" }
  ],
  max_tokens: 500
});
console.log(response.choices[0].message.content);
Enter fullscreen mode Exit fullscreen mode

Image Generation

const image = await hf.textToImage({
  model: "stabilityai/stable-diffusion-xl-base-1.0",
  inputs: "A robot scraping data from a website, digital art"
});
// image is a Blob
const buffer = Buffer.from(await image.arrayBuffer());
fs.writeFileSync("robot.png", buffer);
Enter fullscreen mode Exit fullscreen mode

Embeddings

const embeddings = await hf.featureExtraction({
  model: "sentence-transformers/all-MiniLM-L6-v2",
  inputs: "Web scraping best practices for developers"
});
console.log(`Dimensions: ${embeddings.length}`);
Enter fullscreen mode Exit fullscreen mode

Text Classification (Sentiment)

const result = await hf.textClassification({
  model: "distilbert-base-uncased-finetuned-sst-2-english",
  inputs: "This API is amazing and so easy to use!"
});
console.log(result); // [{ label: "POSITIVE", score: 0.99 }]
Enter fullscreen mode Exit fullscreen mode

Translation

const translated = await hf.translation({
  model: "Helsinki-NLP/opus-mt-en-de",
  inputs: "Web scraping is the process of extracting data from websites."
});
console.log(translated.translation_text);
Enter fullscreen mode Exit fullscreen mode

Streaming

const stream = hf.chatCompletionStream({
  model: "meta-llama/Llama-3.2-3B-Instruct",
  messages: [{ role: "user", content: "Write a haiku about data" }]
});

for await (const chunk of stream) {
  process.stdout.write(chunk.choices[0]?.delta?.content || "");
}
Enter fullscreen mode Exit fullscreen mode

Need to extract or automate web content at scale? Check out my web scraping tools on Apify — no coding required. Or email me at spinov001@gmail.com for custom solutions.

Top comments (0)