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

> Explore the Vercel Serverless Functions powering the GPT-Image2 API. Discover the endpoint reference for image generation, billing, auth, and admin operations built within the api/ directory.

- Repository: [苍何/awesome-gpt-image-2](https://github.com/freestylefly/awesome-gpt-image-2)
- Tags: api-reference
- Published: 2026-09-09

---

**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`](https://github.com/freestylefly/awesome-gpt-image-2/blob/main/api/generate-image.js))

Located at [`api/generate-image.js`](https://github.com/freestylefly/awesome-gpt-image-2/blob/main/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`](https://github.com/freestylefly/awesome-gpt-image-2/blob/main/api/generation/status.js))

The [`api/generation/status.js`](https://github.com/freestylefly/awesome-gpt-image-2/blob/main/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`](https://github.com/freestylefly/awesome-gpt-image-2/blob/main/api/generation/callback.js))

Asynchronous completion notifications from Apimart are received at `POST /api/generation` via [`api/generation/callback.js`](https://github.com/freestylefly/awesome-gpt-image-2/blob/main/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`](https://github.com/freestylefly/awesome-gpt-image-2/blob/main/api/me.js))

The [`api/me.js`](https://github.com/freestylefly/awesome-gpt-image-2/blob/main/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`](https://github.com/freestylefly/awesome-gpt-image-2/blob/main/api/favorites.js))

User-saved cases are managed through [`api/favorites.js`](https://github.com/freestylefly/awesome-gpt-image-2/blob/main/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`](https://github.com/freestylefly/awesome-gpt-image-2/blob/main/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`](https://github.com/freestylefly/awesome-gpt-image-2/blob/main/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:

- **[`api/billing/plans.js`](https://github.com/freestylefly/awesome-gpt-image-2/blob/main/api/billing/plans.js)** (line 9): Returns available subscription tiers via `GET /api/billing/plans`.
- **[`api/billing/checkout.js`](https://github.com/freestylefly/awesome-gpt-image-2/blob/main/api/billing/checkout.js)** (line 24): Initiates subscription checkout sessions via `POST /api/billing/checkout`.
- **[`api/billing/portal.js`](https://github.com/freestylefly/awesome-gpt-image-2/blob/main/api/billing/portal.js)** (line 8): Generates Supabase billing portal URLs via `GET /api/billing/portal`.
- **[`api/billing/history.js`](https://github.com/freestylefly/awesome-gpt-image-2/blob/main/api/billing/history.js)** (line 20): Retrieves transaction history via `GET /api/billing/history`.

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

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

- **[`api/billing/alipay/checkout.js`](https://github.com/freestylefly/awesome-gpt-image-2/blob/main/api/billing/alipay/checkout.js)** (line 20): Creates credit purchase orders via `POST /api/billing/alipay/checkout`.
- **[`api/billing/alipay/query.js`](https://github.com/freestylefly/awesome-gpt-image-2/blob/main/api/billing/alipay/query.js)** (line 15): Checks payment status via `GET /api/billing/alipay/query`.
- **[`api/billing/alipay/notify.js`](https://github.com/freestylefly/awesome-gpt-image-2/blob/main/api/billing/alipay/notify.js)** (line 25): Receives asynchronous Alipay notifications via `POST /api/billing/alipay/notify`.
- **[`api/billing/alipay/close.js`](https://github.com/freestylefly/awesome-gpt-image-2/blob/main/api/billing/alipay/close.js)** (line 10): Cancels pending transactions via `POST /api/billing/alipay/close`.
- **[`api/billing/alipay/refund.js`](https://github.com/freestylefly/awesome-gpt-image-2/blob/main/api/billing/alipay/refund.js)** (line 18): Requests refunds via `POST /api/billing/alipay/refund`.
- **[`api/billing/alipay/refund-query.js`](https://github.com/freestylefly/awesome-gpt-image-2/blob/main/api/billing/alipay/refund-query.js)** (line 13): Checks refund status via `GET /api/billing/alipay/refund-query`.

## Community Features

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

### Community Status and Configuration (`api/community/*`)

- **[`api/community/status.js`](https://github.com/freestylefly/awesome-gpt-image-2/blob/main/api/community/status.js)** (line 12): Returns service health at `GET /api/community/status`.
- **[`api/community/config.js`](https://github.com/freestylefly/awesome-gpt-image-2/blob/main/api/community/config.js)** (line 1): Provides frontend configuration including pricing tiers via `GET /api/community/config`.
- **[`api/community/qr.js`](https://github.com/freestylefly/awesome-gpt-image-2/blob/main/api/community/qr.js)** (line 10): Generates QR codes for community actions via `GET /api/community/qr`.

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

Four functions handle community-specific transactions:

- **[`api/community/alipay/checkout.js`](https://github.com/freestylefly/awesome-gpt-image-2/blob/main/api/community/alipay/checkout.js)** (line 28): Starts community access checkout.
- **[`api/community/alipay/query.js`](https://github.com/freestylefly/awesome-gpt-image-2/blob/main/api/community/alipay/query.js)** (line 16): Queries community payment status.
- **[`api/community/alipay/notify.js`](https://github.com/freestylefly/awesome-gpt-image-2/blob/main/api/community/alipay/notify.js)** (line 27): Processes community payment notifications.
- **[`api/community/alipay/close.js`](https://github.com/freestylefly/awesome-gpt-image-2/blob/main/api/community/alipay/close.js)** (line 15): Closes pending community transactions.

## Administrative Operations

Restricted admin endpoints enable platform management and user support.

### User and Metrics Management (`api/admin/*`)

- **[`api/admin/users.js`](https://github.com/freestylefly/awesome-gpt-image-2/blob/main/api/admin/users.js)** (line 30): Lists all platform users via `GET /api/admin/users`.
- **[`api/admin/metrics.js`](https://github.com/freestylefly/awesome-gpt-image-2/blob/main/api/admin/metrics.js)** (line 169): Provides usage analytics and dashboard charts via `GET /api/admin/metrics`.

### Credit Adjustment ([`api/admin/credits/adjust.js`](https://github.com/freestylefly/awesome-gpt-image-2/blob/main/api/admin/credits/adjust.js))

Administrators can modify user balances through `POST /api/admin/credits/adjust` implemented in [`api/admin/credits/adjust.js`](https://github.com/freestylefly/awesome-gpt-image-2/blob/main/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:

- **[`api/admin/community/orders.js`](https://github.com/freestylefly/awesome-gpt-image-2/blob/main/api/admin/community/orders.js)** (line 11): Lists community purchase orders.
- **[`api/admin/community/revoke.js`](https://github.com/freestylefly/awesome-gpt-image-2/blob/main/api/admin/community/revoke.js)** (line 13): Revokes community access.
- **[`api/admin/community/refund.js`](https://github.com/freestylefly/awesome-gpt-image-2/blob/main/api/admin/community/refund.js)** (line 45): Initiates community refunds.
- **[`api/admin/community/refund-query.js`](https://github.com/freestylefly/awesome-gpt-image-2/blob/main/api/admin/community/refund-query.js)** (line 12): Checks refund status.
- **[`api/admin/community/qr.js`](https://github.com/freestylefly/awesome-gpt-image-2/blob/main/api/admin/community/qr.js)** (line 20): Generates administrative QR codes.

### Apimart Pricing Configuration ([`api/apimart/pricing.js`](https://github.com/freestylefly/awesome-gpt-image-2/blob/main/api/apimart/pricing.js))

The frontend retrieves current generation pricing from `GET /api/apimart/pricing` via [`api/apimart/pricing.js`](https://github.com/freestylefly/awesome-gpt-image-2/blob/main/api/apimart/pricing.js) (line 7), ensuring cost transparency before users submit generation requests.

## Practical Usage Examples

### Authenticate and Retrieve User Profile

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

```

### Submit an Image Generation Request

```bash
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

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

```

### Add a Favorite Case

```bash
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

```bash
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`](https://github.com/freestylefly/awesome-gpt-image-2/blob/main/generate-image.js) to create tasks, [`status.js`](https://github.com/freestylefly/awesome-gpt-image-2/blob/main/status.js) for polling, and [`callback.js`](https://github.com/freestylefly/awesome-gpt-image-2/blob/main/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`](https://github.com/freestylefly/awesome-gpt-image-2/blob/main/api/billing/checkout.js) handles requests to `/api/billing/checkout`, while [`api/admin/community/qr.js`](https://github.com/freestylefly/awesome-gpt-image-2/blob/main/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`](https://github.com/freestylefly/awesome-gpt-image-2/blob/main/api/auth/watcha/start.js) and [`api/auth/watcha/callback.js`](https://github.com/freestylefly/awesome-gpt-image-2/blob/main/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`](https://github.com/freestylefly/awesome-gpt-image-2/blob/main/api/admin/credits/adjust.js)) or user listing ([`api/admin/users.js`](https://github.com/freestylefly/awesome-gpt-image-2/blob/main/api/admin/users.js)). These endpoints return authorization errors for non-admin users.