Introduction
Replicate is a platform that lets you run machine learning models through a simple and scalable API. In 2026, it remains essential for developers who want to quickly integrate AI features without managing infrastructure. This tutorial shows you how to get started with Replicate in JavaScript, from installation to running your first model. You will learn how to handle inputs, outputs, and errors professionally.
Prerequisites
- Node.js 20+
- Free Replicate account (replicate.com)
- Basic knowledge of JavaScript and npm
Package Installation
npm install replicateThis command installs the official Replicate client for Node.js. It handles authentication and API calls in an optimized way.
Creating the Replicate Client
After installation, initialize the client with your personal API token. This token is available in your Replicate account settings.
Client Initialization
import Replicate from "replicate";
const replicate = new Replicate({
auth: process.env.REPLICATE_API_TOKEN,
});The client is configured with the token stored in an environment variable. This avoids exposing sensitive keys in the source code.
Running a Simple Model
Choose a public model like llava-13b for your first generation. Provide the inputs expected by the model.
Model Call
const output = await replicate.run(
"yorickvp/llava-13b:5b6c7f4e8f7e4f7c8b9a1d2e3f4a5b6c",
{
input: {
image: "https://example.com/photo.jpg",
prompt: "Décris cette image en français",
},
}
);
console.log(output);This call executes the model with an image and a prompt. The result is returned directly as an array or text depending on the model.
Output Handling
if (Array.isArray(output)) {
output.forEach((item) => console.log(item));
} else {
console.log(output);
}Replicate models often return an array. This simple check allows you to correctly display all results.
Error Handling
try {
const output = await replicate.run(...);
} catch (error) {
console.error("Erreur Replicate:", error);
}Always wrap calls in a try/catch block to handle network errors or quota limits.
Best Practices
- Always store the token in environment variables
- Check the model documentation before use
- Add timeouts for long requests
- Monitor your Replicate credit usage
- Use models optimized for your use case
Common Errors to Avoid
- Forgetting to set REPLICATE_API_TOKEN
- Using an outdated model identifier
- Ignoring input size limits
- Not handling long asynchronous responses
Going Further
Explore advanced models and custom deployments on learni-group.com/formations.