背景替换
通过文本提示词替换图片背景。
/v2/open/image/replace_background13 积分 / 次输入
https://static.codia.ai/image_generate/2026-10-04/8c7b6ac0-2c6f-440b-867b-a1f6b77a2192.jpeg
1024 × 1024 px · JPG · 684 KBPremium skincare campaign scene: the bottle on a sculpted sand-colored plinth with soft desert shadows and dried palm leaves, warm golden-hour light
页面内在线运行即将上线。以下是 API 的真实请求与输出——创建密钥即可自行运行。
输出
示例13 积分 / 次


保留主体,根据文本提示词生成新背景。
- 适用场景
- 把商品图放进新场景
- 季节性和区域性营销活动变体
- 一张照片复用于多个渠道
- 输入
- 公开图片 URL 或上传的图片文件、一段描述新背景的 prompt,以及可选的 model。
- 输出
- data.image_urls 包含一张生成的图片。
- 运行方式
- 同步执行,通常不到十秒。
完整技术说明(英文)
Cost: 13 credits per successful call.
Image Replace Background supports two input modes:
- JSON URL mode: send
application/jsonwith the existing URL field. - Direct upload mode: send
multipart/form-datawith a local image file in theimagefield. The compatible field namefileis also accepted for the primary image. Codia checks credits before reading and uploading the file, so insufficient-credit requests return402without uploading anything.
Direct upload example:
curl 'https://openapi.codia.ai/v2/open/image/replace_background' \
-H 'Authorization: Bearer {codia_api_key}' \
-F 'image=@./product.png' \
-F 'prompt=clean white studio background'Keep the foreground subject of an image and synthesize a new background described by a text prompt. Synchronous, single-call. Combines background removal + inpainting + generative rendering in one step.
What this API is for
Reshooting product photography against new scenes ("on a marble counter", "in front of a sunset window"), reusing portraits across campaigns, or any time you want to keep the subject and change its environment. The subject is preserved; the prompt only steers the new background.
Request
| Field | Required | Notes |
|---|---|---|
image_url | yes | Public URL of the source image. |
prompt | yes | Natural-language description of the desired new background. Be specific about lighting, surface, and context for best results. |
model | no | Model ID. Defaults to nano_banana_2_1. Also: nano_banana_2, nano_banana_pro (higher fidelity), ideogram_v3 (alternative aesthetic). |
Prompt tips: describe the surface or scene ("clean white marble table"), the lighting ("soft studio lighting from the left"), and the mood. Avoid contradicting the subject's lighting unless you want the subject re-lit (the model may match it).
Response
Shared ImageUrlListSuccess shape — data.image_urls is an array of CDN URLs (length 1).
Billing & limits
- 13 credits per request from the unified Codia Open API balance.
- Synchronous, single-digit-second latency for typical inputs.
Errors
Standard set — 400 bad input (missing image_url or prompt), 401/403 auth, 402 credits, 429 rate limit, 500 internal.
Sync vs. async This synchronous endpoint returns the result inline — the simplest path. For production pipelines that need webhook delivery, automatic retry with idempotency, or explicit task cancellation, call
POST /v2/open/taskswithoperation=image_replace_backgroundinstead. Inputs and credit cost are identical.
安装与配置
将 API 密钥存储为运行环境中的环境变量。
export CODIA_API_KEY="your-api-key"身份验证
以 Bearer 令牌的形式发送密钥。密钥可在控制台中创建和吊销。
Authorization: Bearer <your-api-key>提交请求
JSON 请求接受公开 URL。标注支持 multipart/form-data 的接口也可以直接上传文件。
https://openapi.codia.ai·13 积分 / 次这些示例和 Sandbox 示例发送的是同一个请求,设置好 CODIA_API_KEY 就能直接运行。换成你自己的公开文件 URL 即可。
curl -X POST 'https://openapi.codia.ai/v2/open/image/replace_background' \
-H "Authorization: Bearer $CODIA_API_KEY" \
-H 'Content-Type: application/json' \
-d '{
"image_url": "https://static.codia.ai/image_generate/2026-10-04/8c7b6ac0-2c6f-440b-867b-a1f6b77a2192.jpeg",
"prompt": "Premium skincare campaign scene: the bottle on a sculpted sand-colored plinth with soft desert shadows and dried palm leaves, warm golden-hour light"
}'curl -X POST 'https://openapi.codia.ai/v2/open/image/replace_background' \
-H "Authorization: Bearer $CODIA_API_KEY" \
-F 'image=@./product.jpg' \
-F 'prompt=Premium skincare campaign scene: the bottle on a sculpted sand-colored plinth with soft desert shadows and dried palm leaves, warm golden-hour light'请求参数
Idempotency-Keystring · header可选可选,由用户自定义的幂等键(最多 255 个字符)。设置后,成功(2xx)的响应会缓存 7 天。使用相同的键 + 相同的请求体重试,会返回缓存的响应(不额外扣除积分);使用相同的键但不同的请求体重试,则返回 409。用于确保 POST 请求在网络故障时可以安全重试。对于 multipart/form-data 上传,请求会按表单字段和文件内容进行比较,因此即使客户端生成了新的 multipart boundary,重试的上传也会被重放。
image_urlstring必填源图片的公开 URL
promptstring必填对期望新背景的文本描述
modelstring可选生成新背景的模型。默认为 nano_banana_2_1。目前所有支持的模型每次请求均消耗 13 积分。
| 模型 | 适用场景 | 积分 |
|---|---|---|
nano_banana_2_1 | 高效替换背景,视觉一致性更好(Gemini Flash Image),适合作为大多数图片的默认选择。 | 13 |
nano_banana_2 | 快速替换背景(Gemini 3.1 Flash)。 | 13 |
nano_banana_pro | 影棚级 4K 背景,光照和反射真实自然。 | 13 |
ideogram_v3 | 风格化、插画风或文字较多的背景。 | 13 |
"nano_banana_2_1"imagefile必填源图片文件
filefile可选已弃用兼容用的源图片文件字段(已弃用:请改用 image)。
image_urlstring可选不上传图片时使用的源图片 URL
promptstring必填替换背景的提示词
modelstring可选可选的模型
响应结构
200处理后的图片 URL
codeintegermessagestringdataobjectdata.image_urlsstring[]生成或处理后的图片 URL
{
"code": 0,
"message": "ok",
"data": {
"image_urls": [
"https://static.codia.ai/image_generate/2026-10-04/e96ad334-0b97-4eb4-8953-57936bcfc99c.png"
]
}
}错误码
错误使用同样的响应结构:code 与 HTTP 状态码一致,message 说明原因,data 为 null,部分错误还会带 details 对象。请根据状态码判断,不要依赖 message 文本。
{
"code": 401,
"message": "Unauthorized",
"data": null
}| 状态码 | 说明 |
|---|---|
| 400 | 请求体格式错误或缺少必填参数 |
| 401 | API Key 缺失、无效或已被吊销 |
| 402 | 账户余额不足或订阅已过期 |
| 403 | 账户无权访问此接口 |
| 409 | Idempotency-Key 已被用于不同的请求体。 |
| 429 | 请求频率超限。请等待 `Retry-After` 响应头中指定的秒数后再重试。 |
| 500 | 服务器发生意外错误 |


