Build a website with Next.jsBuild a Discord bot with PythonBuild an app with SwiftUIPush your own modelPush a Diffusers modelPush a Transformers modelPush a model using GitHub ActionsDeploy a custom modelFine-tune a language modelGet a GPU machine
Home / Get started

Run a model from Node.js


Learn how to run a model on Replicate using Node.js.

This guide includes a quickstart to scaffold a new project with a single command in your terminal, followed by a step-by-step tutorial for setting up a project from scratch. By the end, you'll have a working Node.js project that can run any model on Replicate.

Prerequisites

Node.js 16 or greater: The simplest way to install Node.js is using the installer at nodejs.org.

🐇 Quickstart: Scaffold a project with a one-liner

To get up and running as quickly as possible, you can use create-replicate, an npm package that creates a project directory for you, writes some starter code, installs the dependencies, and runs the code.

Run the following command to scaffold a new project:

npx create-replicate

That's it. You should now have a working Node.js project that generates images with the SDXL model using Replicate's API.

If you want to use a different model than SDXL, specify it when creating your project:

npx create-replicate --model black-forest-labs/flux-schnell

To learn more about scaffolding new Node.js projects, check out the create-replicate documentation.

🐢 Slowstart: Set up a project from scratch

If you prefer to manually set up your Node.js project step by step, follow the instructions below.

Step 1: Authenticate

Authenticate by setting your Replicate API token in an environment variable:

export REPLICATE_API_TOKEN=r8_******

Step 2: Create a new Node.js project

# create the directory
mkdir my-replicate-app
cd my-replicate-app
 
# set up package.json
npm init -y
npm pkg set type=module

Step 3: Install the Replicate JavaScript client

Use npm to install the Replicate JavaScript client:

npm install replicate

Step 4: Write some code

Create a file called index.js and add the following code:

import Replicate from "replicate";
const replicate = new Replicate();
 
console.log("Running the model...");
const [output] = await replicate.run(
  "black-forest-labs/flux-schnell",
  {
    input: {
      prompt: "An astronaut riding a rainbow unicorn, cinematic, dramatic",
    },
  }
);
 
// Save the generated image
import { writeFile } from "node:fs/promises";
 
await writeFile("./output.png", output);
console.log("Image saved as output.png");

Step 5: Run your code

Next, run your code from your terminal:

node index.js

You should see output indicating the model is running and the image has been saved:

Running the model...
Image saved as output.png

Next steps

Now you're up and running on Replicate with Node.js. 🚀🐢

In this guide you used the SDXL image generation model, but you can adapt the code to run any model on Replicate. Try chatting with images using the LLaVa vision model, or writing AI-generated Python code using CodeLlama. There are thousands of public models on Replicate, and you can run any of them using the project you just created.

Further reading