Basic Example

POST /sdapi/v1/txt2img

{
  "prompt": "Sunset in the mountains, lake in front",
  "negative_prompt": "clouds, people",
  "sampler_name": "Euler",
  "steps": 20,
  "cfg_scale": 7,
  "height": 512,
  "width": 512,
  "seed": -1
}

High Resolution

To increase the resolution of the generated image, add the following params:

{
  "enable_hr": true,
  "hr_scale": 2,
  "denoising_strength": 0.7,
  "hr_second_pass_steps": 10,
  "hr_upscaler": "R-ESRGAN"
}

The hr-scale param multiplies the specified width and height (if the base resolution is 512x512, and the hr_scale is 2, then the output will be 1024x1024)

Response

Your response will contain an array of output images, and the values of all the parameters it used to generate the images:

{
  "images": ["iVBORw0KGgoAAAANSUhEUgAAA..."],
  "parameters": {
    "enable_hr": false,
    "denoising_strength": 0,
    "firstphase_width": 0,
    "firstphase_height": 0,
    "hr_scale": 2.0,
    "hr_upscaler": null,
    "hr_second_pass_steps": 0,
    "hr_resize_x": 0,
    "hr_resize_y": 0,
    "prompt": "Astronaut planting a flag on the moon",
    "styles": null,
    "seed": -1,
    "subseed": -1,
    "subseed_strength": 0,
    "seed_resize_from_h": -1,
    "seed_resize_from_w": -1,
    "sampler_name": null,
    "batch_size": 1,
    "n_iter": 1,
    "steps": 20,
    "cfg_scale": 7.0,
    "width": 512,
    "height": 512,
    "restore_faces": false,
    "tiling": false,
    "do_not_save_samples": false,
    "do_not_save_grid": false,
    "negative_prompt": "",
    "eta": null,
    "s_min_uncond": 0.0,
    "s_churn": 0.0,
    "s_tmax": null,
    "s_tmin": 0.0,
    "s_noise": 1.0,
    "override_settings": null,
    "override_settings_restore_afterwards": true,
    "script_args": [],
    "sampler_index": "Euler",
    "script_name": null,
    "send_images": true,
    "save_images": false,
    "alwayson_scripts": {}
  }
}

The number of images depends on what you specified for a batch size. The images are in base64 format, and therefore need to be decoded and saved to the file system as normal images:

result.images.forEach((img, i) => {
  const buf = Buffer.from(img, 'base64');
  fs.writeFileSync(`image-${i}.png`, buf);
});

Full Example

Here is a full javascript (nodejs) example:

const result = await fetch('[server]:7860/sdapi/v1/txt2img', {
  method: 'POST',
  body: JSON.stringify({
    prompt: "Sunset in the mountains, lake in front",
    negative_prompt: "clouds, people",
    sampler_name: "Euler",
    steps: 20,
    cfg_scale: 7,
    height: 512,
    width: 512
  })
}).then(res => res.json());
result.images.forEach((img, i) => {
  const buf = Buffer.from(img, 'base64');
  fs.writeFileSync(`image-${i}.png`, buf);
});

Control Net

See the ControlNet API guide for sending in a control input image

Full List of params

This is the full list of params that can be used for the txt2img endpoint:

KeyDescriptionTypeDefault
promptThe prompt (what to include in the image)string
negative_promptThe negative prompt (what you don't want to see)string
sampler_nameSampler to use (Euler, DPM++ 2M Karras, etc)string
stepsHow many steps to performinteger50
cfg_scaleHow closely your prompt should be followed. Use a lower value for more creativity, and a higher value for more precision.number7
widthWidth of the output in pixels (this should match the images used to train the model)integer512
heightHeight of the output in pixels (this should match the images used to train the model)integer512
batch_sizeHow many output images to create in one runinteger1
n_iterHow many runs to performinteger1
restore_facesTries to fix facial defectsboolean
tilingCreate a repeating patternboolean
enable_hrEnable High Resolution (increases the size of your output)boolean
hr_scaleHigh Resolution Scalenumber2
hr_upscalerHigh Resolution Upscaler to usestring
hr_second_pass_stepsHigh Resolution Second Pass Stepsinteger0
hr_resize_xHigh Resolution Resize X (use instead of hr_scale)integer0
hr_resize_yHigh Resolution Resize Y (use instead of hr_scale)integer0
hr_checkpoint_nameHigh Resolution Checkpoint Namestring
hr_sampler_nameHigh Resolution Sampler Namestring
hr_promptHigh Resolution Promptstring
hr_negative_promptHigh Resolution Negative Promptstring
denoising_strengthDenoising Strength for high resolutionnumber
seedThe seed to use. Using the same seed will produce the same output.integer-1
subseedSubseedinteger-1
subseed_strengthSubseed Strengthnumber0
seed_resize_from_hSeed Resize From Hinteger-1
seed_resize_from_wSeed Resize From Winteger-1
do_not_save_samplesDo Not Save Samplesboolean
do_not_save_gridDo Not Save Gridboolean
etaEtanumber
s_min_uncondS Min Uncondnumber
s_churnS Churnnumber
s_tmaxS Tmaxnumber
s_tminS Tminnumber
s_noiseS Noisenumber
override_settingsOverride Settingsobject
override_settings _restore_afterwardsOverride Settings Restore Afterwardsboolean
refiner_checkpointRefiner Checkpointstring
refiner_switch_atRefiner Switch Atnumber
disable_extra_networksDisable Extra Networksboolean
commentsCommentsobject
firstphase_widthFirstphase Widthinteger0
firstphase_heightFirstphase Heightinteger0
sampler_indexSampler IndexstringEuler
script_nameScript Namestring
script_argsScript Argsarray
send_imagesSend Imagesboolean
save_imagesSave Imagesboolean
stylesApply stylesarray
alwayson_scriptsAlwayson Scriptsobject