Skip to content

A lightweight FastAPI server that provides OpenAI-compatible endpoints for image generation and editing using Google's ImageFx API

License

Notifications You must be signed in to change notification settings

MinhxThanh/Unlimited-API

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Google ImageFx Image Generation API

A lightweight FastAPI server that provides OpenAI-compatible endpoints for image generation and editing using Google's ImageFx API

⭐ Give a star if you like this project! ⭐

Buy Me A Coffee

Overview

This API wraps Google's ImageFx API with OpenAI-compatible endpoints, making it easy to integrate Google's image generation capabilities into existing applications that use OpenAI's image generation format.

Features

  • OpenAI-Compatible Interface: Standard /v1/images/generations and /v1/images/image-edit endpoints
  • Text-to-Image Generation: Generate images from text prompts using Imagen 4 and Nano Banana
  • Image-to-Image Editing: Upload reference images and generate new images using R2I model
  • API Key Authentication: Secure access with Bearer token authentication
  • Multiple Image Formats: Supports b64_json response format
  • Various Aspect Ratios: Square (1:1), landscape (16:9), and portrait (9:16) image generation
  • Configurable Parameters: Control number of images, size, and seed for reproducibility

Installation

Requirements

  • Python 3.8+
  • pip

Install Dependencies

pip install -r requirements.txt

Configuration

1. Google ImageFx Authentication

You need a Google ImageFx bearer token to use this API.

📹 Video Tutorial: How to get Google ImageFx Token

Get-Token.mp4

Configure it in one of two ways:

Option A: Configuration File (Recommended)

Edit token config/google_flow_token.json:

{
  "bearer_token": "your_google_flow_bearer_token_here"
}

Option B: Environment Variable

export GOOGLE_FLOW_TOKEN="your_bearer_token_here"

2. API Key Authentication

The API uses Bearer token authentication. The default API key is sk-demo (configured in main.py).

To change it, edit the ARK_API_KEY variable in main.py:

ARK_API_KEY = "your-custom-api-key"

Running the Server

Using uvicorn directly

uvicorn main:app --host 0.0.0.0 --port 8000

Using Docker

docker build -t image-api .
docker run -p 8000:8000 image-api

The API will be available at http://localhost:8000

API Usage

Available Endpoints

  • GET /v1/models - List available models
  • POST /v1/images/generations - Generate images from text prompts
  • POST /v1/images/image-edit - Generate images using reference images

Supported Models

  • nano-banana - Nano Banana for text-to-image generation
  • IMAGEN_4 - Imagen 4 for text-to-image generation
  • nano-banana-r2i - R2I model for image-to-image generation with reference images

1. Text-to-Image Generation

Generate images from text prompts using the Imagen 4 model.

Basic Example

curl -X POST "http://localhost:8000/v1/images/generations" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer sk-demo" \
  -d '{
    "model": "nano-banana",
    "prompt": "a beautiful cat walking on water in a lake",
    "n": 1,
    "size": "1024x1792"
  }'

Generate Multiple Images

curl -X POST "http://localhost:8000/v1/images/generations" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer sk-demo" \
  -d '{
    "model": "nano-banana",
    "prompt": "a serene mountain landscape at sunset",
    "n": 4,
    "size": "1792x1024"
  }'

With Custom Seed (Reproducible Results)

curl -X POST "http://localhost:8000/v1/images/generations" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer sk-demo" \
  -d '{
    "model": "nano-banana",
    "prompt": "a cozy cabin in the woods at dusk",
    "n": 2,
    "size": "1024x1024",
    "seed": 123456
  }'

2. Image-to-Image Editing

Upload reference images and generate new images based on them using the R2I model.

curl -X POST "http://localhost:8000/v1/images/image-edit" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer sk-demo" \
  -d '{
    "model": "nano-banana-r2i",
    "prompt": "Generate a close-up image of a dog lying on lush grass",
    "images": ["base64_encoded_image_1", "base64_encoded_image_2"],
    "n": 2,
    "size": "1024x1792"
  }'

How it works:

  1. All images in the images array are uploaded to Google ImageFx
  2. Uploaded images are used as reference images for the R2I model
  3. The API generates n new images based on the prompt and reference images
  4. Generated images are returned in base64 format

Request Parameters

Text-to-Image (/v1/images/generations)

Parameter Type Required Default Description
model string Yes - Model to use: nano-banana
prompt string Yes - Text description of desired image
n integer No 1 Number of images to generate (1-4)
size string No "1024x1792" Image dimensions (see supported sizes)
response_format string No "b64_json" Response format (currently only b64_json supported)
seed integer No auto Seed for reproducible results

Image-to-Image (/v1/images/image-edit)

Parameter Type Required Default Description
model string Yes - Model to use: nano-banana-r2i
prompt string Yes - Text description for transformation
images array Yes - Array of base64-encoded images
n integer No 1 Number of images to generate (1-4)
size string No "1024x1792" Image dimensions (see supported sizes)
response_format string No "b64_json" Response format (currently only b64_json supported)
seed integer No auto Seed for reproducible results

Supported Image Sizes

  • 1792x1024 - Landscape (16:9)
  • 1024x1792 - Portrait (9:16)
  • 1024x1024 - Square (1:1)

Only these three aspect ratios are currently supported by the Google ImageFx API.

Response Format

All endpoints return responses in OpenAI-compatible format:

{
  "created": 1640995200,
  "data": [
    {
      "b64_json": "iVBORw0KGgoAAAANSUhEUgAA...",
      "revised_prompt": "a beautiful cat walking on water in a lake"
    },
    {
      "b64_json": "iVBORw0KGgoAAAANSUhEUgAA...",
      "revised_prompt": "a beautiful cat walking on water in a lake"
    }
  ]
}

Error Handling

The API returns standard HTTP status codes:

  • 200 OK - Successful request
  • 400 Bad Request - Invalid model or parameters
  • 401 Unauthorized - Invalid or missing API key
  • 500 Internal Server Error - Google ImageFx API error or configuration issues

Example Error Response

{
  "error": "Invalid API key"
}
{
  "error": "Model not supported for image generation"
}
{
  "error": "Google token not configured"
}

Project Structure

API-Image-Advance/
├── main.py                       # FastAPI application
├── requirements.txt              # Python dependencies
├── .env                          # Environment variables (optional)
├── Dockerfile                    # Docker configuration
├── run_image_api.sh              # Server startup script
├── config/
│   └── google_flow_token.json    # Google ImageFx authentication token
└── providers/
    ├── __init__.py               # Package initialization
    └── google_flow.py            # Google ImageFx API provider implementation

Development

Running in Development Mode

uvicorn main:app --reload --port 8000

The --reload flag enables auto-reload on code changes.

Testing the API

Test Model List

curl http://localhost:8000/v1/models

Test Image Generation

curl -X POST "http://localhost:8000/v1/images/generations" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer sk-demo" \
  -d '{
    "model": "nano-banana",
    "prompt": "image a cat",
    "n": 1
  }'

Notes and Limitations

  • The API requires a valid Google ImageFx bearer token to function
  • Tokens may expire and need to be refreshed periodically
  • The API maps OpenAI size formats to Google ImageFx aspect ratios automatically
  • Only three aspect ratios are supported (landscape, portrait, square)
  • Response format is currently limited to b64_json (base64-encoded images)
  • The maximum number of images per request is 4
  • Image-to-image editing requires the nano-banana-r2i model

Security Considerations

  • Change the default API key (sk-demo) in production environments
  • Keep your Google ImageFx bearer token secure and never commit it to version control
  • Use environment variables or secure configuration management for sensitive data
  • Consider implementing rate limiting for production deployments
  • Use HTTPS in production environments

Troubleshooting

"Google token not configured" Error

Make sure you have either:

  • Created config/google_flow_token.json with your bearer token, OR
  • Set the GOOGLE_FLOW_TOKEN environment variable

"Invalid API key" Error

Ensure your request includes the Authorization: Bearer sk-demo header (or your custom API key).

Connection Errors

Verify that:

  • You have internet connectivity
  • The Google ImageFx API is accessible from your network
  • Your bearer token is valid and not expired

License

This project is licensed under the MIT License - see the LICENSE file for details.

About

A lightweight FastAPI server that provides OpenAI-compatible endpoints for image generation and editing using Google's ImageFx API

Topics

Resources

License

Stars

Watchers

Forks