Integration with Hugging Face Inference API
Hugging Face hosts thousands of open models for NLP, vision, and other tasks. The Inference API (via Inference Providers) lets you call those models over HTTP. The @huggingface/inference package from huggingface.js is the Node.js client.
Prerequisites
- Hugging Face account
- Access token with permission to call Inference (create a token of type Read or Fine-grained with inference access)
- Node.js version 26
@huggingface/inferenceinstalled (npm i @huggingface/inference)
Some models (especially image generation) are routed through Inference Providers. Enable providers and billing in account settings when a model requires it.
Client setup
Pass your token to InferenceClient. Read it from the environment in production.
import { InferenceClient } from '@huggingface/inference';const client = new InferenceClient(process.env.HF_TOKEN);
Summarization
Shrink longer text into a short summary. Pick a model trained for summarization and respect its max input length.
const result = await client.summarization({model: 'facebook/bart-large-cnn',inputs: `The tower is 324 metres (1,063 ft) tall, about the same height as an 81-storey building, and the tallest structure in Paris. Its base is square, measuring 125 metres (410 ft) on each side. During its construction, the Eiffel Tower surpassed the Washington Monument to become the tallest man-made structure in the world, a title it held for 41 years until the Chrysler Building in New York City was finished in 1930. It was initially criticized by some artists and intellectuals, but it became a global cultural icon of France and one of the most recognizable structures in the world.`,parameters: {max_length: 80,},});console.log(result.summary_text);
Text classification
Assign labels with confidence scores. Common use case: sentiment analysis on short text.
const labels = await client.textClassification({model: 'distilbert-base-uncased-finetuned-sst-2-english',inputs: 'I really enjoyed this workshop.',});for (const { label, score } of labels) {console.log(label, score.toFixed(4));}
Text to image
Generate an image from a text prompt. The client returns a Blob; write it to disk or serve it from your app.
import { writeFileSync } from 'node:fs';const image = await client.textToImage({provider: 'hf-inference',model: 'black-forest-labs/FLUX.1-schnell',inputs: 'A watercolor painting of a fox in an autumn forest',parameters: {num_inference_steps: 4,},});const buffer = Buffer.from(await image.arrayBuffer());writeFileSync('output.png', buffer);
With provider: 'hf-inference', use a model from the hf-inference catalog for that task (for example black-forest-labs/FLUX.1-schnell). Older Hub repos such as stabilityai/stable-diffusion-2-1 may no longer exist or lack provider mapping.
Image models are often slower and may incur provider usage charges.
Demo
Runnable scripts for each task live in the huggingface-inference-api-demo folder. Get access via code demos.