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

# Edit

> Edit and repaint image areas

## Headers

| Header      | Type   | Required | Description  |
| ----------- | ------ | -------- | ------------ |
| `x-api-key` | string | Yes      | Your API key |

## Request Body

| Parameter     | Type                    | Required | Description                                    |
| ------------- | ----------------------- | -------- | ---------------------------------------------- |
| `jobId`       | string                  | Yes      | ID of the original image generation task       |
| `imageNo`     | integer                 | Yes      | Image number to edit (0/1/2/3)                 |
| `canvas`      | [Canvas](#canvas)       | Yes      | Target canvas dimensions                       |
| `imgPos`      | [CanvasImg](#canvasimg) | Yes      | Image position relative to canvas              |
| `remixPrompt` | string                  | Yes      | Text prompt for the edit                       |
| `mask`        | [Mask](#mask)           | No       | Areas to repaint on the original image         |
| `callback`    | string                  | No       | Callback URL for task completion notifications |

## Data Types

### Canvas

| Field    | Type    | Description             |
| -------- | ------- | ----------------------- |
| `width`  | integer | Canvas width in pixels  |
| `height` | integer | Canvas height in pixels |

### CanvasImg

| Field    | Type    | Description                            |
| -------- | ------- | -------------------------------------- |
| `width`  | integer | Image width in pixels                  |
| `height` | integer | Image height in pixels                 |
| `x`      | integer | Horizontal offset from canvas top-left |
| `y`      | integer | Vertical offset from canvas top-left   |

### Mask

| Field   | Type                   | Description                               |
| ------- | ---------------------- | ----------------------------------------- |
| `areas` | [Polygon](#polygon)\[] | Polygonal areas to repaint (optional)     |
| `url`   | string                 | Black and white mask image URL (optional) |

### Polygon

| Field    | Type       | Description                                                 |
| -------- | ---------- | ----------------------------------------------------------- |
| `width`  | integer    | Image width in pixels (500-4096)                            |
| `height` | integer    | Image height in pixels (500-4096)                           |
| `points` | integer\[] | Polygon coordinates in XYXY format, clockwise from top-left |

## Example Request

<CodeGroup>
  ```bash cURL theme={null}
  curl -X POST "https://api.legnext.ai/api/v1/edit" \
    -H "x-api-key: YOUR_API_KEY" \
    -H "Content-Type: application/json" \
    -d '{
      "jobId": "c6821434-5e41-408f-b42b-2562306b109c",
      "imageNo": 1,
      "canvas": {
          "width": 1024,
          "height": 1024
      },
      "imgPos": {
          "width": 512,
          "height": 512,
          "x": 256,
          "y": 256
      },
      "remixPrompt": "A beautiful landscape with mountains",
      "mask": {
          "areas": [
              {
                  "width": 500,
                  "height": 500,
                  "points": [10, 10, 10, 100, 100, 100, 100, 10]
              }
          ]
      }
    }'
  ```

  ```python Python theme={null}
  import requests

  url = "https://api.legnext.ai/api/v1/edit"
  headers = {
      "x-api-key": "YOUR_API_KEY",
      "Content-Type": "application/json"
  }
  data = {
      "jobId": "c6821434-5e41-408f-b42b-2562306b109c",
      "imageNo": 1,
      "canvas": {
          "width": 1024,
          "height": 1024
      },
      "imgPos": {
          "width": 512,
          "height": 512,
          "x": 256,
          "y": 256
      },
      "remixPrompt": "A beautiful landscape with mountains",
      "mask": {
          "areas": [
              {
                  "width": 500,
                  "height": 500,
                  "points": [10, 10, 10, 100, 100, 100, 100, 10]
              }
          ]
      }
  }

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

  ```javascript JavaScript theme={null}
  const response = await fetch('https://api.legnext.ai/api/v1/edit', {
    method: 'POST',
    headers: {
      'x-api-key': 'YOUR_API_KEY',
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      jobId: 'c6821434-5e41-408f-b42b-2562306b109c',
      imageNo: 1,
      canvas: {
          width: 1024,
          height: 1024
      },
      imgPos: {
          width: 512,
          height: 512,
          x: 256,
          y: 256
      },
      remixPrompt: 'A beautiful landscape with mountains',
      mask: {
          areas: [
              {
                  width: 500,
                  height: 500,
                  points: [10, 10, 10, 100, 100, 100, 100, 10]
              }
          ]
      }
    })
  });

  const result = await response.json();
  console.log(result);
  ```
</CodeGroup>

## Response

Returns a task object containing the task information.

```json theme={null}
{
    "job_id": "4017934e-35dd-4640-9585-68c224a66f3b",
    "model": "midjourney",
    "task_type": "edit",
    "status": "pending",
    "config": {
        "service_mode": "public",
        "webhook_config": {
            "endpoint": "",
            "secret": ""
        }
    },
    "input": null,
    "output": {
        "image_url": "",
        "image_urls": null,
        "seed": ""
    },
    "meta": {
        "created_at": "",
        "started_at": "",
        "ended_at": "",
        "usage": {
            "type": "point",
            "frozen": 120,
            "consume": 0
        }
    },
    "detail": null,
    "logs": [],
    "error": {
        "code": 0,
        "raw_message": "",
        "message": "",
        "detail": null
    }
}
```

<Note>
  This is the initial success response. Use the `job_id` to check the task status via the [Get Task](/api-reference/task-management/get-task) endpoint to retrieve the completed result with image URLs.
</Note>


## OpenAPI

````yaml POST /v1/edit
openapi: 3.0.3
info:
  title: Legnext AI API
  description: >
    Comprehensive API for professional image and video generation and
    manipulation using Midjourney AI models.


    ## Authentication

    All API requests require an API key passed in the `x-api-key` header.


    ## Base URL

    `https://api.legnext.ai/api`


    ## Rate Limiting

    API requests are rate limited based on your subscription plan.


    ## Webhook Support

    Most endpoints support optional callbacks. When a callback URL is provided,
    the API will POST the completed job result to that URL.
  version: 1.0.0
  contact:
    name: Legnext Support
    email: support@legnext.ai
    url: https://legnext.ai
  license:
    name: Apache 2.0
    url: https://www.apache.org/licenses/LICENSE-2.0.html
servers:
  - url: https://api.legnext.ai/api
    description: Production server
security:
  - ApiKeyAuth: []
tags:
  - name: Image Generation
    description: Text to image, image editing, and manipulation endpoints
  - name: Video Generation
    description: Video creation and enhancement endpoints
  - name: Image Enhancement
    description: AI-powered image upscaling and enhancement using Topaz Labs
  - name: Task Management
    description: Job status tracking and retrieval
  - name: Account
    description: Account information and active tasks management
paths:
  /v1/edit:
    post:
      tags:
        - Image Generation
      summary: Edit Image
      description: >-
        Edit and repaint specific areas of existing images with precision
        control
      operationId: editImage
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/EditRequest'
      responses:
        '200':
          description: Edit task created successfully
          content:
            application/json:
              schema:
                $ref: 6d781e11-1d35-4b14-b58a-515c92e0dd0e
        '400':
          description: Bad Request - Invalid request parameters or malformed request body
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
              example:
                code: 400
                message: Invalid request parameters
                raw_message: Invalid request parameters
                detail: null
        '401':
          description: Unauthorized - Invalid or missing API key
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
              example:
                code: 401
                message: Failed to verify api key
                raw_message: ''
                detail: null
        '402':
          description: >-
            Payment Required - Insufficient credits in user account or provider
            account issue
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
              example:
                code: 402
                message: insufficient quota
                raw_message: insufficient quota
                detail: null
        '403':
          description: Forbidden - Sensitive content detected or permission denied
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
              example:
                code: 403
                message: Forbidden - Sensitive content detected
                raw_message: Forbidden - Sensitive content detected
                detail: null
        '413':
          description: Payload Too Large - File size exceeds maximum limit
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
              example:
                code: 413
                message: File size exceeds maximum limit
                raw_message: file too large
                detail: null
        '422':
          description: Unprocessable Entity - Invalid parameter values or validation failed
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
              example:
                code: 422
                message: Invalid parameter values
                raw_message: Invalid parameter values
                detail: null
        '429':
          description: Too Many Requests - Rate limit exceeded (automatically retried)
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
              example:
                code: 429
                message: rate limit exceeded, please retry later
                raw_message: rate limit exceeded, please retry later
                detail: null
        '500':
          description: Internal Server Error - Server-side error (automatically retried)
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
              example:
                code: 500
                message: Internal server error
                raw_message: Internal server error
                detail: null
      security:
        - ApiKeyAuth: []
components:
  schemas:
    EditRequest:
      type: object
      required:
        - jobId
        - imageNo
        - canvas
        - imgPos
        - remixPrompt
      properties:
        jobId:
          type: string
          format: uuid
          description: ID of the original image generation task
        imageNo:
          type: integer
          minimum: 0
          maximum: 3
          example: 0
          description: Image number to edit (0, 1, 2, or 3)
        canvas:
          type: object
          required:
            - width
            - height
          properties:
            width:
              type: integer
              description: Canvas width in pixels
            height:
              type: integer
              description: Canvas height in pixels
          description: Target canvas dimensions
        imgPos:
          type: object
          required:
            - width
            - height
            - x
            - 'y'
          properties:
            width:
              type: integer
              description: Image width in pixels
            height:
              type: integer
              description: Image height in pixels
            x:
              type: integer
              description: Horizontal offset from canvas top-left
            'y':
              type: integer
              description: Vertical offset from canvas top-left
          description: Image position relative to canvas
        remixPrompt:
          type: string
          minLength: 1
          maxLength: 8192
          description: Text prompt for the edit
        callback:
          type: string
          format: uri
          description: Callback URL for task completion notifications
    Error:
      type: object
      properties:
        code:
          type: integer
          description: Error code
          example: 400
        message:
          type: string
          description: Human-readable error message
          example: Invalid request parameters
        raw_message:
          type: string
          description: Raw error message from service
          example: image_url must start with http:// or https://
        detail:
          type: object
          nullable: true
          description: Additional error details
          example: null
  securitySchemes:
    ApiKeyAuth:
      type: apiKey
      in: header
      name: x-api-key
      description: API key for authentication

````