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

# 生成图片

> 调用 gpt-image-2 的同步生图接口并直接获取结果。

使用这个接口调用 `gpt-image-2` 的同步图片生成能力。

<Tip>
  建议先从 `model: gpt-image-2`、`n: 1`、`resolution: 2k` 开始。同步模式下，结果会直接在接口响应中返回。
</Tip>


## OpenAPI

````yaml POST /v1/images/generations
openapi: 3.1.0
info:
  title: SmewAI Images API
  description: OpenAI-compatible image generation and editing endpoints for SmewAI.
  version: 1.0.0
servers:
  - url: https://smew.ai
    description: Production API
security:
  - bearerAuth: []
paths:
  /v1/images/generations:
    post:
      tags:
        - Images
      summary: 生成图片
      description: >-
        Generate images with the `gpt-image-2` model in synchronous mode. The
        API returns the image result directly in the response.
      operationId: generateImage
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/ImageGenerationRequest'
            examples:
              basic:
                summary: 文生图
                value:
                  model: gpt-image-2
                  prompt: A clean orange cat astronaut sticker on a pastel background.
                  'n': 1
                  size: '1:1'
                  resolution: 2k
              reference:
                summary: 基于参考图生成
                value:
                  model: gpt-image-2
                  prompt: Turn this product photo into a watercolor poster.
                  'n': 1
                  size: '1:1'
                  resolution: 2k
                  image_urls:
                    - https://example.com/reference-photo.jpg
      responses:
        '200':
          description: Image generation result
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ImageGenerationResponse'
              examples:
                success:
                  value:
                    created: 1712345678
                    data:
                      - url: https://smew.ai/media/generated/0.png
        '400':
          description: Bad request
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
        '401':
          description: Unauthorized
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
components:
  schemas:
    ImageGenerationRequest:
      type: object
      required:
        - model
        - prompt
      properties:
        model:
          type: string
          enum:
            - gpt-image-2
          description: Image model name.
        prompt:
          type: string
          description: Describe the image you want to generate.
        'n':
          type: integer
          description: Number of images to generate. Use `1`.
          default: 1
          minimum: 1
          maximum: 1
        size:
          type: string
          description: Output aspect ratio.
          enum:
            - auto
            - '1:1'
            - '3:2'
            - '2:3'
            - '4:3'
            - '3:4'
            - '5:4'
            - '4:5'
            - '16:9'
            - '9:16'
            - '2:1'
            - '1:2'
            - '21:9'
            - '9:21'
          default: '1:1'
        resolution:
          type: string
          description: Output resolution tier.
          enum:
            - 1k
            - 2k
            - 4k
          default: 2k
        image_urls:
          type: array
          description: >-
            Optional reference images. Supports remote URLs and base64 data
            URIs.
          maxItems: 16
          items:
            type: string
    ImageGenerationResponse:
      type: object
      properties:
        created:
          type: integer
          example: 1712345678
        data:
          type: array
          items:
            type: object
            properties:
              url:
                type: string
                format: uri
              b64_json:
                type: string
    ErrorResponse:
      type: object
      properties:
        type:
          type: string
          example: error
        error:
          $ref: '#/components/schemas/ErrorDetail'
    ErrorDetail:
      type: object
      properties:
        type:
          type: string
          example: invalid_request_error
        message:
          type: string
          example: Invalid request body.
  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer
      bearerFormat: API Key
      description: Paste your SmewAI API key as a Bearer token.

````