Skip to main content
POST
Both segment and region_edit use the existing asynchronous image endpoint. Save the returned task_id, then poll Get task status; the create request does not return final layers or images.
Never expose an API key in a browser bundle, LocalStorage, a URL, or frontend logs. Call APIMart through your backend or BFF.

Operation overview

source_task_id and image_id are not interchangeable. segment takes the source task ID; region_edit takes the image asset ID. To segment an edited image, use the completed region_edit task ID as the next source_task_id.

Request headers

Use Authorization: Bearer <APIMART_API_KEY>, Content-Type: application/json, and Accept: application/json. Idempotency-Key is optional and strongly recommended for paid region_edit requests. It accepts 1–191 visible ASCII characters; UUID is recommended. Use a new key for each new logical operation. A network retry of the same request must reuse the original key and identical body. If a paid edit has an indeterminate result, do not retry automatically with a new key.

Asynchronous task flow

A successful create request returns HTTP 200 and data[0].task_id. Poll GET /v1/tasks/{task_id}?language=en every 2 seconds, back off to at most 5 seconds, and set a 10-minute overall timeout. Stop old polling when the source image changes.
A task query can return HTTP 200 while data.status is failed. Always determine success from data.status and display data.error when present.

segment

Request parameters

segment does not need prompt. Do not send image_id, image_index, billing_model_name, n, size, or response_format. cache_only=true and refresh=true are mutually exclusive.

Request examples

A cache miss is still a successful task. Use cache_status (hit or miss) or from_cache; do not infer a hit from cached.

Completed response

For segment, data.result is the segmentation result directly; it is not wrapped in images.
An object without a valid mask_rle or mask_url can only use approximate box editing.

Decode mask_rle

mask_rle.counts is a COCO compressed-count string, not Base64 or zlib. It expands in column-major order; the first run is background, followed by alternating foreground and background runs. The following TypeScript converts it into a browser-friendly row-major binary mask:
Decode large masks in a Web Worker. Never send complete mask_rle.counts values to logs, analytics, URLs, or error reporting.

Convert masks to precise selections

Trace connected components and holes, simplify the contours, and normalize every point to 0–1. Each ring needs at least 3 distinct points, must have non-zero area, and must not self-intersect. Keep at most 16 largest regions per layer and 400 points per ring.
mask_size is [height,width] and uses source-mask coordinates, not CSS display coordinates. With object-fit: contain, subtract letterbox offsets, scale against the actual drawn area, and clamp the final values to 0–1.
Reading source-image or mask_url pixels requires CORS. Set crossOrigin = "anonymous" before src, or fetch a Blob. Decoding mask_rle directly avoids this dependency.

Edit a region: region_edit

Request parameters

At least one of selection_regions, boxes, or object_indices must be non-empty. The API accepts combinations, but the frontend should use one method per request.
Do not send billing_model_name, size, aspect_ratio, source_aspect_ratio, source_size, or image_urls. Omit n or set it to 1; omit claim_asset or set it to false; omit response_format or set it to url. Base64 output and stream=true are unsupported.

Selection methods

points can be flat or nested pairs. Every value must be finite and within 0–1; each ring needs at least 3 coordinate pairs.

Completed response

Prefer result.images[0].items[0]. For legacy responses, pair url[0] with image_ids[0] only when the arrays have equal lengths. Continue only after obtaining both an HTTP(S) URL and a new image_id. Use expires_at as the source of truth for URL expiry; do not hard-code a number of hours. Download or persist assets needed for long-term display.

Continuous editing

After an edit completes, update the displayed URL, the current asset ID, and the source task ID together, then clear old layers and polling state.
  • Segment again: use this region_edit task ID as source_task_id
  • Edit again: use the newly returned image_id
  • Never pass image_id to segment, and never keep editing the previous image ID.

Error handling

Billing

  • segment is free and completes with cost=0 and credits_cost=0, but still requires authentication and a valid source task.
  • region_edit is paid. Use the completed task’s cost and credits_cost; do not hard-code prices in the frontend.
  • Never send the internal field billing_model_name.

Frontend checklist

  • Keep the API key only in the backend or BFF.
  • Send only source_task_id to segment; do not send image_id or image_index.
  • Use segment’s image_id for region_edit and provide at least one selection method.
  • Use selection_regions for precise production editing; object_indices is only a box approximation.
  • Always parse mask_size as [height,width] and account for display scaling and letterboxing.
  • Reuse the original idempotency key for the same network retry and validate both the output URL and new image_id.