AI Background Generation API — Product and Scene Backgrounds
Your product photos have white backgrounds. Your users want lifestyle shots, studio gradients, and contextual scenes — but hiring a photographer or a design team for every SKU doesn't scale. A background generation API lets you produce these programmatically: call an endpoint, describe a scene, get a polished image back in seconds.
This guide shows you how to generate product and scene backgrounds using the Nano Banana Pro and GPT-image-2 models via GenRelay, with code you can drop into your pipeline today.
What models should I use for background generation?
Two GenRelay models cover the main background generation use cases:
| Model | Best for | Pricing |
|---|---|---|
| Nano Banana Pro | New background scenes from text prompts; reference-guided style consistency | 1K: $0.030 / 2K: $0.030 / 4K: $0.042 per image |
| Nano Banana 2 | Fast, cost-efficient backgrounds where 1K–2K resolution is sufficient | 1K: $0.020 / 4K: $0.036 per image |
| GPT-image-2 | Editing an existing product image — swap or remove background via instruction | $0.014 per image |
Rule of thumb: use Nano Banana Pro or Nano Banana 2 to create backgrounds from scratch; use GPT-image-2 to edit an existing product photo's background.
How do I authenticate with the GenRelay API?
All GenRelay endpoints use Bearer token authentication. Generate a key in your dashboard, then set it as an environment variable:
export GENRELAY_API_KEY="gr_your_key_here"
Every request includes this header:
headers = {
"Authorization": f"Bearer {os.environ['GENRELAY_API_KEY']}",
"Content-Type": "application/json",
}
How do I generate a background scene with Nano Banana Pro?
To generate a standalone background image — say, a warm studio gradient for a product shot — POST to the image generations endpoint with your prompt and resolution:
import os, requests
headers = {
"Authorization": f"Bearer {os.environ['GENRELAY_API_KEY']}",
"Content-Type": "application/json",
}
payload = {
"model": "nano-banana-pro",
"prompt": (
"Soft warm studio background, gradient from ivory white to warm beige, "
"subtle bokeh, no objects, professional product photography lighting"
),
"size": "2048x2048", # 2K tier — $0.030/image
"n": 1,
}
r = requests.post(
"https://genrelay.ai/v1/images/generations",
headers=headers,
json=payload,
)
r.raise_for_status()
image_url = r.json()["data"][0]["url"]
print("Background ready:", image_url)
Resolution tiers for Nano Banana Pro:
| Resolution | Tier | Price |
|---|---|---|
| 1024×1024 | 1K | $0.030/image |
| 2048×2048 | 2K | $0.030/image |
| 4096×4096 | 4K | $0.042/image |
For most product backgrounds, 2K is the sweet spot: same price as 1K but significantly more detail to crop and reuse across different aspect ratios.
How do I use GPT-image-2 to replace a product photo's background?
GPT-image-2 accepts an instruction-based edit — you supply the source image and a text instruction describing the change. This is the most direct path when you already have a product photo and need to swap its background.
import os, base64, requests
headers = {
"Authorization": f"Bearer {os.environ['GENRELAY_API_KEY']}",
"Content-Type": "application/json",
}
# Read and base64-encode the source image
with open("product_white_bg.jpg", "rb") as f:
image_b64 = base64.b64encode(f.read()).decode()
payload = {
"model": "gpt-image-2",
"image": f"data:image/jpeg;base64,{image_b64}",
"prompt": (
"Replace the white background with a lush green forest scene, "
"soft afternoon light filtering through trees, product stays unchanged"
),
}
r = requests.post(
"https://genrelay.ai/v1/images/edits",
headers=headers,
json=payload,
)
r.raise_for_status()
result_url = r.json()["data"][0]["url"]
print("Edited image:", result_url)
GPT-image-2 costs $0.014 per image regardless of edit complexity. For pipelines processing thousands of SKUs, this flat per-image rate makes cost straightforward to forecast.
For more editing workflows — inpainting, variations, transparent background removal — see AI image editing API guide.
How do I maintain consistent backgrounds across multiple products?
For brand consistency — same studio style, same color temperature, same scene across 50 SKUs — use Nano Banana Pro's reference-guided mode. Pass a reference_image along with your prompt to anchor style, lighting, and composition:
import os, base64, requests
headers = {
"Authorization": f"Bearer {os.environ['GENRELAY_API_KEY']}",
"Content-Type": "application/json",
}
with open("brand_reference_bg.jpg", "rb") as f:
ref_b64 = base64.b64encode(f.read()).decode()
payload = {
"model": "nano-banana-pro",
"prompt": "Same studio background as reference, product photography, consistent warm lighting",
"reference_image": f"data:image/jpeg;base64,{ref_b64}",
"size": "2048x2048",
"n": 1,
}
r = requests.post(
"https://genrelay.ai/v1/images/generations",
headers=headers,
json=payload,
)
r.raise_for_status()
print("Consistent background:", r.json()["data"][0]["url"])
This avoids prompt drift across large product catalogs. Instead of trying to describe a lighting style in words, you anchor the model to a real image. It extracts color palette, depth, and composition from the reference while following your prompt for any deliberate changes.
What does background generation cost at scale?
Here's the math for a typical e-commerce workflow — 500 product backgrounds per day, split between net-new generations and edits:
| Task | Model | Unit price | Volume | Daily cost |
|---|---|---|---|---|
| Generate new studio backgrounds | Nano Banana Pro 2K | $0.030 | 300 images | $9.00 |
| Edit existing product photos (bg swap) | GPT-image-2 | $0.014 | 200 images | $2.80 |
| Daily total | 500 images | $11.80 |
Monthly: approximately $354 for 15,000 background images. Shifting more volume to GPT-image-2 edits (which are cheaper and synchronous) lowers that further.
If you need 4K resolution for print or large-format display, factor in Nano Banana Pro's 4K tier at $0.042/image — that's $12.60 for 300 high-res backgrounds vs. $9.00 at 2K.
For batch generation patterns — submitting multiple images concurrently with proper retry logic — see Batch image generation via API.
FAQ
Can I generate a transparent background (PNG with alpha channel)?
GPT-image-2 supports transparent output when you prompt it to remove the background: use "output_format": "png" in your request and write "Remove the background entirely, output transparent PNG." Nano Banana Pro currently returns opaque backgrounds only.
What prompt structure produces the best product backgrounds?
Scene descriptors outperform mood words. Instead of "beautiful background," write: surface material + lighting direction + color palette + depth-of-field note. Example: "Matte concrete surface, left-side window light, cool grey tones, shallow depth of field, no objects." Generic adjectives like "elegant" don't map to image features the model can reproduce reliably.
Can I upload a reference background and generate color or scene variations?
Yes — pass a reference_image to Nano Banana Pro with a prompt describing the variation (different time of day, different color, exterior vs. interior). The model generates a new image that shares the reference's structural composition while following the prompt for the specific changes.
Is there a rate limit on image generation requests?
GenRelay enforces per-key concurrency limits. For high-volume batch jobs, queue requests at 5–10 concurrent and use exponential backoff on 429 responses. See Image generation API authentication guide for error handling patterns.
When should I use Nano Banana 2 instead of Nano Banana Pro?
Use Nano Banana 2 if you need 1K backgrounds at $0.020/image — it's 33% cheaper at that tier. Use Nano Banana Pro when you need reference-guided generation, 2K at the same price as 1K, 4K output, or more precise instruction-following for complex scene compositions. For a full capability comparison, see Nano Banana Pro vs Nano Banana 2.
Getting started
- Create a free account at genrelay.ai — free credits included.
- Generate an API key in your dashboard.
- Run the generation snippet above with a prompt describing your target scene.
Background generation at scale comes down to model selection: Nano Banana Pro or Nano Banana 2 for net-new scenes, GPT-image-2 for instruction-based edits to existing images. Both integrate through the same GenRelay endpoint — one API key, consistent authentication, unified billing.