Vercel Serverless Functions in the GPT-Image2 API: Complete Endpoint Reference

The GPT-Image2 API is built on 25+ modular Vercel Serverless Functions located in the api/ directory, where each file automatically maps to an HTTP endpoint handling image generation, billing, authentication, and administrative operations.

The freestylefly/awesome-gpt-image-2 repository implements a scalable, serverless backend using Vercel's function-per-file architecture. Every endpoint exports an async handler(req, res) that Vercel invokes independently, allowing the platform to process image generation jobs, handle Alipay payments, and manage user sessions without maintaining persistent server infrastructure.

Core Image Generation Endpoints

The image generation workflow relies on three coordinated functions that interface with the Apimart API to process prompts and deliver results asynchronously.

Image Generation (api/generate-image.js)

Located at api/generate-image.js (line 80), this function handles both GET and POST requests to /api/generate-image. It validates user prompts, checks credit balances, reserves credits, and forwards the generation job to Apimart. The function returns a unique taskId that clients use to poll for completion.

Generation Status Polling (api/generation/status.js)

The api/generation/status.js file (line 30) exposes GET /api/generation for querying task progress. It accepts a taskId query parameter and returns the current state—queued, running, succeeded, or failed—along with error details or the resulting image URL when complete.

Apimart Webhook Callback (api/generation/callback.js)

Asynchronous completion notifications from Apimart are received at POST /api/generation via api/generation/callback.js (line 41). This webhook updates the task status, stores the generated image metadata, and triggers credit consumption finalization.

User Profile and Authentication

User identity and session management are handled through dedicated authentication providers and profile endpoints.

Current User Profile (api/me.js)

The api/me.js function (line 63) serves GET /api/me and returns the authenticated user's profile, credit balance, and subscription status. It reads the __session cookie to identify the user via Supabase authentication.

Favorites Management (api/favorites.js)

User-saved cases are managed through api/favorites.js (line 44), which supports both GET and POST methods at /api/favorites. This endpoint lists saved items and handles adding or removing favorites using the caseId parameter.

Watcha OAuth Integration (api/auth/watcha/*)

Third-party authentication is implemented through two coordinated functions:

  • api/auth/watcha/start.js (line 18): Initiates the OAuth flow with Watcha when called via GET /api/auth/watcha/start, redirecting users to the identity provider.
  • api/auth/watcha/callback.js (line 120): Handles the OAuth callback at GET /api/auth/watcha/callback, exchanges authorization codes for tokens, and creates or updates user sessions in Supabase.

Billing and Payment Processing

The platform implements a dual-billing system supporting subscription plans via Supabase and credit purchases through Alipay.

Subscription Management (api/billing/*)

Four core billing functions manage recurring subscriptions:

Alipay Credit Purchase Flow (api/billing/alipay/*)

Credit purchases use a comprehensive Alipay integration with six specialized endpoints:

Community Features

Paid community access is managed through a separate Alipay workflow and configuration endpoints.

Community Status and Configuration (api/community/*)

Community Alipay Payments (api/community/alipay/*)

Four functions handle community-specific transactions:

Administrative Operations

Restricted admin endpoints enable platform management and user support.

User and Metrics Management (api/admin/*)

Credit Adjustment (api/admin/credits/adjust.js)

Administrators can modify user balances through POST /api/admin/credits/adjust implemented in api/admin/credits/adjust.js (line 8), accepting userId and amount parameters to add or subtract credits.

Community Administration (api/admin/community/*)

Five functions manage community orders and refunds:

Apimart Pricing Configuration (api/apimart/pricing.js)

The frontend retrieves current generation pricing from GET /api/apimart/pricing via api/apimart/pricing.js (line 7), ensuring cost transparency before users submit generation requests.

Practical Usage Examples

Authenticate and Retrieve User Profile

curl -X GET https://your-domain.vercel.app/api/me \
     -H "Cookie: __session=YOUR_SESSION_COOKIE"

Submit an Image Generation Request

curl -X POST https://your-domain.vercel.app/api/generate-image \
     -H "Content-Type: application/json" \
     -d '{
           "prompt": "A futuristic city skyline at sunset",
           "caseId": 42,
           "language": "en"
         }'

Poll Generation Status

curl -G https://your-domain.vercel.app/api/generation \
     --data-urlencode "taskId=abcd1234"

Add a Favorite Case

curl -X POST https://your-domain.vercel.app/api/favorites \
     -H "Content-Type: application/json" \
     -d '{"caseId": 42}'

Initiate Watcha OAuth Login

Navigate to:


https://your-domain.vercel.app/api/auth/watcha/start

Create Alipay Checkout for Credits

curl -X POST https://your-domain.vercel.app/api/billing/alipay/checkout \
     -H "Content-Type: application/json" \
     -d '{"credits": 100}'

Summary

  • Modular Architecture: Each file in api/ automatically deploys as an independent Vercel Serverless Function mapped to a corresponding HTTP route.
  • Image Pipeline: The generation workflow uses generate-image.js to create tasks, status.js for polling, and callback.js for webhook completion.
  • Dual Payment Systems: Billing splits between Supabase subscriptions (api/billing/*) and Alipay credit purchases (api/billing/alipay/*).
  • Admin Controls: Restricted endpoints in api/admin/* provide user management, metrics, credit adjustment, and community administration.
  • Third-Party Auth: Watcha OAuth integration enables external authentication through dedicated start and callback handlers.

Frequently Asked Questions

How does Vercel route HTTP requests to these functions?

Vercel automatically maps files in the api/ directory to URL paths based on their relative location. For example, api/billing/checkout.js handles requests to /api/billing/checkout, while api/admin/community/qr.js responds to /api/admin/community/qr. Each file must export an async handler(req, res) function that receives the request and response objects.

What is the complete workflow for generating an image?

First, the client posts to /api/generate-image with a prompt, which validates credits and returns a taskId. The client then polls /api/generation?taskId=xxx every few seconds. Meanwhile, Apimart processes the job and calls /api/generation (the callback endpoint) when finished, updating the status to succeeded with the image URL.

Which authentication providers does the API support?

The primary authentication method is Watcha OAuth, implemented through api/auth/watcha/start.js and api/auth/watcha/callback.js. The system uses Supabase for session management, verified via the __session cookie on protected endpoints like /api/me.

How are administrative privileges verified in the admin endpoints?

Admin functions located in api/admin/* check the requesting user's role through the session cookie before executing privileged operations like credit adjustment (api/admin/credits/adjust.js) or user listing (api/admin/users.js). These endpoints return authorization errors for non-admin users.

Have a question about this repo?

These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:

Share the following with your agent to get started:
curl -s "https://instagit.com/install.md"

Works with
Claude Codex Cursor VS Code OpenClaw Any MCP Client

Maintain an open-source project? Get it listed too →