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:
Key | Description | Type | Default |
---|---|---|---|
prompt | The prompt (what to include in the image) | string | |
negative_prompt | The negative prompt (what you don't want to see) | string | |
sampler_name | Sampler to use (Euler, DPM++ 2M Karras, etc) | string | |
steps | How many steps to perform | integer | 50 |
cfg_scale | How closely your prompt should be followed. Use a lower value for more creativity, and a higher value for more precision. | number | 7 |
width | Width of the output in pixels (this should match the images used to train the model) | integer | 512 |
height | Height of the output in pixels (this should match the images used to train the model) | integer | 512 |
batch_size | How many output images to create in one run | integer | 1 |
n_iter | How many runs to perform | integer | 1 |
restore_faces | Tries to fix facial defects | boolean | |
tiling | Create a repeating pattern | boolean | |
enable_hr | Enable High Resolution (increases the size of your output) | boolean | |
hr_scale | High Resolution Scale | number | 2 |
hr_upscaler | High Resolution Upscaler to use | string | |
hr_second_pass_steps | High Resolution Second Pass Steps | integer | 0 |
hr_resize_x | High Resolution Resize X (use instead of hr_scale) | integer | 0 |
hr_resize_y | High Resolution Resize Y (use instead of hr_scale) | integer | 0 |
hr_checkpoint_name | High Resolution Checkpoint Name | string | |
hr_sampler_name | High Resolution Sampler Name | string | |
hr_prompt | High Resolution Prompt | string | |
hr_negative_prompt | High Resolution Negative Prompt | string | |
denoising_strength | Denoising Strength for high resolution | number | |
seed | The seed to use. Using the same seed will produce the same output. | integer | -1 |
subseed | Subseed | integer | -1 |
subseed_strength | Subseed Strength | number | 0 |
seed_resize_from_h | Seed Resize From H | integer | -1 |
seed_resize_from_w | Seed Resize From W | integer | -1 |
do_not_save_samples | Do Not Save Samples | boolean | |
do_not_save_grid | Do Not Save Grid | boolean | |
eta | Eta | number | |
s_min_uncond | S Min Uncond | number | |
s_churn | S Churn | number | |
s_tmax | S Tmax | number | |
s_tmin | S Tmin | number | |
s_noise | S Noise | number | |
override_settings | Override Settings | object | |
override_settings _restore_afterwards | Override Settings Restore Afterwards | boolean | |
refiner_checkpoint | Refiner Checkpoint | string | |
refiner_switch_at | Refiner Switch At | number | |
disable_extra_networks | Disable Extra Networks | boolean | |
comments | Comments | object | |
firstphase_width | Firstphase Width | integer | 0 |
firstphase_height | Firstphase Height | integer | 0 |
sampler_index | Sampler Index | string | Euler |
script_name | Script Name | string | |
script_args | Script Args | array | |
send_images | Send Images | boolean | |
save_images | Save Images | boolean | |
styles | Apply styles | array | |
alwayson_scripts | Alwayson Scripts | object |