Grok Imagine 2.0 Ext
Grok Imagine 2.0 Ext Layers and Region Editing
Use segment to retrieve object layers and precise masks, then edit polygons, boxes, or detected objects with region_edit.
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.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
UseAuthorization: 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 HTTP200 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.
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
- Fetch layers
- Cache probe
cache_status (hit or miss) or from_cache; do not infer a hit from cached.
Completed response
Forsegment, 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:
mask_rle.counts values to logs, analytics, URLs, or error reporting.
Convert masks to precise selections
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.
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.
Selection methods
- Precise polygon
- Normalized box
- Pixel box
- Object index
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
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_edittask ID assource_task_id - Edit again: use the newly returned
image_id - Never pass
image_idtosegment, and never keep editing the previous image ID.
Error handling
Billing
segmentis free and completes withcost=0andcredits_cost=0, but still requires authentication and a valid source task.region_editis paid. Use the completed task’scostandcredits_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_idtosegment; do not sendimage_idorimage_index. - Use segment’s
image_idforregion_editand provide at least one selection method. - Use
selection_regionsfor precise production editing;object_indicesis only a box approximation. - Always parse
mask_sizeas[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.