Run Stable Diffusion 3 with an API

Posted June 12, 2024 by

Stable Diffusion 3 is the latest text-to-image model from Stability . It has greatly improved performance in image quality, typography, complex prompt understanding, and resource-efficiency.

With Replicate, you can run Stable Diffusion 3 with one line of code.


Try Stable Diffusion 3 in our API playground

Before you dive in, try Stable Diffusion 3 in our API playground.

Try tweaking the prompt and see how Stable Diffusion 3 responds. Most models on Replicate have an interactive API playground like this, available on the model page: https://replicate.com/stability-ai/stable-diffusion-3

The API playground is a great way to get a feel for what a model can do, and provides copyable code snippets in a variety of languages to help you get started.

Running Stable Diffusion 3 with JavaScript

You can run Stable Diffusion 3 with our official JavaScript client:

Install Replicate's Node.js client library

npm install replicate

Set the REPLICATE_API_TOKEN environment variable

export REPLICATE_API_TOKEN=r8_9wm**********************************

(You can generate an API token in your account. Keep it to yourself.)

Import and set up the client

import Replicate from "replicate";
 
const replicate = new Replicate({
  auth: process.env.REPLICATE_API_TOKEN,
});

Run stability-ai/stable-diffusion-3 using Replicate's API. Check out the model's schema for an overview of inputs and outputs.

const input = {
  prompt: "a photo of vibrant artistic graffiti on a wall saying 'SD3 medium'"
};
 
const output = await replicate.run("stability-ai/stable-diffusion-3", { input });
console.log(output);

To learn more, take a look at the guide on getting started with Node.js.

Running Stable Diffusion 3 with Python

You can run Stable Diffusion 3 with our official Python client:

Install Replicate's Python client library

pip install replicate

Set the REPLICATE_API_TOKEN environment variable

export REPLICATE_API_TOKEN=r8_9wm**********************************

(You can generate an API token in your account. Keep it to yourself.)

Import the client

import replicate

Run stability-ai/stable-diffusion-3 using Replicate's API. Check out the model's schema for an overview of inputs and outputs.

output = replicate.run(
    "stability-ai/stable-diffusion-3",
    input={
        "prompt": "a photo of vibrant artistic graffiti on a wall saying 'SD3 medium'"
    }
)
print(output)

To learn more, take a look at the guide on getting started with Python.

Running Stable Diffusion 3 with cURL

Your can call the HTTP API directly with tools like cURL:

Set the REPLICATE_API_TOKEN environment variable

export REPLICATE_API_TOKEN=r8_9wm**********************************

(You can generate an API token in your account. Keep it to yourself.)

Run stability-ai/stable-diffusion-3 using Replicate's API. Check out the model's schema for an overview of inputs and outputs.

curl -s -X POST\
  -H "Authorization: Bearer $REPLICATE_API_TOKEN"\
  -H "Content-Type: application/json"\
  -H "Prefer: wait"\
  -d $'{
    "input": {
      "prompt": "a photo of vibrant artistic graffiti on a wall saying \'SD3 medium\'"
    }
  }'\
  https://api.replicate.com/v1/models/stability-ai/stable-diffusion-3/predictions

To learn more, take a look at Replicate's HTTP API reference docs.

You can also run Stable Diffusion 3 using other Replicate client libraries for Go, Swift, and others.

Keep up to speed

Happy hacking! 🦙