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 will learn how to integrate the ModelsLab Stable Diffusion API into your Python applications, understand all key parameters, and build real-world image generation workflows.
Table of Contents
- What Is the Stable Diffusion API?
- Getting Started: API Key and Setup
- Text-to-Image Generation (Python)
- Key API Parameters Explained
- Image-to-Image Transformation
- Advanced: Custom Models and LoRA
- Pricing and Rate Limits
- FAQ
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 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 and 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 infrastructure, returning image URLs in 2-8 seconds.

Getting Started: API Key and Setup
Before writing any code, you will need a ModelsLab API key.
- Sign up at modelslab.com
- Navigate to your dashboard to API Keys
- Create a new API key and copy it
Install the requests library if you have not 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 is 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",
"prompt": prompt,
"negative_prompt": negative_prompt,
"width": str(width),
"height": str(height),
"samples": "1",
"num_inference_steps": str(steps),
"safety_checker": "no",
"enhance_prompt": "yes",
"seed": None,
"guidance_scale": 7.5,
"webhook": None,
"track_id": None
}
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</code></pre>
Key API Parameters Explained
Understanding these parameters lets you fine-tune your image output:
Parameter Type Description Recommended
model_idstring Model to use (e.g., "sdxl", "sd-1.5", "realistic-vision-v51") sdxl for quality
promptstring Text description of the image you want Detailed, specific
negative_promptstring What to exclude from the image blurry, low quality
width / heightstring Output dimensions (multiples of 64) 512-1024px
num_inference_stepsstring Diffusion steps — more equals better quality but slower 20-50
guidance_scalefloat Prompt adherence (higher equals more literal) 7.0-9.0
seedinteger Reproducibility seed (null equals random) null for variety
samplesstring Number of images per request (1-4) 1 for speed
enhance_promptstring 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,
"prompt": prompt,
"negative_prompt": "blurry, low quality",
"width": "1024",
"height": "1024",
"samples": "1",
"num_inference_steps": "30",
"strength": strength,
"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 and LoRA
One of ModelsLab most 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_strength": lora_strength,
"multi_lingual": "no",
"panorama": "no",
"self_attention": "no"
}
headers = {"Content-Type": "application/json"}
response = requests.post(url, headers=headers, json=payload)
return response.json()</code></pre>
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 per 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 and Rate Limits
ModelsLab offers flexible pricing tiers for the Stable Diffusion API:
- Free Tier: 100 API calls per month, standard models
- Starter ($9 per month): 1,000 calls per month, all models
- Pro ($49 per month): 10,000 calls per month, priority queue
- Enterprise: Custom volume, dedicated infrastructure, SLA
Rate limits:
- Free: 1 request per second
- Starter: 3 requests per second
- Pro: 10 requests per 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 or PNG image URLs hosted on ModelsLab 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 to yes to enable content filtering, or no to disable. NSFW generation requires an upgraded plan.
What is 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 is 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:
- Inpainting API — Edit specific regions of images
- ControlNet API — Precise pose and structure control
- Browse all 600+ models — Find the perfect style
