A lightweight FastAPI server that provides OpenAI-compatible endpoints for image generation and editing using Google's ImageFx API
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.
- OpenAI-Compatible Interface: Standard
/v1/images/generationsand/v1/images/image-editendpoints - 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_jsonresponse 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
- Python 3.8+
- pip
pip install -r requirements.txtYou 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"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"uvicorn main:app --host 0.0.0.0 --port 8000docker build -t image-api .
docker run -p 8000:8000 image-apiThe API will be available at http://localhost:8000
GET /v1/models- List available modelsPOST /v1/images/generations- Generate images from text promptsPOST /v1/images/image-edit- Generate images using reference images
nano-banana- Nano Banana for text-to-image generationIMAGEN_4- Imagen 4 for text-to-image generationnano-banana-r2i- R2I model for image-to-image generation with reference images
Generate images from text prompts using the Imagen 4 model.
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"
}'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"
}'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
}'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:
- All images in the
imagesarray are uploaded to Google ImageFx - Uploaded images are used as reference images for the R2I model
- The API generates
nnew images based on the prompt and reference images - Generated images are returned in base64 format
| 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 |
| 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 |
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.
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"
}
]
}The API returns standard HTTP status codes:
200 OK- Successful request400 Bad Request- Invalid model or parameters401 Unauthorized- Invalid or missing API key500 Internal Server Error- Google ImageFx API error or configuration issues
{
"error": "Invalid API key"
}{
"error": "Model not supported for image generation"
}{
"error": "Google token not configured"
}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
uvicorn main:app --reload --port 8000The --reload flag enables auto-reload on code changes.
curl http://localhost:8000/v1/modelscurl -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
}'- 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-r2imodel
- 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
Make sure you have either:
- Created
config/google_flow_token.jsonwith your bearer token, OR - Set the
GOOGLE_FLOW_TOKENenvironment variable
Ensure your request includes the Authorization: Bearer sk-demo header (or your custom API key).
Verify that:
- You have internet connectivity
- The Google ImageFx API is accessible from your network
- Your bearer token is valid and not expired
This project is licensed under the MIT License - see the LICENSE file for details.
