> ## 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 图片生成接口

> 创建图片生成请求，兼容 OpenAI Images 协议，支持多种尺寸、质量与输出格式

此接口兼容 OpenAI Images 协议，通过提示词生成图片，请求体可直接复用 OpenAI 官方文档中的参数结构。

## 功能特性

* 兼容 OpenAI Images API 请求结构，可无缝迁移已有接入代码
* 支持批量生成，通过 `n` 参数一次返回多张图片
* 支持 `png`、`jpeg`、`webp` 输出格式与 `output_compression` 压缩率控制
* 支持背景设置 `background`（`transparent` / `opaque` / `auto`）
* 支持 `url` 与 `b64_json` 两种响应格式
* 支持流式（SSE）响应，通过 `partial_images` 推送中间结果
* 支持 `quality`、`size`、`style` 等生成参数

## 认证方式

在请求头中携带 `Authorization` 字段，格式为 `Bearer YOUR_API_KEY`。

## 支持的图片模型

模型广场中可用的图片模型，请参考 [模型列表](https://portal.haitoken.ai/zh/models)。

## 快速示例

<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": "一只橘猫戴着宇航员头盔漂浮在太空中，背景是地球",
      "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: '一只橘猫戴着宇航员头盔漂浮在太空中，背景是地球',
      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": "一只橘猫戴着宇航员头盔漂浮在太空中，背景是地球",
      "size": "1024x1024",
      "quality": "high",
      "n": 1
    }'
  ```
</CodeGroup>

生成成功后返回图片数据：

```json theme={null}
{
  "created": 1787875200,
  "data": [
    {
      "url": "https://example.com/images/generated-1.png",
      "revised_prompt": "一只橘猫戴着透明的宇航员头盔漂浮在太空中，背景是蔚蓝色的地球"
    }
  ],
  "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
    }
  }
}
```

## 下一步

* 查看 [OpenAI 图片编辑接口](/docs/zh/api-reference/images/openai/edit) 编辑已有图片
* 查看 [Seedream 图片生成接口](/docs/zh/api-reference/images/seedream/generation) 了解 Seedream 扩展参数
* 查看 [模型列表](/docs/zh/api-reference/models/list-models) 了解可用的图片模型


## OpenAPI

````yaml zh/api-reference/images/openai/generation/openapi.json POST /v1/images/generations
openapi: 3.0.1
info:
  title: 默认模块
  description: ''
  version: 1.0.0
servers: []
security: []
tags: []
paths:
  /v1/images/generations:
    post:
      tags: []
      summary: OpenAI图片生成接口
      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: 提示词
                background:
                  type: string
                  description: 设置生成图像的背景： "transparent" /"opaque"/ "auto" 默认为 "auto"
                model:
                  type: string
                  description: 模型名称
                moderation:
                  type: string
                  description: 内容审核模式
                'n':
                  type: integer
                  description: 生成图片数量
                output_compression:
                  type: integer
                  description: 输出压缩率
                output_format:
                  type: string
                  description: 输出图片格式，如 "png"、"jpeg"、"webp"
                partial_images:
                  type: integer
                  description: 流式返回时每次推送的中间结果图片数量
                response_format:
                  type: string
                  description: 响应格式，"url" 或 "b64_json"
                quality:
                  type: string
                  description: 图片质量，如 "high"、"medium" and "low" and "auto" 默认为 "auto"
                size:
                  type: string
                  description: 生成图片尺寸，如 "1024x1024"
                stream:
                  type: boolean
                  description: 是否使用流式响应（SSE）
                style:
                  type: string
                  description: 图片风格，如 "vivid"、"natural"（DALL-E 3 特性）
                user:
                  type: string
                  description: 终端用户标识，用于监控和滥用检测
              required:
                - model
                - prompt
            examples: {}
      responses:
        '200':
          description: ''
          content:
            application/json:
              schema:
                type: object
                properties:
                  created:
                    type: integer
                    description: 图片生成时间戳（Unix 秒级时间戳）
                    format: int64
                  background:
                    type: string
                    description: 背景图像数据回显
                  data:
                    type: array
                    items:
                      $ref: '#/components/schemas/ImageData'
                      description: >-
                        单张图片数据。

                        包含图片的 URL 或 Base64 编码数据、修订后的提示词、图片尺寸等信息。

                        兼容 OpenAI DALL-E 格式（b64_json /
                        revised_prompt）及通用图像模型返回格式。
                    description: 生成的图片数据列表
                  output_format:
                    type: string
                    description: 输出图片格式回显
                  quality:
                    type: string
                    description: 图片质量回显
                  size:
                    type: string
                    description: 生成图片尺寸回显
                  usage:
                    description: Token 及图片用量统计
                    type: object
                    properties:
                      input_tokens:
                        type: integer
                        description: 输入 Token 消耗数量
                      output_tokens:
                        type: integer
                        description: 输出 Token 消耗数量
                      total_tokens:
                        type: integer
                        description: 总 Token 消耗数量
                      input_tokens_details:
                        $ref: '#/components/schemas/InputTokensDetails'
                        description: 输入 Token 明细
                      output_tokens_details:
                        $ref: '#/components/schemas/OutputTokensDetails'
                        description: 输出 Token 明细
                    required:
                      - output_tokens_details
                description: ''
          headers: {}
      deprecated: false
      security: []
components:
  schemas:
    ImageData:
      type: object
      properties:
        b64_json:
          type: string
          description: 图片的 Base64 编码数据（当 response_format 为 b64_json 时返回）
        revised_prompt:
          type: string
          description: 模型自动修订后的提示词（DALL-E 3 特性）
        output_format:
          type: string
          description: 输出图片格式，如 "png"、"jpeg"、"webp"
        z_index:
          type: integer
          description: 图层叠放顺序
        name:
          type: string
          description: 图层名称
        bounding_box:
          $ref: '#/components/schemas/BoundingBox'
          description: 图层边界框
        url:
          type: string
          description: 图片的可公开访问 URL 地址
        size:
          type: string
          description: 图片尺寸，如 "1024x1024"
        error:
          $ref: '#/components/schemas/ImageError'
          description: 当前图片生成过程中出现的错误信息（部分成功时填充）
        description:
          type: string
          description: 图层描述
    InputTokensDetails:
      type: object
      properties:
        image_tokens:
          type: integer
          description: 图片部分消耗的 Token 数
        text_tokens:
          type: integer
          description: 文本部分消耗的 Token 数
    OutputTokensDetails:
      type: object
      properties:
        image_tokens:
          type: integer
          description: 图片部分消耗的 Token 数
        text_tokens:
          type: integer
          description: 文本部分消耗的 Token 数
    BoundingBox:
      type: object
      properties:
        absolute:
          type: array
          items:
            type: integer
          description: 绝对坐标
        normalized:
          type: array
          items:
            type: integer
          description: 归一化坐标
    ImageError:
      type: object
      properties:
        code:
          type: string
          description: 错误码
        message:
          type: string
          description: 错误描述信息
        type:
          type: string
          description: 错误类型分类
        param:
          type: string
          description: 导致错误的请求参数名

````