Nano Banana 2 Image Generation API
API for generating AI images with text-to-image and image-to-image modes using Nano Banana 2, with Google Search enhanced prompts.
Try it online: https://nanophoto.ai/nano-banana-2
| Method | Path | Description |
|---|
POST | /api/nano-banana-2/generate | Create an image generation task |
POST | /api/nano-banana-2/check-status | Check task status and retrieve result |
Authorization: Bearer YOUR_API_KEY
Credits are pre-deducted when the task is created and automatically refunded if generation fails.
POST /api/nano-banana-2/generate
| Header | Value |
|---|
Content-Type | application/json |
Authorization | Bearer YOUR_API_KEY |
| Parameter | Type | Required | Description |
|---|
prompt | string | Yes | Image generation prompt |
mode | string | Yes | generate (text-to-image) or edit (image-to-image) |
aspectRatio | string | No | 16:9, 9:16, 4:3, or 3:4 (default: 16:9) |
imageQuality | string | No | 1K, 2K, or 4K (default: 1K) |
googleSearch | boolean | No | Enable Google Search to enhance prompt (default: false) |
inputImageUrls | string[] | Conditional | Public image URLs for edit mode (max 14). Required when mode is edit |
- API calls only accept
inputImageUrls (publicly accessible URLs) for edit mode. Base64 image upload (inputImages) is not supported via API.
- Maximum 14 input images allowed.
- Set
googleSearch: true to let the model search the web for additional context to enhance your prompt.
curl -X POST "https://nanophoto.ai/api/nano-banana-2/generate" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_API_KEY" \
--data-raw '{
"prompt": "A futuristic cityscape at sunset with flying cars and neon lights",
"mode": "generate",
"aspectRatio": "16:9",
"imageQuality": "2K"
}'
curl -X POST "https://nanophoto.ai/api/nano-banana-2/generate" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_API_KEY" \
--data-raw '{
"prompt": "The latest Tesla Cybertruck in a desert landscape",
"mode": "generate",
"aspectRatio": "16:9",
"imageQuality": "2K",
"googleSearch": true
}'
curl -X POST "https://nanophoto.ai/api/nano-banana-2/generate" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_API_KEY" \
--data-raw '{
"prompt": "Transform this photo into a watercolor painting style",
"mode": "edit",
"aspectRatio": "16:9",
"imageQuality": "1K",
"inputImageUrls": ["https://static.nanophoto.ai/demo/nano-banana-pro.webp"]
}'
{
"success": true,
"generationId": "abc123xyz",
"taskId": "task-456",
"status": "pending"
}
The generation is asynchronous. Use the generationId from Step 1 to poll for the result.
POST /api/nano-banana-2/check-status
| Header | Value |
|---|
Content-Type | application/json |
Authorization | Bearer YOUR_API_KEY |
| Parameter | Type | Required | Description |
|---|
generationId | string | Yes | The generationId returned from the generate endpoint |
curl -X POST "https://nanophoto.ai/api/nano-banana-2/check-status" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_API_KEY" \
--data-raw '{
"generationId": "abc123xyz"
}'
{
"success": true,
"status": "completed",
"imageUrl": "https://static.nanophoto.ai/generations/abc123.png",
"generationId": "abc123xyz",
"progress": 100
}
{
"success": true,
"status": "pending",
"generationId": "abc123xyz",
"progress": 15
}
{
"success": false,
"status": "failed",
"error": "Generation failed",
"generationId": "abc123xyz",
"progress": 0
}
async function generateImage(apiKey: string) {
// Step 1: Create generation task
const generateRes = await fetch("https://nanophoto.ai/api/nano-banana-2/generate", {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${apiKey}`,
},
body: JSON.stringify({
prompt: "A serene Japanese garden with cherry blossoms and a koi pond",
mode: "generate",
aspectRatio: "16:9",
imageQuality: "2K",
googleSearch: true,
}),
});
const generateData = await generateRes.json();
if (!generateData.success) {
throw new Error(`Generate failed: ${generateData.error}`);
}
const { generationId } = generateData;
console.log(`Task created: ${generationId}`);
// Step 2: Poll for result
while (true) {
await new Promise((r) => setTimeout(r, 3000)); // Wait 3 seconds
const statusRes = await fetch("https://nanophoto.ai/api/nano-banana-2/check-status", {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${apiKey}`,
},
body: JSON.stringify({ generationId }),
});
const statusData = await statusRes.json();
console.log(`Status: ${statusData.status}, Progress: ${statusData.progress}%`);
if (statusData.status === "completed") {
console.log(`Image URL: ${statusData.imageUrl}`);
return statusData.imageUrl;
}
if (statusData.status === "failed") {
throw new Error(`Generation failed: ${statusData.error}`);
}
}
}
- Poll every 3-5 seconds
- Typical generation time: 10-30 seconds
- Status flow:
pending â generating â completed or failed
- Credits are automatically refunded if the generation fails
| errorCode | HTTP Status | Description |
|---|
LOGIN_REQUIRED | 401 | Authentication required |
API_KEY_RATE_LIMIT_EXCEEDED | 429 | Rate limit exceeded |
INSUFFICIENT_CREDITS | 402 | Not enough credits |
INVALID_PROMPT | 400 | Prompt is missing or empty |
MISSING_INPUT_IMAGE | 400 | Edit mode requires input images |
TOO_MANY_IMAGES | 400 | More than 14 input images |
IMAGE_URLS_REQUIRED | 400 | API calls require inputImageUrls for edit mode |
CREDIT_RESERVATION_FAILED | 500 | Failed to reserve credits |
GENERATION_FAILED | 500 | Image generation failed |
NOT_FOUND | 404 | Generation ID not found |
FORBIDDEN | 403 | Access denied (not your generation) |