> ## 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 edit

> Edit existing images with the OpenAI Images protocol, supporting inpainting and outpainting

This endpoint is compatible with the OpenAI Images protocol and generates a new image from the source images you provide and an edit prompt. It supports inpainting, outpainting, and multi-image fusion scenarios.

## Features

* References source images through URL addresses or file IDs
* Supports inpainting with the `mask` image to specify the edit area
* Supports outpainting with the `background` image for canvas extension
* Supports `input_fidelity` to control input image fidelity (`high` / `low`)
* Supports `png`, `jpeg`, and `webp` output formats with `output_compression` control
* Supports both `url` and `b64_json` response formats
* Supports streaming (SSE) responses with intermediate results pushed via `partial_images`

## 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/edits"
  headers = {
      "Authorization": "Bearer YOUR_API_KEY",
      "Content-Type": "application/json"
  }
  data = {
      "model": "gpt-image-2",
      "images": [
          {"image_url": "https://example.com/images/cat.png"}
      ],
      "prompt": "Replace the background with a beach at sunset, keeping the cat unchanged",
      "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/edits', {
    method: 'POST',
    headers: {
      'Authorization': 'Bearer YOUR_API_KEY',
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      model: 'gpt-image-2',
      images: [
        { image_url: 'https://example.com/images/cat.png' }
      ],
      prompt: 'Replace the background with a beach at sunset, keeping the cat unchanged',
      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/edits' \
    -H 'Authorization: Bearer YOUR_API_KEY' \
    -H 'Content-Type: application/json' \
    -d '{
      "model": "gpt-image-2",
      "images": [
        {"image_url": "https://example.com/images/cat.png"}
      ],
      "prompt": "Replace the background with a beach at sunset, keeping the cat unchanged",
      "size": "1024x1024",
      "quality": "high",
      "n": 1
    }'
  ```
</CodeGroup>

A successful edit returns the image data:

```json theme={null}
{
  "created": 1787875200,
  "data": [
    {
      "url": "https://example.com/images/edited-1.png"
    }
  ],
  "size": "1024x1024",
  "quality": "high",
  "usage": {
    "input_tokens": 3280,
    "output_tokens": 4160,
    "total_tokens": 7440,
    "input_tokens_details": {
      "image_tokens": 3268,
      "text_tokens": 12
    },
    "output_tokens_details": {
      "image_tokens": 4160,
      "text_tokens": 0
    }
  }
}
```

## Next steps

* See [OpenAI image generation](/docs/en/api-reference/images/openai/generation) to generate images from a text prompt
* 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/edit/openapi.json POST /v1/images/edits
openapi: 3.0.1
info:
  title: Default Module
  description: ''
  version: 1.0.0
servers: []
security: []
tags: []
paths:
  /v1/images/edits:
    post:
      tags: []
      summary: OpenAI image edit
      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:
                images:
                  type: array
                  items:
                    $ref: '#/components/schemas/ImageReference'
                    description: |-
                      Image reference DTO.
                      Used to reference input images in image edit requests,
                      supporting references via URL address or file ID.
                  description: Source images to edit
                prompt:
                  type: string
                  description: Edit prompt describing the changes to apply to the image
                background:
                  type: string
                  description: >-
                    Background image data (for outpainting and similar extension
                    scenarios)
                input_fidelity:
                  type: string
                  description: 'Input image fidelity control: high, low'
                mask:
                  $ref: '#/components/schemas/ImageReference'
                  description: Mask image specifying the edit area (inpainting)
                model:
                  type: string
                  description: Model name, e.g. "dall-e-2"
                '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. "standard", "hd"
                size:
                  type: string
                  description: Image size, e.g. "1024x1024"
                stream:
                  type: boolean
                  description: Whether to use streaming (SSE) responses
                user:
                  type: string
                  description: End-user identifier for monitoring and abuse detection
              required:
                - model
                - prompt
                - images
            examples: {}
      responses:
        '200':
          description: ''
          content:
            application/json:
              schema:
                type: object
                properties:
                  created:
                    type: integer
                    description: Image generation timestamp (Unix seconds)
                    format: int64
                  output_format:
                    type: string
                    description: Output image format echo
                  background:
                    type: string
                    description: Background image data echo
                  quality:
                    type: string
                    description: Image quality echo
                  size:
                    type: string
                    description: Image size echo
                  data:
                    type: array
                    items:
                      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.
                      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)
                        url:
                          type: string
                          description: Publicly accessible image URL
                    description: Generated image data list
                  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:
    ImageReference:
      type: object
      properties:
        image_url:
          type: string
          description: Image URL address
        file_id:
          type: string
          description: File ID in the file system (obtained after upload)
    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

````