Happy Horse 1.0 is now on ModelsLab

Try Now
Skip to main content

How to Use the Stable Diffusion API with Python: A Complete Developer Guide 2026

||8 min read|Stable Diffusion API
How to Use the Stable Diffusion API with Python: A Complete Developer Guide 2026

Start Building with ModelsLab APIs

One API key. 100,000+ models. Image, video, audio, and LLM generation.

99.9% UptimePay-as-you-goFree tier available
Get Started

What Is the Stable Diffusion API?

The Stable Diffusion API is a cloud-hosted REST endpoint that gives you programmatic access to Stable Diffusion image generation — without renting a GPU, managing CUDA drivers, or downloading 6GB model weights. You send a text prompt, the API returns an image URL in a few seconds.

ModelsLab's API runs 600+ models, including:

  • SD 1.5, SDXL, SD 3 — the major release versions
  • Fine-tuned community models like Realistic Vision, DreamShaper, Anything-v5
  • Custom LoRA weights you upload yourself
  • ControlNet and Inpainting endpoints for advanced workflows

The difference between running this locally versus calling an API is stark. Locally, you're managing conda environments, VAE files, and CUDA out-of-memory errors. With the API, it's a single POST request.

Stable Diffusion API output examples

Getting Started: API Key and Setup

You need two things: a ModelsLab account and an API key.

  1. Sign up at modelslab.com/register
  2. Go to Dashboard → API Keys → Create new key
  3. Copy the key — you won't see it again

Store it in an environment variable, never hardcode it:

# Bad — don't do this
API_KEY = "mlb_abc123..."
,[object Object],
import os
API_KEY = os.environ.get("MODELSLAB_API_KEY")
if not API_KEY:
raise ValueError("MODELSLAB_API_KEY environment variable not set")

Install the only dependency you need:

pip install requests

Text-to-Image Generation with Python

The core operation. Here is a complete, working example:

import requests
import os
from PIL import Image
from io import BytesIO
,[object Object],
,[object Object],
,[object Object],
,[object Object],
,[object Object],[object Object],
,[object Object],
,[object Object],
,[object Object]
Share:
Plugins

Explore Plugins for Pro

Our plugins are designed to work with the most popular content creation software.

API

Build Apps with
ML
API

Use our API to build apps, generate AI art, create videos, and produce audio with ease.