Run Stable Diffusion with an API

Posted August 29, 2022 by @zeke

The neat thing about Stable Diffusion being open source is that you can modify it and build things with it. Photoshop plugins, bots, animations, fixing human foibles, all sorts.

But, if you want to integrate it into an app or a project, you need to set it up with GPUs and put an API in front of it. And the GPUs are expensive so you don't want to leave them switched on all the time.

Replicate lets you run machine learning models from your own code without having to set up any infrastructure. In this post, we'll show you how to use it to run Stable Diffusion.

a screenshot of the stable-diffusion-bot generating a cute rabbit in Discord

Install the Python library

We maintain an open-source Python client for the API. Install it with pip:

pip install replicate

There's also a community-maintained Node.js/JavaScript library. See replicate-js on GitHub.


Sign up for Replicate, then you can find your API token on your account page. Set your token in an environment variable in your terminal shell:

export REPLICATE_API_TOKEN=<token>

You'll be able to use the API for free for a bit, but eventually we'll ask you to enter your credit card. We only charge you by the second for the amount of time your requests are running, so it usually works out much cheaper than running your own GPUs.

Run predictions

Create a file called and paste in the following content:

import replicate
import webbrowser

model = replicate.models.get("stability-ai/stable-diffusion")
output_url = model.predict(prompt="electric sheep, neon, synthwave")[0]

Then run the script from your terminal:


This will create a prediction using the API and open the generated image in your web browser. It might look something like this:

View your predictions

Whenever you run a model on Replicate, whether in the browser or with the API, the prediction is saved and associated with your user account. Visit your dashboard to view all your previous predictions.

Next steps

Join us in Discord to show us what you've made, or if you need any help. We're looking forward to seeing your creations. ✨