Create & Edit Images Instantly with Grok Imagine

Try Grok Imagine
Skip to main content

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

Adhik JoshiAdhik Joshi
||9 min read|Stable Diffusion API
How to Use the Stable Diffusion API with Python: A Complete Developer Guide

Integrate AI APIs Today

Build next-generation applications with ModelsLab's enterprise-grade AI APIs for image, video, audio, and chat generation

Get Started
Get Started

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

Stable Diffusion is one of the most powerful open-source AI image generation models available. While running it locally requires significant GPU resources, the Stable Diffusion API from ModelsLab lets you generate stunning images in seconds — with just a few lines of Python code and no hardware setup required.

In this tutorial, you'll learn how to integrate the ModelsLab Stable Diffusion API into your Python applications, understand all key parameters, and build real-world image generation workflows.

What Is the Stable Diffusion API?

The Stable Diffusion API is a cloud-hosted REST API that gives developers programmatic access to Stable Diffusion image generation models — including SD 1.5, SDXL, SD 3, and thousands of fine-tuned community models — without managing any infrastructure.

ModelsLab's Stable Diffusion API hosts over 600 AI models and supports:

  • Text-to-Image: Generate images from text prompts
  • Image-to-Image: Transform existing images with AI
  • Inpainting: Edit specific parts of an image
  • ControlNet: Precise control over image composition
  • Custom LoRA Models: Fine-tuned models for specific styles
  • SDXL & SD 3: Latest generation high-resolution models

Unlike running Stable Diffusion locally (which requires an NVIDIA GPU with 8GB+ VRAM), the API handles all compute on ModelsLab's infrastructure, returning image URLs in 2–8 seconds.

Getting Started: API Key & Setup

Before writing any code, you'll need a ModelsLab API key.

  1. Sign up at modelslab.com
  2. Navigate to your dashboard → API Keys
  3. Create a new API key and copy it

Install the requests library if you haven't already:

pip install requests pillow

Store your API key securely (never hardcode it in production):

import os
API_KEY = os.environ.get("MODELSLAB_API_KEY", "your_api_key_here")

Text-to-Image Generation with Python

The core use case: generating an image from a text prompt. Here's a complete working example using the ModelsLab Stable Diffusion API:

import requests
import json
import os
from PIL import Image
from io import BytesIO

API_KEY = os.environ.get("MODELSLAB_API_KEY")

def generate_image(prompt, negative_prompt="", width=512, height=512, steps=30):
    """Generate an image using the Stable Diffusion API."""
    
    url = "https://modelslab.com/api/v6/images/text2img"
    
    payload = {
        "key": API_KEY,
        "model_id": "sdxl",            # Model to use
        "prompt": prompt,               # Your text prompt
        "negative_prompt": negative_prompt,
        "width": str(width),
        "height": str(height),
        "samples": "1",                # Number of images to generate
        "num_inference_steps": str(steps),
        "safety_checker": "no",        # Optional: skip NSFW filter
        "enhance_prompt": "yes",       # Optional: AI prompt enhancement
        "seed": None,                  # None for random seed
        "guidance_scale": 7.5,         # How closely to follow prompt (1-20)
        "webhook": None,               # Optional: async webhook URL
        "track_id": None               # Optional: request tracking ID
    }
    
    headers = {"Content-Type": "application/json"}
    
    response = requests.post(url, headers=headers, json=payload)
    result = response.json()
    
    if result.get("status") == "success":
        image_url = result["output"][0]
        print(f"✅ Image generated: {image_url}")
        return image_url
    elif result.get("status") == "processing":
        # Image is generating — fetch it later
        fetch_url = result.get("fetch_result")
        print(f"⏳ Processing... Fetch from: {fetch_url}")
        return fetch_url
    else:
        print(f"❌ Error: {result}")
        return None


# Example usage
image_url = generate_image(
    prompt="a futuristic cityscape at sunset, cyberpunk style, ultra detailed, 8k",
    negative_prompt="blurry, low quality, watermark",
    width=1024,
    height=1024,
    steps=30
)

# Download and display the image
if image_url and image_url.startswith("http"):
    img_response = requests.get(image_url)
    img = Image.open(BytesIO(img_response.content))
    img.show()
    img.save("generated_image.png")

Handling Async Responses

Large or complex images may return a "processing" status with a fetch_result URL. Poll this URL until the image is ready:

import time

def fetch_when_ready(fetch_url, max_attempts=10, wait_seconds=5):
    """Poll the fetch URL until the image is ready."""
    for attempt in range(max_attempts):
        response = requests.post(fetch_url, json={"key": API_KEY})
        result = response.json()
        
        if result.get("status") == "success":
            return result["output"][0]
        elif result.get("status") == "processing":
            print(f"Still processing... attempt {attempt + 1}/{max_attempts}")
            time.sleep(wait_seconds)
        else:
            print(f"Failed: {result}")
            return None
    
    return None  # Timed out

Key API Parameters Explained

Understanding these parameters lets you fine-tune your image output:

Parameter Type Description Recommended
model_id string Model to use (e.g., "sdxl", "sd-1.5", "realistic-vision-v51") "sdxl" for quality
prompt string Text description of the image you want Detailed, specific
negative_prompt string What to exclude from the image "blurry, low quality"
width / height string Output dimensions (multiples of 64) 512–1024px
num_inference_steps string Diffusion steps — more = better quality, slower 20–50
guidance_scale float Prompt adherence (higher = more literal) 7.0–9.0
seed integer Reproducibility seed (null = random) null for variety
samples string Number of images per request (1–4) 1 for speed
enhance_prompt string "yes" to auto-improve your prompt with AI "yes"

Choosing the Right Model

ModelsLab hosts 600+ models. Here are the most popular for different use cases:

  • sdxl — Best overall quality, photorealistic results
  • sd-1.5 — Fastest, widest model compatibility
  • realistic-vision-v51 — Hyper-realistic photos
  • dreamshaper-8 — Artistic, painterly style
  • anything-v5 — Anime and illustration

Browse all models at modelslab.com/models.

Image-to-Image Transformation

The image-to-image endpoint lets you transform an existing image using a prompt:

def image_to_image(init_image_url, prompt, strength=0.7):
    """Transform an existing image using Stable Diffusion."""
    
    url = "https://modelslab.com/api/v6/images/img2img"
    
    payload = {
        "key": API_KEY,
        "model_id": "sdxl",
        "init_image": init_image_url,   # URL of your source image
        "prompt": prompt,
        "negative_prompt": "blurry, low quality",
        "width": "1024",
        "height": "1024",
        "samples": "1",
        "num_inference_steps": "30",
        "strength": strength,            # 0.0=keep original, 1.0=full change
        "guidance_scale": 7.5
    }
    
    headers = {"Content-Type": "application/json"}
    response = requests.post(url, headers=headers, json=payload)
    result = response.json()
    
    if result.get("status") == "success":
        return result["output"][0]
    return None

# Transform a photo into a watercolor painting
new_image = image_to_image(
    init_image_url="https://example.com/your-photo.jpg",
    prompt="watercolor painting, artistic, vibrant colors",
    strength=0.75
)

Advanced: Custom Models & LoRA

One of ModelsLab's most powerful features is support for custom fine-tuned models and LoRA (Low-Rank Adaptation) weights. This lets you generate images in very specific styles or with consistent characters.

def generate_with_lora(prompt, lora_model, lora_strength=0.8):
    """Generate an image using a LoRA model."""
    
    url = "https://modelslab.com/api/v6/images/text2img"
    
    payload = {
        "key": API_KEY,
        "model_id": "sdxl",
        "prompt": prompt,
        "negative_prompt": "low quality, blurry",
        "width": "1024",
        "height": "1024",
        "samples": "1",
        "num_inference_steps": "30",
        "guidance_scale": 7.5,
        "lora_model": lora_model,       # LoRA model ID
        "lora_strength": lora_strength,  # 0.1–1.0
        "multi_lingual": "no",
        "panorama": "no",
        "self_attention": "no"
    }
    
    headers = {"Content-Type": "application/json"}
    response = requests.post(url, headers=headers, json=payload)
    return response.json()

Batch Generation with Error Handling

For production applications, implement robust error handling and batch processing:

def batch_generate(prompts, model_id="sdxl"):
    """Generate multiple images from a list of prompts."""
    results = []
    
    for i, prompt in enumerate(prompts):
        print(f"Generating image {i+1}/{len(prompts)}...")
        try:
            url = generate_image(prompt)
            results.append({"prompt": prompt, "url": url, "status": "success"})
        except Exception as e:
            results.append({"prompt": prompt, "url": None, "status": f"error: {e}"})
        
        time.sleep(1)  # Rate limiting: 1 request/second on free tier
    
    return results

# Generate a batch of 5 images
prompts = [
    "sunset over mountains, golden hour, cinematic",
    "futuristic robot, neon lights, cyberpunk",
    "cute cat in a garden, watercolor style",
    "ancient temple ruins, jungle, atmospheric",
    "abstract geometric art, vibrant colors, minimal"
]

results = batch_generate(prompts)
for r in results:
    print(f"✅ {r['prompt'][:40]}... → {r['url']}")

Pricing & Rate Limits

ModelsLab offers flexible pricing tiers for the Stable Diffusion API:

  • Free Tier: 100 API calls/month, standard models
  • Starter ($9/mo): 1,000 calls/month, all models
  • Pro ($49/mo): 10,000 calls/month, priority queue
  • Enterprise: Custom volume, dedicated infrastructure, SLA

Rate limits:

  • Free: 1 request/second
  • Starter: 3 requests/second
  • Pro: 10 requests/second

For high-volume use cases, contact the enterprise team for custom pricing with volume discounts.

FAQ: Stable Diffusion API

How fast does the Stable Diffusion API generate images?

Standard SD 1.5 models typically return images in 3–5 seconds. SDXL models take 5–10 seconds. Processing status responses occur when the queue is busy; in those cases, you can fetch the result in 30–60 seconds via the fetch URL.

Can I use the API without a GPU?

Yes. The ModelsLab Stable Diffusion API runs entirely in the cloud. Your Python application only needs an internet connection and an API key — no GPU or local installation required.

What image formats does the API return?

The API returns JPEG/PNG image URLs hosted on ModelsLab's CDN. Images are available for 24 hours. For permanent storage, download and save them to your own storage (S3, GCS, etc.).

Can I fine-tune my own Stable Diffusion model?

Yes. ModelsLab supports custom model fine-tuning via the Training API. Upload your dataset, trigger training, and get a model ID you can use in any generation endpoint.

Is the API NSFW-capable?

ModelsLab supports both safe and NSFW image generation. Set "safety_checker": "yes" to enable content filtering, or "no" to disable. NSFW generation requires an upgraded plan.

What's the difference between ModelsLab and Stability AI APIs?

Stability AI (stability.ai) offers their own proprietary SD API focused on their official models. ModelsLab provides access to the full open-source ecosystem: 600+ community models, custom LoRAs, and models from Civitai — at competitive pricing with a developer-first experience.

How do I handle "processing" status responses?

When the API returns "status": "processing", use the fetch_result URL provided in the response. Poll that URL every 5 seconds until you receive "status": "success" with the image URL. See the async polling example above.

Conclusion

The ModelsLab Stable Diffusion API makes it simple to integrate state-of-the-art AI image generation into any Python application. With support for 600+ models, image-to-image transformation, custom LoRA, and enterprise-grade infrastructure, it's the most flexible option for developers building AI-powered products.

Ready to start? Sign up for a free account and get 100 free API calls to try it out.

Next steps:

Share:
Adhik Joshi

Written by

Adhik Joshi

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.