Stable Diffusion 2.1 Serverless API

Stable Diffusion is a type of latent diffusion model that can generate images from text. It was created by a team of researchers and engineers from CompVis, Stability AI, and LAION. Stable Diffusion v2 is a specific version of the model architecture. It utilizes a downsampling-factor 8 autoencoder with an 865M UNet and OpenCLIP ViT-H/14 text encoder for the diffusion model. When using the SD 2-v model, it produces 768x768 px images. It uses the penultimate text embeddings from a CLIP ViT-H/14 text encoder to condition the generation process.

~0.00s
~$0.003
 1import requests
 2import json
 3
 4url = "https://api.segmind.com/v1/sd2.1-txt2img"
 5headers = {
 6    "x-api-key": "YOUR_API_KEY",
 7    "Content-Type": "application/json"
 8}
 9
10data = {
11    "samples": 1,
12    "prompt": "calico cat wearing a cosmonaut suit, 3d render, pixar style, 8k, high resolution",
13    "scheduler": "DDIM",
14    "num_inference_steps": 25,
15    "guidance_scale": 7.5,
16    "strength": 1,
17    "seed": 17123564234,
18    "img_width": 512,
19    "img_height": 512,
20    "base64": false
21}
22
23response = requests.post(url, headers=headers, json=data)
24
25if response.status_code == 200:
26    result = response.json()
27    print(json.dumps(result, indent=2))
28else:
29    print(f"Error: {response.status_code}")
30    print(response.text)

API Endpoint

POSThttps://api.segmind.com/v1/sd2.1-txt2img

Parameters

base64optional
boolean

Base64 encoding of the output image.

Default: false
guidance_scaleoptional
number

Scale for classifier-free guidance

Range: 1 - 15
img_heightoptional
integer

Image resolution.

Range: 512 - 1024
img_widthoptional
integer

Image resolution.

Range: 512 - 1024
negative_promptoptional
string

Prompts to exclude, eg. 'bad anatomy, bad hands, missing fingers'

num_inference_stepsoptional
integer

Number of denoising steps.

Range: 10 - 40
promptoptional
string

Prompt to render

samplesoptional
integer

Number of samples to generate.

Default: 1Range: 1 - 10
scheduleroptional
string

Type of scheduler.

Allowed values (12 total):
"DDIM""DPM Multi""DPM Single""Euler a""Euler""Heun""DPM2 a Karras""DPM2 Karras""LMS""PNDM"+2 more
seedoptional
integer

Seed for image generation.

strengthoptional
number

How much to transform the reference image

Response Type

Returns: Text/JSON

Common Error Codes

The API returns standard HTTP status codes. Detailed error messages are provided in the response body.

400

Bad Request

Invalid parameters or request format

401

Unauthorized

Missing or invalid API key

403

Forbidden

Insufficient permissions

404

Not Found

Model or endpoint not found

406

Insufficient Credits

Not enough credits to process request

429

Rate Limited

Too many requests

500

Server Error

Internal server error

502

Bad Gateway

Service temporarily unavailable

504

Timeout

Request timed out