stable-diffusion-webui-docker开发者API文档:后端接口详细说明
1. API概述与环境配置
你是否在使用Stable Diffusion WebUI Docker时,因缺乏完整的API文档而难以实现自动化工作流?本文详细说明后端接口设计、调用方法与实战案例,帮助开发者快速集成文本生成图像、模型管理等核心功能。
读完本文你将获得:
- 完整API接口清单(含请求参数/响应格式)
- 认证与权限控制方案
- 批量任务处理与异步执行策略
- 错误处理与调试最佳实践
- 生产环境部署优化指南
1.1 API启用配置
通过Docker Compose启用API服务:
# docker-compose.yml 核心配置片段
services:
auto: &automatic
<<: *base_service
profiles: ["auto"]
build: ./services/AUTOMATIC1111
environment:
- CLI_ARGS=--allow-code --medvram --xformers --enable-insecure-extension-access --api
启动服务使配置生效:
docker-compose up -d auto # 启动带API支持的GPU服务
# 或使用CPU版本(性能有限,仅推荐测试环境)
docker-compose up -d auto-cpu
1.2 服务架构
2. 核心API接口详解
2.1 文本生成图像 (txt2img)
请求参数
| 参数名 | 类型 | 必填 | 描述 | 示例值 |
|---|---|---|---|---|
| prompt | string | 是 | 文本提示词 | "a photo of an astronaut riding a horse on mars" |
| negative_prompt | string | 否 | 负面提示词 | "blurry, low quality" |
| steps | integer | 否 | 采样步数,范围[1, 150] | 20 |
| width | integer | 否 | 图像宽度 | 512 |
| height | integer | 否 | 图像高度 | 512 |
| batch_size | integer | 否 | 批量生成数量 | 4 |
| seed | integer | 否 | 随机种子,-1表示随机 | 12345 |
| sampler_name | string | 否 | 采样器名称 | "Euler a" |
| cfg_scale | float | 否 | 提示词引导系数 | 7.5 |
请求示例
POST /sdapi/v1/txt2img HTTP/1.1
Content-Type: application/json
Host: localhost:7860
{
"prompt": "a photo of an astronaut riding a horse on mars",
"negative_prompt": "blurry, low quality",
"steps": 20,
"width": 512,
"height": 512,
"batch_size": 2,
"sampler_name": "Euler a",
"cfg_scale": 7.5
}
响应格式
{
"images": ["base64_encoded_image_1", "base64_encoded_image_2"],
"parameters": {
"prompt": "a photo of an astronaut riding a horse on mars",
"steps": 20,
"width": 512,
"height": 512
},
"info": "{\"seed\": 12345, \"all_seeds\": [12345, 12346]}"
}
2.2 图像生成图像 (img2img)
请求参数(扩展txt2img参数)
| 参数名 | 类型 | 必填 | 描述 |
|---|---|---|---|
| init_images | array | 是 | 初始图像base64数组 |
| denoising_strength | float | 否 | 去噪强度,0.0-1.0 |
请求示例
POST /sdapi/v1/img2img HTTP/1.1
Content-Type: application/json
Host: localhost:7860
{
"init_images": ["base64_encoded_image"],
"prompt": "convert to cyberpunk style",
"denoising_strength": 0.75,
"steps": 30
}
2.3 模型管理接口
列出可用模型
GET /sdapi/v1/sd-models HTTP/1.1
响应示例:
[
{
"title": "v1-5-pruned-emaonly.safetensors [6ce0161689]",
"model_name": "v1-5-pruned-emaonly",
"hash": "6ce0161689",
"sha256": "810d6288d173f9a3c7044d1131555d3372a0d5d3f7f9d3d5d5d5d5d5d5d5d5d5",
"filename": "/data/models/Stable-diffusion/v1-5-pruned-emaonly.safetensors",
"config": null
}
]
切换当前模型
POST /sdapi/v1/options HTTP/1.1
Content-Type: application/json
{
"sd_model_checkpoint": "v1-5-pruned-emaonly.safetensors [6ce0161689]"
}
3. 高级功能
3.1 异步任务处理
通过任务ID轮询获取结果:
3.2 批量任务执行
POST /sdapi/v1/batch HTTP/1.1
Content-Type: application/json
{
"tasks": [
{
"type": "txt2img",
"params": {
"prompt": "task 1",
"steps": 20
}
},
{
"type": "img2img",
"params": {
"prompt": "task 2",
"init_images": ["base64..."]
}
}
]
}
4. 错误处理与调试
4.1 错误码体系
| 状态码 | 含义 | 示例场景 |
|---|---|---|
| 400 | 无效请求 | 参数格式错误 |
| 401 | 未授权 | API密钥验证失败 |
| 404 | 资源不存在 | 请求不存在的模型 |
| 429 | 请求频率超限 | 每秒请求超过10次 |
| 500 | 服务器错误 | 模型加载失败 |
4.2 调试工具
启用详细日志:
# docker-compose.yml 添加日志配置
services:
auto:
environment:
- CLI_ARGS=--api --verbose
查看API请求日志:
docker-compose logs -f auto | grep "API request"
5. 生产环境部署
5.1 性能优化配置
# docker-compose.yml 资源配置优化
services:
auto:
deploy:
resources:
reservations:
devices:
- driver: nvidia
device_ids: ['0'] # 指定GPU设备
capabilities: [compute, utility]
limits:
cpus: '8'
memory: 16G
5.2 安全加固
- API密钥认证
services:
auto:
environment:
- API_KEY=your_secure_random_key
请求时附加认证头:
POST /sdapi/v1/txt2img HTTP/1.1
Authorization: Bearer your_secure_random_key
- 网络隔离
services:
auto:
networks:
- backend # 仅允许后端服务访问API
ports:
- "127.0.0.1:7860:7860" # 绑定本地回环地址
6. 实战案例
6.1 Python客户端示例
import requests
import base64
from io import BytesIO
from PIL import Image
API_URL = "http://localhost:7860/sdapi/v1/txt2img"
API_KEY = "your_secure_random_key"
def generate_image(prompt):
payload = {
"prompt": prompt,
"steps": 25,
"width": 768,
"height": 512,
"batch_size": 1
}
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {API_KEY}"
}
response = requests.post(API_URL, json=payload, headers=headers)
response.raise_for_status()
# 解码base64图像数据
image_data = base64.b64decode(response.json()["images"][0])
return Image.open(BytesIO(image_data))
# 生成并保存图像
img = generate_image("a futuristic cityscape at sunset")
img.save("output.png")
6.2 负载测试脚本
# 使用curl进行简单负载测试
for i in {1..10}; do
curl -X POST "http://localhost:7860/sdapi/v1/txt2img" \
-H "Content-Type: application/json" \
-d '{"prompt":"test '${i}'", "steps":10, "width":256, "height":256}' &
done
7. 总结与展望
本文详细介绍了stable-diffusion-webui-docker的后端API接口,涵盖基础功能、高级特性与部署最佳实践。开发者可基于这些接口构建自动化工作流、集成到第三方应用或开发自定义插件。
7.1 接口版本控制
| API版本 | WebUI版本 | 主要变更 |
|---|---|---|
| v1 | 1.6.0+ | 初始版本 |
| v2 | 1.8.0+ | 添加批量任务接口 |
7.2 未来扩展方向
- WebSocket实时通知
- GraphQL接口支持
- 多节点任务分发
- 模型热更新机制
如果你觉得本文对你有帮助,请点赞、收藏、关注三连支持!有任何问题或建议,欢迎在评论区留言讨论。
下一篇预告:《API性能优化实战:从毫秒级响应到高并发处理》
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考



