Welcome to the NanoBanana API!

This quickstart guide will walk you through the essential steps to start generating and editing images using our AI models.

Overview

Authentication

All API requests require authentication via Bearer Token.
1

Get Your API Key

Visit the API Key Management Page to obtain your API key.
2

Add to Request Headers

Include your API key in all requests:
Authorization: Bearer YOUR_API_KEY
Keep your API key secure and never share it publicly. If compromised, reset it immediately in the management page.

Basic Usage

1. Generate an Image

Start by creating your first image generation task:
curl -X POST "https://api.nanobananaapi.ai/api/v1/nanobanana/generate" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "prompt": "A beautiful sunset over mountains",
    "type": "TEXTTOIAMGE",
    "numImages": 1,
    "callBackUrl": "https://your-callback-url.com/webhook"
  }'
Response:
{
  "code": 200,
  "msg": "success",
  "data": {
    "taskId": "task12345"
  }
}

2. Check Task Status

Use the returned taskId to monitor progress:
curl -X GET "https://api.nanobananaapi.ai/api/v1/nanobanana/record-info?taskId=task12345" \
  -H "Authorization: Bearer YOUR_API_KEY"
Status Values:
  • 0: GENERATING - Task is currently being processed
  • 1: SUCCESS - Task completed successfully
  • 2: CREATE_TASK_FAILED - Failed to create the task
  • 3: GENERATE_FAILED - Task creation succeeded but generation failed

Complete Example

Here’s a complete workflow example:
class NanoBananaAPI {
  constructor(apiKey) {
    this.apiKey = apiKey;
    this.baseUrl = 'https://api.nanobananaapi.ai/api/v1/nanobanana';
  }
  
  async generateImage(prompt, options = {}) {
    const response = await fetch(`${this.baseUrl}/generate`, {
      method: 'POST',
      headers: {
        'Authorization': `Bearer ${this.apiKey}`,
        'Content-Type': 'application/json'
      },
      body: JSON.stringify({
        prompt,
        type: options.type || 'TEXTTOIAMGE',
        numImages: options.numImages || 1,
        callBackUrl: options.callBackUrl,
        watermark: options.watermark,
        imageUrls: options.imageUrls
      })
    });
    
    const result = await response.json();
    if (!response.ok || result.code !== 200) {
      throw new Error(`Generation failed: ${result.msg || 'Unknown error'}`);
    }
    
    return result.data.taskId;
  }
  
  async getTaskStatus(taskId) {
    const response = await fetch(`${this.baseUrl}/record-info?taskId=${taskId}`, {
      method: 'GET',
      headers: {
        'Authorization': `Bearer ${this.apiKey}`
      }
    });
    
    const result = await response.json();
    return result;
  }
  
  async waitForCompletion(taskId, maxWaitTime = 300000) {
    const startTime = Date.now();
    
    while (Date.now() - startTime < maxWaitTime) {
      const status = await this.getTaskStatus(taskId);
      
      switch (status.successFlag) {
        case 0:
          console.log('Task is generating...');
          break;
        case 1:
          console.log('Generation completed successfully!');
          return status.response;
        case 2:
        case 3:
          throw new Error(status.errorMessage || 'Generation failed');
      }
      
      await new Promise(resolve => setTimeout(resolve, 3000));
    }
    
    throw new Error('Generation timeout');
  }
}

// Usage Example
async function main() {
  const api = new NanoBananaAPI('YOUR_API_KEY');
  
  try {
    console.log('Starting image generation...');
    const taskId = await api.generateImage('A beautiful landscape painting');
    
    console.log(`Task ID: ${taskId}. Waiting for completion...`);
    const result = await api.waitForCompletion(taskId);
    
    console.log('Image generated successfully!');
    console.log('Result Image URL:', result.resultImageUrl);
    
  } catch (error) {
    console.error('Error:', error.message);
  }
}

main();

Parameters

Required Parameters

ParameterTypeDescription
promptstringText description for image generation
typestringGeneration type: TEXTTOIAMGE or IMAGETOIAMGE
callBackUrlstringWebhook callback URL

Optional Parameters

ParameterTypeDescriptionDefault
numImagesintegerNumber of images to generate (1-4)1
imageUrlsarrayInput image URLs for editing-
watermarkstringWatermark text for generated images-

Next Steps

Support

Need help? Contact our technical support: Ready to create amazing AI images? Start with the examples above and explore the full API capabilities!