For the complete documentation index, see llms.txt. This page is also available as Markdown.

Replace Background

Replace or swap the background of any image while preserving the foreground subject using the Image Generation API.


Overview

The Replace Background feature takes an existing photo and rewrites the background based on your text prompt, while keeping the foreground subject intact.

Replace Background API

POST /api/v1/images/generate

Headers

Name
Value

Content-Type

application/json

Authorization

Bearer <token>

How it works:

  Input photo + prompt


  [Gemini model understands
   subject vs. background]


  New background rendered
  around the subject


  Output image returned

Two approaches:

Approach
When to Use
Models

Direct replacement

Background is clearly distinct from subject

vertex/nano-banana-flash, vertex/nano-banana-pro

Cutout + replace pipeline

Hair, fur, complex edges, transparent objects

BiRefNet → Nano Banana


Supported Models

Only Gemini-based models support image-to-image editing. Imagen 4 variants are text-to-image only and do not accept source images.

Model
Speed
Max Resolution
Max Source Images
Best For

vertex/nano-banana-flash

Fast

4K

14

Rapid iteration, prototyping, batch jobs

vertex/nano-banana-pro

Standard

4K

14

Production, text in images, precise consistency


Quick Start

Response 200 OK:


Request Reference

Required Fields

Field
Type
Description

model

string

vertex/nano-banana-flash or vertex/nano-banana-pro

prompt

string (max 4000)

Describe the new background. See Prompt Writing Guide.

image_urls

string[]

Array containing the URL of the source image to edit.

Optional Fields

Field
Type
Default
Description

quality

string

2K

Output resolution. Flash: 512 | 1K | 2K | 4K. Pro: 1K | 2K | 4K.

aspect_ratio

string

1:1

Output dimensions. See valid values below.

num_images

integer

1

Number of variations to generate (1–4).

temperature

number

1.0

Creativity level. 0.7–0.9 = faithful, 1.2–1.5 = more creative.

reference_images

object[]

Up to 14 reference images for style/environment matching.

use_search_grounding

boolean

false

Enable real-time search for factually accurate backgrounds. Flash only.

celery

boolean

false

true = async processing, returns task_id immediately.

call_webhook

boolean

false

Send a POST callback when generation completes.

webhook_url

string

Webhook destination URL. Required when call_webhook: true.

webhook_secret

string

Secret sent as x-webhook-secret header in the callback.

Valid aspect_ratio Values


Payloads by Use Case

1. Simple Background Swap

Replace a busy background with a clean, minimal one.


2. Outdoor / Scenic Background

Place the subject in a natural or architectural environment.


3. Product Photography Background

Ideal for e-commerce — clean, professional backgrounds.


4. Background Matching a Reference Image

Use a reference photo to match a specific environment or style.


5. Virtual Meeting / Office Background

Professional background replacement for profile photos or video thumbnails.


6. Real-World Context with Search Grounding

Generate a geographically or factually accurate background using live search data. Nano Banana Flash only.


7. Multiple Variations in One Request

Generate several background options in a single call.


Prompt Writing Guide

The prompt is the most important factor in getting clean background replacement. Follow these principles:

Always Anchor the Subject

Include an explicit instruction to preserve the foreground. Without it the model may alter both subject and background.

Describe the New Background Specifically

Vague prompts lead to inconsistent results. Be specific about lighting, distance, depth of field, and mood.

Specify Lighting to Match the Subject

If the source photo has directional light, describe matching light in the background to avoid a compositing-look.

Use Negative Instructions When Needed

State what you do NOT want to help the model avoid common pitfalls.

Run the API

To test this API, please use the following link:

Last updated