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 nodejs.org.

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

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 replicate.run(model, { input });

console.log(output);
// ['https://replicate.com/api/models/stability-ai/stable-diffusion/files/50fcac81-865d-499e-81ac-49de0cb79264/out-0.png']

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: "https://replicate.delivery/pbxt/IJEPmgAlL2zNBNDoRRKFegTEcxnlRhoQxlNjPHSZEy0pSIKn/gg_bridge.jpeg",
  question: "what body of water does this bridge cross?"
};
const output = await replicate.run(model, { 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.