How to Add AI Image Generation to Your App | GenRelay
Your product roadmap has "AI image generation" on it. Your users are expecting it. The practical question is: how do you go from a working backend to one that generates images on demand, without managing three separate vendor accounts or rewriting your auth layer every time a better model ships?
This guide walks through the full integration path using the GenRelay API — a unified generative media API that routes requests to Nano Banana Pro, Nano Banana 2, and GPT-image-2 through a single endpoint with a consistent request and response schema across all models. As of August 2026, all three image models are available with no waitlist.
What does adding AI image generation to your app actually require?
The integration requires four components: an API key, a model choice, a server-side request handler, and a way to pass the resulting image URL or binary back to your frontend.
GenRelay exposes all image models at https://genrelay.ai/v1/images/generations. The request format follows the same structure as the OpenAI images API, so if your codebase already handles one image model, adding a second is a one-line model field swap.
Here is the minimal working integration:
import os
import requests
def generate_image(prompt: str, model: str = "nano-banana-pro", size: str = "1024x1024") -> str:
"""Generate an image and return its URL."""
r = requests.post(
"https://genrelay.ai/v1/images/generations",
headers={"Authorization": f"Bearer {os.environ['GENRELAY_API_KEY']}"},
json={
"model": model,
"prompt": prompt,
"n": 1,
"size": size,
},
timeout=60,
)
r.raise_for_status()
return r.json()["data"][0]["url"]
That is the entire surface area for a basic integration. The response structure (data[0].url) is identical regardless of which image model you route to.
How do you choose the right image model for your use case?
Model choice depends on three factors: output resolution, per-image cost, and whether your app needs image editing (in-painting, variations) in addition to generation.
| Model | Max resolution | Price at 1K | Price at 4K | Editing support |
|---|---|---|---|---|
| Nano Banana Pro | 4096×4096 | $0.030/image | $0.042/image | No |
| Nano Banana 2 | 4096×4096 | $0.020/image | $0.036/image | No |
| GPT-image-2 | 1536×1024 | $0.014/image | — | Yes (edit endpoint) |
Practical selection guide:
- App UI, avatars, thumbnails: GPT-image-2 at $0.014 handles most web-resolution needs and adds editing/variation support through the
/v1/images/editsendpoint. - High-resolution product shots or marketing assets: Nano Banana Pro at 4K for $0.042 per image.
- User-facing generation on a tighter budget: Nano Banana 2 at $0.020 for 1K images, with 4K available when needed.
For a detailed comparison of the two Nano Banana models, see the Nano Banana model pages. For GPT-image-2 specifics including the editing endpoint, see the GPT-image-2 guide.
How do you authenticate with the GenRelay API?
Authentication uses a Bearer token header. One API key from the GenRelay console works across all models — there is no per-model credential setup.
curl https://genrelay.ai/v1/images/generations \
-H "Authorization: Bearer YOUR_GENRELAY_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "nano-banana-2",
"prompt": "A product photograph of a white ceramic mug on a wooden table, studio lighting, white background",
"n": 1,
"size": "1024x1024"
}'
Key storage rules: never embed the API key in frontend code or mobile app bundles. All image generation calls should be routed through your backend. Store the key as an environment variable (GENRELAY_API_KEY) and inject it at runtime. The GenRelay console supports creating multiple scoped keys if you want separate credentials per environment (dev, staging, prod).
How do you handle the API response and return images to your frontend?
The API returns a JSON object with a data array. Each item contains a url field pointing to a time-limited public image, or base64-encoded image data if you set response_format: "b64_json".
import os
import requests
def generate_and_return(prompt: str, model: str = "gpt-image-2") -> dict:
r = requests.post(
"https://genrelay.ai/v1/images/generations",
headers={"Authorization": f"Bearer {os.environ['GENRELAY_API_KEY']}"},
json={
"model": model,
"prompt": prompt,
"n": 1,
"size": "1024x1024",
"response_format": "url", # swap to "b64_json" to receive inline data
},
timeout=60,
)
r.raise_for_status()
item = r.json()["data"][0]
return {
"url": item["url"],
"revised_prompt": item.get("revised_prompt"), # set when the model rewrites the prompt
}
On image URL lifetime: the returned URL is time-limited (typically 60 minutes). For production apps where you need images to persist, download the image immediately in your handler and upload it to your own object storage (S3, GCS, R2). Return your permanent URL to clients instead of the generated one. This also avoids serving images cross-origin from a third-party CDN.
How do you handle errors gracefully?
Four HTTP status codes cover the common failure cases:
| Status | Cause | Action |
|---|---|---|
| 400 | Invalid size, unrecognized model field, malformed JSON |
Validate request parameters; check model ID spelling |
| 401 | Missing or expired API key | Regenerate key in the GenRelay console |
| 429 | Rate limit exceeded | Add exponential backoff and retry |
| 500 / 503 | Upstream model unavailable | Retry once after a short delay; surface to user if persistent |
import os
import time
import requests
def generate_with_retry(prompt: str, model: str = "nano-banana-pro", max_attempts: int = 3) -> str:
for attempt in range(max_attempts):
try:
r = requests.post(
"https://genrelay.ai/v1/images/generations",
headers={"Authorization": f"Bearer {os.environ['GENRELAY_API_KEY']}"},
json={"model": model, "prompt": prompt, "n": 1, "size": "1024x1024"},
timeout=60,
)
if r.status_code == 429:
time.sleep(2 ** attempt)
continue
r.raise_for_status()
return r.json()["data"][0]["url"]
except requests.HTTPError:
if attempt == max_attempts - 1:
raise
time.sleep(1)
For high-throughput workloads, also consider wrapping your generation calls in a queue so that bursts don't hit rate limits synchronously. See the step-by-step generation guide for additional patterns on handling concurrent generation requests.
How much will AI image generation cost once your feature is live?
GenRelay bills per image, not per token or per second, so costs scale linearly with generation volume and are predictable from day one.
Example: 10,000 images per month at 1024×1024
| Model | Price per image | Monthly total |
|---|---|---|
| GPT-image-2 | $0.014 | $140 |
| Nano Banana 2 (1K) | $0.020 | $200 |
| Nano Banana Pro (1K–2K) | $0.030 | $300 |
The $14/month plan includes credits that cover approximately 466 GPT-image-2 images or 466 Nano Banana 2 images before pay-as-you-go billing applies. There is no minimum monthly commitment on the pay-as-you-go tier.
If your app generates images at user request and usage is hard to predict, Nano Banana 2 at $0.020 gives you room to grow without a large upfront spend. If you need 4K output, Nano Banana Pro at $0.042 per 4K image is still well under $50 per 1,000 high-resolution generations.
Frequently asked questions
Can I switch between models without changing my integration code?
Yes. The only required change between Nano Banana Pro, Nano Banana 2, and GPT-image-2 is the model field value. The endpoint URL, authentication, and response shape are identical across all three.
Does GenRelay support generating multiple images in a single call?
Yes. Set n to up to 4 (model-dependent) to receive multiple images from one request. For larger batches, run requests concurrently — see the batch image generation guide for concurrency patterns and cost math.
What's the typical latency for an image generation request?
Generation completes in roughly 5–20 seconds depending on model and resolution. Plan your frontend UX accordingly — show a loading state and avoid synchronous generation in user-facing flows without feedback.
Does GPT-image-2 support editing existing images?
Yes. GPT-image-2 exposes a /v1/images/edits endpoint that accepts an existing image and an optional mask for targeted in-painting. Nano Banana Pro and Nano Banana 2 are generation-only as of August 2026.
Is there a free tier for testing?
GenRelay includes free credits on signup that are usable across all image models. They are sufficient for development and integration testing without a paid plan.