> ## Documentation Index
> Fetch the complete documentation index at: https://docs.haitoken.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# OpenAI image generation

> Create image generation requests compatible with the OpenAI Images protocol, supporting multiple sizes, quality levels, and output formats

This endpoint is compatible with the OpenAI Images protocol and generates images from a text prompt. You can reuse the parameter structure from the official OpenAI documentation directly in the request body.

## Features

* Compatible with the OpenAI Images API request structure, so existing integration code migrates seamlessly
* Supports batch generation through the `n` parameter to return multiple images at once
* Supports `png`, `jpeg`, and `webp` output formats with `output_compression` control
* Supports background settings via `background` (`transparent` / `opaque` / `auto`)
* Supports both `url` and `b64_json` response formats
* Supports streaming (SSE) responses with intermediate results pushed via `partial_images`
* Supports generation parameters such as `quality`, `size`, and `style`

## Authentication

Include the `Authorization` header in the format `Bearer YOUR_API_KEY`.

## Supported image models

Available image models in the Model Square, please refer to [Model List](https://portal.haitoken.ai/en/models).

## Quick example

<CodeGroup>
  ```python Python theme={null}
  import requests

  url = "https://api.haitoken.ai/v1/images/generations"
  headers = {
      "Authorization": "Bearer YOUR_API_KEY",
      "Content-Type": "application/json"
  }
  data = {
      "model": "gpt-image-2",
      "prompt": "An orange cat wearing an astronaut helmet floating in space with Earth in the background",
      "size": "1024x1024",
      "quality": "high",
      "n": 1
  }

  response = requests.post(url, json=data, headers=headers)
  print(response.json())
  ```

  ```javascript Node.js theme={null}
  const response = await fetch('https://api.haitoken.ai/v1/images/generations', {
    method: 'POST',
    headers: {
      'Authorization': 'Bearer YOUR_API_KEY',
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      model: 'gpt-image-2',
      prompt: 'An orange cat wearing an astronaut helmet floating in space with Earth in the background',
      size: '1024x1024',
      quality: 'high',
      n: 1
    })
  });

  const data = await response.json();
  console.log(data);
  ```

  ```curl cURL theme={null}
  curl -X POST 'https://api.haitoken.ai/v1/images/generations' \
    -H 'Authorization: Bearer YOUR_API_KEY' \
    -H 'Content-Type: application/json' \
    -d '{
      "model": "gpt-image-2",
      "prompt": "An orange cat wearing an astronaut helmet floating in space with Earth in the background",
      "size": "1024x1024",
      "quality": "high",
      "n": 1
    }'
  ```
</CodeGroup>

A successful generation returns the image data:

```json theme={null}
{
  "created": 1787875200,
  "data": [
    {
      "url": "https://example.com/images/generated-1.png",
      "revised_prompt": "An orange cat wearing a transparent astronaut helmet floating in space with a blue Earth in the background"
    }
  ],
  "size": "1024x1024",
  "quality": "high",
  "usage": {
    "input_tokens": 12,
    "output_tokens": 4160,
    "total_tokens": 4172,
    "input_tokens_details": {
      "image_tokens": 0,
      "text_tokens": 12
    },
    "output_tokens_details": {
      "image_tokens": 4160,
      "text_tokens": 0
    }
  }
}
```

## Next steps

* See [OpenAI image edit](/docs/en/api-reference/images/openai/edit) to edit existing images
* See [Seedream image generation](/docs/en/api-reference/images/seedream/generation) for Seedream extension parameters
* See the [model list](/docs/en/api-reference/models/list-models) for available image models


## OpenAPI

````yaml en/api-reference/images/openai/generation/openapi.json POST /v1/images/generations
openapi: 3.0.1
info:
  title: Default Module
  description: ''
  version: 1.0.0
servers: []
security: []
tags: []
paths:
  /v1/images/generations:
    post:
      tags: []
      summary: OpenAI image generation
      parameters:
        - name: Authorization
          in: header
          description: API Key token (Bearer sk-xxx)
          required: true
          example: 'Bearer '
          schema:
            type: string
        - name: Content-Type
          in: header
          description: ''
          example: application/json
          schema:
            type: string
      requestBody:
        content:
          application/json:
            schema:
              description: ''
              type: object
              properties:
                prompt:
                  type: string
                  description: Text prompt
                background:
                  type: string
                  description: >-
                    Background of the generated image: "transparent" / "opaque"
                    / "auto"; defaults to "auto"
                model:
                  type: string
                  description: Model name
                moderation:
                  type: string
                  description: Content moderation mode
                'n':
                  type: integer
                  description: Number of images to generate
                output_compression:
                  type: integer
                  description: Output compression level
                output_format:
                  type: string
                  description: Output image format, e.g. "png", "jpeg", "webp"
                partial_images:
                  type: integer
                  description: >-
                    Number of intermediate result images pushed per update in
                    streaming responses
                response_format:
                  type: string
                  description: Response format, "url" or "b64_json"
                quality:
                  type: string
                  description: >-
                    Image quality, e.g. "high", "medium", "low", and "auto";
                    defaults to "auto"
                size:
                  type: string
                  description: Image size, e.g. "1024x1024"
                stream:
                  type: boolean
                  description: Whether to use streaming (SSE) responses
                style:
                  type: string
                  description: Image style, e.g. "vivid", "natural" (DALL-E 3 feature)
                user:
                  type: string
                  description: End-user identifier for monitoring and abuse detection
              required:
                - model
                - prompt
            examples: {}
      responses:
        '200':
          description: ''
          content:
            application/json:
              schema:
                type: object
                properties:
                  created:
                    type: integer
                    description: Image generation timestamp (Unix seconds)
                    format: int64
                  background:
                    type: string
                    description: Background image data echo
                  data:
                    type: array
                    items:
                      $ref: '#/components/schemas/ImageData'
                      description: >-
                        Single image data.

                        Contains the image URL or Base64 data, the revised
                        prompt, the image size, and more.

                        Compatible with the OpenAI DALL-E format (b64_json /
                        revised_prompt) and common image model response formats.
                    description: Generated image data list
                  output_format:
                    type: string
                    description: Output image format echo
                  quality:
                    type: string
                    description: Image quality echo
                  size:
                    type: string
                    description: Image size echo
                  usage:
                    description: Token and image usage statistics
                    type: object
                    properties:
                      input_tokens:
                        type: integer
                        description: Input token consumption
                      output_tokens:
                        type: integer
                        description: Output token consumption
                      total_tokens:
                        type: integer
                        description: Total token consumption
                      input_tokens_details:
                        $ref: '#/components/schemas/InputTokensDetails'
                        description: Input token details
                      output_tokens_details:
                        $ref: '#/components/schemas/OutputTokensDetails'
                        description: Output token details
                    required:
                      - output_tokens_details
                description: ''
          headers: {}
      deprecated: false
      security: []
components:
  schemas:
    ImageData:
      type: object
      properties:
        b64_json:
          type: string
          description: >-
            Base64-encoded image data (returned when response_format is
            b64_json)
        revised_prompt:
          type: string
          description: Prompt revised by the model (DALL-E 3 feature)
        output_format:
          type: string
          description: Output image format, e.g. "png", "jpeg", "webp"
        z_index:
          type: integer
          description: Layer stacking order
        name:
          type: string
          description: Layer name
        bounding_box:
          $ref: '#/components/schemas/BoundingBox'
          description: Layer bounding box
        url:
          type: string
          description: Publicly accessible image URL
        size:
          type: string
          description: Image size, e.g. "1024x1024"
        error:
          $ref: '#/components/schemas/ImageError'
          description: >-
            Error that occurred while generating this image (populated on
            partial success)
        description:
          type: string
          description: Layer description
    InputTokensDetails:
      type: object
      properties:
        image_tokens:
          type: integer
          description: Tokens consumed by the image portion
        text_tokens:
          type: integer
          description: Tokens consumed by the text portion
    OutputTokensDetails:
      type: object
      properties:
        image_tokens:
          type: integer
          description: Tokens consumed by the image portion
        text_tokens:
          type: integer
          description: Tokens consumed by the text portion
    BoundingBox:
      type: object
      properties:
        absolute:
          type: array
          items:
            type: integer
          description: Absolute coordinates
        normalized:
          type: array
          items:
            type: integer
          description: Normalized coordinates
    ImageError:
      type: object
      properties:
        code:
          type: string
          description: Error code
        message:
          type: string
          description: Error description
        type:
          type: string
          description: Error type classification
        param:
          type: string
          description: Request parameter that caused the error

````