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

# OpenAI Images Generations

根据文本提示创建图像。该接口与 OpenAI 官方 Images API 的 `POST /v1/images/generations` 请求和响应结构保持一致，文档翻译自 <a href="https://developers.openai.com/api/reference/resources/images/methods/generate" target="_self">官方文档</a>，如已对接官方文档可直接使用，入参出参结构与支持保持一致。"。

支持模型包括 `gpt-image-2`、`gpt-image-1.5`、`gpt-image-1`、`dall-e-2` 和 `dall-e-3`。

<Note>
  由于 OpenAI 图像生成是同步响应，生成时间较长，建议使用 **[https://api.ttapi.org](https://api.ttapi.org)** 网关
</Note>


## OpenAPI

````yaml openapi/cn/openai.json POST /v1/images/generations
openapi: 3.1.0
info:
  title: Open AI API DOCS
  version: 1.0.0
  description: TTAPI Open AI API 接口服务
servers:
  - url: https://api.ttapi.io
security: []
paths:
  /v1/images/generations:
    post:
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/GenerationsRequest'
      responses:
        '200':
          description: 请求成功，返回图像生成结果（支持url/base64两种格式）
          content:
            application/json:
              schema:
                type: object
                properties:
                  created:
                    type: integer
                    description: 生成时间戳（秒）
                    example: 1745818538
                  data:
                    type: array
                    items:
                      oneOf:
                        - title: URL格式
                          type: object
                          properties:
                            url:
                              type: string
                              description: 生成图像的CDN地址。有效期详见[存储与持久化](/grids/cn/faq/storage)
                              example: >-
                                https://filesystem.site/cdn/20250428/5LsFMT0Def02RVVUkwJ4v7IslleMzo.webp
                            revised_prompt:
                              type: string
                              description: 优化后的提示词
                              example: >-
                                A cute fluffy cat with big, bright eyes, soft
                                fur, and a playful expression. It has a light
                                gray coat with hints of white on its paws and
                                tail. The cat is sitting in a cozy setting,
                                surrounded by soft cushions, with its tail
                                wrapped around its paws. Its ears are perked up,
                                and it's looking directly at the viewer, exuding
                                a sense of curiosity and warmth.
                          required:
                            - url
                        - title: Base64格式
                          type: object
                          properties:
                            b64_json:
                              type: string
                              description: 生成图像的Base64编码字符串
                              example: iVBORw0KGgoAAAANSUhE...
                          required:
                            - b64_json
                    description: 图像生成结果列表
                  usage:
                    type: object
                    properties:
                      input_tokens:
                        type: integer
                        description: 输入Token数
                        example: 9
                      input_tokens_details:
                        type: object
                        properties:
                          image_tokens:
                            type: integer
                            description: 图片Token数
                            example: 0
                          text_tokens:
                            type: integer
                            description: 文本Token数
                            example: 9
                        required:
                          - image_tokens
                          - text_tokens
                      output_tokens:
                        type: integer
                        description: 输出Token数
                        example: 4160
                      total_tokens:
                        type: integer
                        description: 总Token数
                        example: 4169
                    required:
                      - input_tokens
                      - input_tokens_details
                      - output_tokens
                      - total_tokens
                required:
                  - created
                  - data
                examples:
                  URL格式:
                    summary: 返回URL格式图像
                    value:
                      data:
                        - url: >-
                            https://filesystem.site/cdn/20250428/5LsFMT0Def02RVVUkwJ4v7IslleMzo.webp
                          revised_prompt: >-
                            A cute fluffy cat with big, bright eyes, soft fur,
                            and a playful expression. It has a light gray coat
                            with hints of white on its paws and tail. The cat is
                            sitting in a cozy setting, surrounded by soft
                            cushions, with its tail wrapped around its paws. Its
                            ears are perked up, and it's looking directly at the
                            viewer, exuding a sense of curiosity and warmth.
                      created: 1745818538
                  Base64格式:
                    summary: 返回Base64格式图像
                    value:
                      created: 1745818890
                      data:
                        - b64_json: iVBORw0KGgoAAAANSUhE...
                      usage:
                        input_tokens: 9
                        input_tokens_details:
                          image_tokens: 0
                          text_tokens: 9
                        output_tokens: 4160
                        total_tokens: 4169
        '400':
          $ref: '#/components/responses/400Response'
        '401':
          $ref: '#/components/responses/401Response'
      security:
        - CustomApiKey: []
components:
  schemas:
    GenerationsRequest:
      type: object
      properties:
        prompt:
          type: string
          description: >-
            图像的文本描述。`gpt-image-1.5`和`gpt-image-1` 最多支持 32000 个字符；`dall-e-2` 最多支持
            1000 个字符；`dall-e-3` 最多支持 4000 个字符。
        background:
          type: string
          description: >-
            设置生成图像背景的透明度。仅适用于 GPT Image 模型。可选值为 `transparent`、`opaque` 或
            `auto`（默认）。当设置为 `transparent` 时，`output_format` 必须支持透明度，请使用 `png` 或
            `webp`。
          enum:
            - transparent
            - opaque
            - auto
          default: auto
        model:
          type: string
          description: 用于图像生成的模型。
          enum:
            - gpt-image-2
            - gpt-image-1.5
            - gpt-image-1
            - dall-e-2
            - dall-e-3
        moderation:
          type: string
          description: 控制 GPT Image 模型生成图像的内容审核级别。可选 `low`（限制更少的过滤）或 `auto`（默认）。
          enum:
            - low
            - auto
          default: auto
        'n':
          type: integer
          description: 要生成的图像数量，默认值为 1。可选范围 `1-10`；`dall-e-3` 固定为 1。
        output_compression:
          type: integer
          description: >-
            生成图像的压缩级别（0-100）。仅适用于 `output_format` 为 `webp` 或 `jpeg` 的 GPT Image
            模型，默认值为 100。
          default: 100
        output_format:
          type: string
          description: 生成图像的返回格式。仅适用于 GPT Image 模型。可选 `png`（默认）、`jpeg` 或 `webp`。
          enum:
            - png
            - jpeg
            - webp
          default: png
        partial_images:
          type: integer
          description: 流式响应中要返回的部分图片数量，范围 `0-3`，默认值为 0。仅当 `stream` 为 `true` 时可用。
          default: 0
        quality:
          type: string
          description: |-
            图像质量。`auto`（默认）会为给定模型自动选择最佳质量。

            GPT Image 模型支持 `high`、`medium`、`low`；

            `dall-e-3` 支持 `hd` 和 `standard`；

            `dall-e-2` 仅支持 `standard`。
          default: auto
        response_format:
          type: string
          description: >-
            返回数据格式。仅 `dall-e-2` 与 `dall-e-3` 支持 `url` 或 `b64_json`；URL 有效期为 60
            分钟。GPT Image 模型始终返回 `b64_json`。
          enum:
            - url
            - b64_json
        size:
          type: string
          description: >-
            图像尺寸。


            GPT Image 模型支持 `1024x1024`、`1536x1024`、`1024x1536`、`auto`（默认）；


            `gpt-image-2` 支持 `WIDTHxHEIGHT` 任意分辨率字符串，宽高需可被 16 整除，宽高比需在 1:3 到 3:1
            之间。


            `dall-e-2` 支持 `256x256`、`512x512`、`1024x1024`；


            `dall-e-3` 支持 `1024x1024`、`1792x1024`、`1024x1792`。
          default: auto
        stream:
          type: boolean
          description: 是否以流式方式返回生成进度。启用后可通过 `partial_images` 获取部分图片事件。
          default: false
        style:
          type: string
          description: 生成图像的风格。仅适用于 `dall-e-3`。
          enum:
            - vivid（生动）
            - natural（自然）
        user:
          type: string
          description: 代表最终用户的唯一标识，可帮助 OpenAI 监测和检测滥用。
      required:
        - prompt
        - model
  responses:
    400Response:
      description: 参数错误
      content:
        application/json:
          schema:
            type: object
            properties:
              status:
                type: string
                example: FAILED
              message:
                type: string
                example: '"prompt" cannot be empty.'
              data:
                type: object
            required:
              - status
              - message
    401Response:
      description: 授权失败
      content:
        application/json:
          schema:
            type: object
            properties:
              status:
                type: string
                example: FAILED
              message:
                type: string
                example: Wrong TT-API-KEY or email is not activated.
            required:
              - status
              - message
  securitySchemes:
    CustomApiKey:
      type: apiKey
      in: header
      name: TT-API-KEY
      description: >-
        请前往 <a href='https://dashboard.ttapi.io' target='_blank'>TTAPI 控制台</a>
        获取 API 密钥。

````