GPT-Image2 API Endpoint and Model: Complete Technical Reference

The GPT-Image2 service exposes a Vercel serverless function at /api/generate-image that explicitly calls the gpt-image-2 model for all image generation requests.

The freestylefly/awesome-gpt-image-2 repository provides a production-ready interface for AI image generation through a simple REST API. This guide examines the exact GPT-Image2 API endpoint and model configuration implemented in the source code, detailing how requests are handled and which specific model powers the image synthesis.

API Endpoint Structure

The project deploys a serverless function that handles image generation requests through a single, dedicated route.

Endpoint Path: /api/generate-image

When deployed on Vercel, the complete URL follows the pattern:


https://[your-domain]/api/generate-image

According to the live deployment documented in the repository, the production endpoint is available at https://gpt-image2.canghe.ai/api/generate-image.

In api/generate-image.js, the request handler validates the HTTP method before processing. The implementation supports both GET and POST requests, as defined in lines 59-66:

// Method validation from api/generate-image.js
if (!['GET', 'POST'].includes(req.method)) {
  return res.status(405).json({ error: 'Method not allowed' });
}

Model Specification

The service explicitly targets a specific model version when communicating with the upstream Ciyuan image generation API.

Model Used: gpt-image-2

Inside the generateImage helper function within api/generate-image.js, the code constructs a request payload that includes the model identifier. Lines 36-38 specify the model field:

// Model configuration from api/generate-image.js
const requestBody = {
  model: 'gpt-image-2',
  prompt: prompt,
  // ... additional parameters
};

This hardcoded value ensures all requests leverage the GPT-Image2 model capabilities rather than defaulting to alternative versions available through the provider.

Source Code Implementation

The endpoint implementation resides in api/generate-image.js and manages the complete request lifecycle from validation to response formatting.

Request Handling

The handler first checks the request method against the allowed list and extracts parameters. For POST requests, it parses the JSON body to retrieve the prompt and optional caseId values.

Upstream Communication

After validation, the function forwards the request to the Ciyuan service. The generateImage function constructs the payload with the mandatory model: 'gpt-image-2' field, ensuring consistency across all generation calls.

Response Format

The endpoint returns a JSON response containing:

  • ok: Boolean indicating success or failure
  • image: A data-URL string (JPEG format) containing the generated image when successful
  • error: Error message string when generation fails

Client Integration Examples

Integrate the GPT-Image2 endpoint into your applications using standard HTTP clients.

JavaScript Fetch Implementation

Call the endpoint from a frontend application using the Fetch API:

async function generateImage(prompt, caseId) {
  const response = await fetch('/api/generate-image', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ prompt, caseId })
  });

  const data = await response.json();
  
  if (data.ok) {
    // data.image contains a data-URL (JPEG) of the generated picture
    console.log('Image generated:', data.image);
    return data.image;
  } else {
    console.error('Generation failed:', data.error);
    throw new Error(data.error);
  }
}

cURL Command Line Request

Test the endpoint directly from the command line:

curl -X POST https://gpt-image2.canghe.ai/api/generate-image \
  -H "Content-Type: application/json" \
  -d '{"prompt":"A futuristic city skyline at sunset","caseId":1}'

The response returns a JSON object where the image field contains a base64-encoded data URL suitable for direct embedding in HTML img tags.

Summary

  • API Endpoint: The service exposes /api/generate-image as a Vercel serverless function, accessible at https://gpt-image2.canghe.ai/api/generate-image in production.
  • HTTP Methods: The handler accepts both GET and POST requests, validated in api/generate-image.js lines 59-66.
  • Model Configuration: All requests explicitly specify model: 'gpt-image-2' in the payload sent to the upstream API, as implemented in lines 36-38.
  • Response Format: Successful requests return a JSON object with an image field containing a JPEG data-URL.
  • Source Location: Core implementation is in api/generate-image.js, with authentication utilities in api/_lib/supabase.js.

Frequently Asked Questions

What is the exact URL for the GPT-Image2 API endpoint?

The production endpoint is https://gpt-image2.canghe.ai/api/generate-image. When running locally or deployed to your own Vercel instance, the relative path is /api/generate-image. The handler is implemented in api/generate-image.js and processes requests at this specific route.

Which AI model does the GPT-Image2 API use?

The API explicitly calls the gpt-image-2 model. In the source code at api/generate-image.js lines 36-38, the generateImage function hardcodes model: 'gpt-image-2' in the request payload sent to the upstream Ciyuan image generation service.

Does the endpoint support both GET and POST requests?

Yes. The handler in api/generate-image.js checks for both methods at lines 59-66, returning a 405 error for unsupported verbs. Both GET and POST requests are validated and processed, though POST is recommended for longer prompts that might exceed URL length limitations.

What format does the image response use?

The endpoint returns a JSON response where the image field contains a data-URL string encoding a JPEG image. This format allows immediate display in HTML without additional processing steps, as the base64-encoded data includes the necessary data:image/jpeg;base64, prefix.

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 →