Skip to main content

Overview

The Generate Image API creates beautiful anime-style visualizations of Bhagavad Gita verses using AI. Images are generated via Replicate and cached in Supabase to avoid regeneration.
This endpoint is rate limited to 5 requests per hour per user to manage AI generation costs.

Generate Image

POST /api/generate-image
endpoint
Generate an anime-style image for a specific verse

Authentication

Required: Yes (Clerk session token)

Request Body

chapter
number
required
Chapter number (1-18)
verse
number
required
Verse number (1-78)

Response

status
string
Always “success”
data
object
Image generation result

Example Request

curl -X POST https://gitachat.org/api/generate-image \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_SESSION_TOKEN" \
  -d '{
    "chapter": 2,
    "verse": 47
  }'

Example Response

{
  "status": "success",
  "data": {
    "id": "cm7x8y9z10001abc123def456",
    "chapter": 2,
    "verse": 47,
    "url": "https://replicate.delivery/pbxt/abc123xyz789.png",
    "created_at": "2024-03-10T12:34:56.789Z"
  }
}

Check Image Existence

GET /api/generate-image?chapter=X&verse=Y
endpoint
Check if an image already exists for a verse (no authentication required)

Query Parameters

chapter
number
required
Chapter number (1-18)
verse
number
required
Verse number (1-78)

Response

Returns the existing image data if found, or null if no image exists.

Example Request

curl https://gitachat.org/api/generate-image?chapter=2&verse=47

Example Response (Image Exists)

{
  "status": "success",
  "data": {
    "id": "cm7x8y9z10001abc123def456",
    "chapter": 2,
    "verse": 47,
    "url": "https://replicate.delivery/pbxt/abc123xyz789.png",
    "created_at": "2024-03-10T12:34:56.789Z"
  }
}

Example Response (No Image)

{
  "status": "success",
  "data": null
}

Technical Details

AI Model

  • Provider: Replicate
  • Model: Anime-style image generation
  • Generation Time: 15-30 seconds
  • Output Format: PNG image

Storage

Images are stored in Supabase with the following schema:
FieldTypeDescription
iduuidPrimary key
chapterintegerChapter number
verseintegerVerse number
urltextReplicate image URL
created_attimestampGeneration timestamp
Unique Constraint: One image per (chapter, verse) combination

Rate Limiting

POST /api/generate-image: 5 requests per hour per user This strict rate limit helps manage AI generation costs while still providing reasonable access for users.

Caching Strategy

  1. Before generating, check if image exists for the verse
  2. If exists, return cached URL immediately
  3. If not exists, generate via Replicate and store in Supabase
  4. Subsequent requests return the cached image

Sharing Generated Images

Generated images can be shared via the shareable page:
https://gitachat.org/image/[image_id]
This page includes:
  • Full-size image display
  • Download as PNG button
  • Open Graph metadata for social media previews
  • Link back to the original verse

Error Codes

Status CodeDescription
200Success
400Invalid chapter/verse numbers
401Unauthorized (authentication required for POST)
429Rate limit exceeded (5/hour)
500Image generation failed or Replicate API error

Usage Notes

Image generation can take 15-30 seconds. Implement appropriate loading states in your UI.
Always check for existing images with the GET endpoint before generating new ones to avoid unnecessary API calls and rate limit consumption.

Implementation Reference

Source: frontend/app/api/generate-image/route.ts
  • POST handler: Lines 11-137
  • GET handler: Lines 140-177
  • Rate limiter: Line 9

Build docs developers (and LLMs) love