# Prepare a model through the API

URL: https://docs.glossi.io/api/model-prep

Split a one-mesh model into parts, group parts by finish, unwrap UVs, assign materials, or make a variant that matches a photo, then keep or discard the result, through the REST API or an agent.

[Model prep](https://docs.glossi.io/bring-in-your-product/prepare-a-model.md) gets a model ready for materials and Studio. The REST API and the agent connector run the same agent as the model page's **Actions** tab, and a run started here appears there.

> **Team plan.** API and agent access are available on the Team plan.

## Access

REST requests use your workspace API key in `X-API-Key` (see [Get started with the API](https://docs.glossi.io/api/getting-started.md)). Paths below are relative to `https://api.glossi.io/api/v1`. Agent (MCP) tools take the same arguments by name (see [Connect an agent](https://docs.glossi.io/agents/connect.md)). Errors use the standard shape, `{ "error": { "code": "...", "message": "..." } }`.

| REST                                        | MCP tool                 |
| ------------------------------------------- | ------------------------ |
| `GET /models/:modelId/prep/offer`           | `get_model_prep_offer`   |
| `POST /models/:modelId/prep`                | `prepare_model`          |
| `POST /models/:modelId/prep/match-photo`    | `match_variant_to_photo` |
| `GET /models/:modelId/prep`                 | `list_model_preps`       |
| `GET /models/:modelId/prep/:jobId`          | `get_model_prep`         |
| `POST /models/:modelId/prep/:jobId/retry`   | `retry_model_prep`       |
| `POST /models/:modelId/prep/:jobId/keep`    | `keep_model_prep`        |
| `POST /models/:modelId/prep/:jobId/discard` | `discard_model_prep`     |

## Passes

A run does one or more passes, always in this order:

| Pass        | What it does                                                                                                                                                                                                                                                                  |
| ----------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `segment`   | Only for a model that is one fused mesh (a scan or an AI-generated model): splits it into its real parts. The look doesn't change.                                                                                                                                            |
| `parts`     | Names every part and groups parts that share a finish, so each finish gets its own material slot. Where one mesh holds several separately modelled pieces with different finishes, it pulls them apart first. This is the pass for regrouping a model that already has parts. |
| `uvs`       | Gives every part even, real-world-scale UVs. Parts with printed artwork keep theirs.                                                                                                                                                                                          |
| `materials` | Picks a library or Glossi catalog material for each finish, and makes a new one when nothing fits.                                                                                                                                                                            |

The model page's **Prepare model** is `["segment", "parts", "uvs", "materials"]` on a one-mesh model and `["parts", "uvs", "materials"]` on a model that already has parts. `refine` isn't started here; it is what `retry` runs.

## What it costs

A run that includes `segment` is a one-off purchase for an uploaded model, paid when it starts; the others use your plan's usage. Models made with AI model creation are prepared at no extra cost. Read the offer before starting a `segment` run:

```bash
curl 'https://api.glossi.io/api/v1/models/MODEL_ID/prep/offer' \
  -H "X-API-Key: $GLOSSI_API_KEY"
```

```json
{
  "offer": {
    "billing": "PURCHASE",
    "available": true,
    "unavailableReason": null,
    "mode": "CARD",
    "price": { "subtotalMinor": 1500, "totalMinor": 1500, "currency": "USD", "taxTreatment": "STRIPE_AUTOMATIC" },
    "includedRefines": 3,
    "discards": { "max": 0, "left": 0, "next": "PREPAY", "freesAt": null },
    "canApprove": true,
    "cardSetup": { ... },
    "partCount": 24
  }
}
```

`billing` is `PURCHASE` for an uploaded model or `INCLUDED` for one made by AI model creation. When a full prep can't start here, `available` is `false` and `unavailableReason` says why; that includes workspaces billed on an organization invoice, which can't start a full prep yet. Amounts are in minor units (cents). `includedRefines` is how many changes the purchase covers before you keep it. `discards.next` is `PREPAY` when a new run is paid when it starts and isn't refunded on discard. Some organizations' agreements include free discards: then `discards.left` counts them, `next` is `FREE` while any are left, and the result is paid when you keep it. `canApprove` is whether the key's user may pay for a prep: a workspace owner or admin. `cardSetup` describes the workspace's saved card when `mode` is `CARD`, and is `null` otherwise. `partCount` is how many mesh parts the model has, counting one per material on each mesh. `1` usually means one fused mesh, the case `segment` is for; a scan with several textures can show more and still need it. `null` means the model hasn't been converted yet.

## Start a run

```bash
curl -X POST 'https://api.glossi.io/api/v1/models/MODEL_ID/prep' \
  -H "X-API-Key: $GLOSSI_API_KEY" -H 'Content-Type: application/json' \
  -d '{ "passes": ["segment", "parts", "uvs", "materials"], "notes": "The strap and buckle are separate parts" }'
```

| Body            |          |                                                                                                             |
| --------------- | -------- | ----------------------------------------------------------------------------------------------------------- |
| `passes`        | required | Pass names from the table above.                                                                            |
| `notes`         |          | Anything the agent should know, in plain words.                                                             |
| `referenceKeys` |          | Up to 6 photos of the real product, each a `referenceKey` from `POST /materials/generate/reference-upload`. |
| `variantName`   |          | With `materials`: also save the picked look as a variant with this name when the result is kept.            |

Returns `202` with `{ "job": { ... } }`. A model must be `READY`, and only one run per model can be open at a time (`409 PREP_IN_PROGRESS`).

## Match a photo

Makes a variant that looks like photos of the product. The agent groups the model's parts by the colour zones the photos show, pulling apart modelled pieces where it needs to but never segmenting, then picks a material for each group, tinted and scaled to match. It runs `parts` and `materials` and uses your plan's usage. A match that finds no materials for the photos fails and uses nothing.

```bash
curl -X POST 'https://api.glossi.io/api/v1/models/MODEL_ID/prep/match-photo' \
  -H "X-API-Key: $GLOSSI_API_KEY" -H 'Content-Type: application/json' \
  -d '{ "referenceKeys": ["REFERENCE_KEY"], "variantName": "Ale brown and forest green" }'
```

| Body            |          |                                                                                        |
| --------------- | -------- | -------------------------------------------------------------------------------------- |
| `referenceKeys` | required | 1 to 6 photos, each a `referenceKey` from `POST /materials/generate/reference-upload`. |
| `variantName`   | required | The variant the result is saved as.                                                    |
| `notes`         |          | Anything the photos don't show, such as which zones are suede.                         |

Follow and review it like any run; its job has `matchesPhoto: true`. Keeping it saves the picks, with their tint, tiling and sheen, as a variant on the regrouped model, whose look is unchanged. Keep it with `asNewModel` when the model already has variants or projects. A zone printed or painted inside one piece can't be pulled apart this way: the result lists it under `uncertain`, and it needs a `segment` run.

## Follow a run

`GET /models/MODEL_ID/prep/JOB_ID` returns the run; `GET /models/MODEL_ID/prep?limit=10` lists the model's runs, newest first. Poll every 30 seconds or so: a full prep takes up to about 40 minutes, a single pass a few minutes.

| `status`                                 |                                                                                  |
| ---------------------------------------- | -------------------------------------------------------------------------------- |
| `AWAITING_PAYMENT`                       | Paid up front and waiting for the payment.                                       |
| `QUEUED`                                 | Waiting for a worker.                                                            |
| `RUNNING`                                | `phase` says where it is: `downloading`, `preparing` or `uploading`.             |
| `CONVERTING`                             | The result is being converted into a version.                                    |
| `READY`                                  | Waiting for you to keep, discard or change it.                                   |
| `NEEDS_INPUT`                            | A change needs an answer first; `question` says what. Answer with `retry`.       |
| `UNCHANGED`                              | A change wasn't made; `note` says why. The result it was about is still `READY`. |
| `KEPT`, `DISCARDED`, `EXPIRED`, `FAILED` | Finished. `error` says why a run failed. An unkept result expires after 30 days. |

A `READY` run carries the report: `summary`, `uncertain` (what the agent wasn't sure of), `changes`, `partCount`, `finishGroupCount`, `bindingProblems`, `materials`, `refinesLeft` and `paysOnKeep`.

## Change something

```bash
curl -X POST 'https://api.glossi.io/api/v1/models/MODEL_ID/prep/JOB_ID/retry' \
  -H "X-API-Key: $GLOSSI_API_KEY" -H 'Content-Type: application/json' \
  -d '{ "feedback": "The laces include the red strip on the tongue" }'
```

Changes one area of a `READY` result, in your words. A photo match can't be changed (`400 INVALID_GOAL`): discard it and match again. A change to a usage run uses usage. `referenceKeys` takes up to 6 screenshots of the area. It starts a new run whose `previousJobId` is the result it changes. A purchase covers `includedRefines` changes; after that, `409 REFINES_USED`.

## Keep or discard

`POST .../keep` makes the result the model's current version; the previous version stays in its version history. With `{ "asNewModel": true }` it makes the result a model of its own instead (`job.keptModelId`), leaving this model, its projects and its variants as they are; use it when the model already has variants, since regrouping renames material slots. By default a full prep is paid when it starts, so keep charges nothing. When `paysOnKeep` is true (agreements with free discards), keep charges the workspace's saved card and returns `{ "job": { ... }, "payment": { "status": "PAID", ... } }`. A card that needs 3-D Secure returns `REQUIRES_ACTION`: finish it on the model page. `403 APPROVAL_REQUIRED` means the key's user isn't a workspace owner or admin.

`POST .../discard` throws the result away. A prep paid when it started isn't refunded; under an agreement with free discards, it uses one.

## Errors

| Code                                                                                                                                                       | HTTP | When                                                                           |
| ---------------------------------------------------------------------------------------------------------------------------------------------------------- | ---- | ------------------------------------------------------------------------------ |
| `MODEL_NOT_FOUND`, `JOB_NOT_FOUND`                                                                                                                         | 404  | Not in your workspace.                                                         |
| `VALIDATION_ERROR`, `MODEL_NOT_READY`, `INVALID_PASSES`, `INVALID_REFERENCE`, `INVALID_VARIANT_NAME`, `INVALID_GOAL`, `TEXT_TOO_LONG`, `FEEDBACK_REQUIRED` | 400  | Bad input, a model still converting, or a change to a photo match.             |
| `JOB_NOT_READY`, `PREP_IN_PROGRESS`, `REFINES_USED`                                                                                                        | 409  | The run isn't waiting for review, another run is open, or no changes are left. |
| `PAYMENT_REQUIRED`, `USAGE_LIMIT_REACHED`                                                                                                                  | 402  | The card was declined, or the workspace has no usage left.                     |
| `PREP_UNAVAILABLE`, `APPROVAL_REQUIRED`                                                                                                                    | 403  | Model prep isn't available in this workspace, or needs an owner or admin.      |
| `PREP_LIMIT`                                                                                                                                               | 429  | Too many runs at once in this workspace.                                       |
| `KEEP_FAILED`                                                                                                                                              | 500  | The result couldn't be saved. Try keeping it again.                            |
| `NOT_CONFIGURED`                                                                                                                                           | 503  | Model prep isn't running right now. Try again later.                           |