Table of contents
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.
Node.js 16 or greater: The simplest way to install Node.js is using the installer at nodejs.org.
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:
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:
To learn more about scaffolding new Node.js projects, check out the create-replicate documentation.
If you prefer to manually set up your Node.js project step by step, follow the instructions below.
Authenticate by setting your Replicate API token in an environment variable:
Use npm to install the Replicate JavaScript client:
Create a file called index.js
and add the following code:
Next, run your code from your terminal:
You should see output indicating the model is running and the image has been saved:
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.