Run a model from Node.js

Learn how to run a model on Replicate from within your Node.js code.

Install Node.js

To get started, you’ll need Node.js 18 or later. The simplest way to install it is using the installer at

Create a project

Run the following command to create a new directory with a package.json file an index.js file for your code:

mkdir replicate-nodejs
cd replicate-nodejs
echo "{\"type\": \"module\"}" > package.json
touch index.js

Install the replicate npm package

We maintain an open-source JavaScript client for the API. Install it with npm:

npm install replicate

This will add the replicate package to your package.json file as a dependency.


Authenticate by setting your token in an environment variable:

Run predictions

You can run any public model on Replicate from your JavaScript code. The following example runs Stable Diffusion.

Paste the following code into index.js:

import Replicate from "replicate";

const replicate = new Replicate({
  auth: process.env.REPLICATE_API_TOKEN,

const model = "stability-ai/stable-diffusion:db21e45d3f7023abc2a46ee38a23973f6dce16bb082a930b0c49861f96d1e5bf";
const input = { prompt: "an astronaut riding a horse on mars, hd, dramatic lighting, detailed" };
const output = await, { input });

// ['']

Run the code with:

node index.js

Some models, like BLIP 2 in the following example, receive images as inputs. To pass a file as an input, use an HTTPS URL:

const model = "andreasjansson/blip-2:4b32258c42e9efd4288bb9910bc532a69727f9acd26aa08e175713a0a857a608";
const input = {
  image: "",
  question: "what body of water does this bridge cross?"
const output = await, { input });

// Output:
// san francisco bay

Next steps

Check our our guide to building a website with Next.js to learn how to build an app that runs models on Replicate, and deploy it to Vercel.

Read the full JavaScript client documentation on GitHub.

You can also run models with the raw HTTP API. Refer to the HTTP API reference for more details.