# A product page's product, to try on

`POST https://stylor.ai/api/v2/agent/looks/piece`

- Authentication: public API key, sent as `Authorization: Bearer sgpt-pk-…`
- Rate-limit resource: `api.v2.agent.looks.render`
- Demo key: not accepted
- Web page: https://stylor.ai/guides/rest/v2/retrieve-piece

Which catalogue item the product on a store's page is, to try it on.
It is the first call of the storefront's "Try it on" button on a
product's photo: name the product the page shows, and the item's id
comes back with the colour chosen on the page.

Render it on the shopper with `POST /v2/agent/looks`, passing the
`itemId`, `piece: true`, their photo as `userPhotoUrl`, the colour in
`colours` and the `datasetIds` returned here. A piece is never
rendered on a model.

The product is found by the SKU of the variant chosen on the page,
else by its title, in the store the page is on (`store`, as on the
chat) unless `datasetIds` are given. `variant` is that variant's own
title (`Rust / XS`): where part of it is exactly one of the product's
colours, that is the `colour` returned.

It is metered on the looks render resource
(`api.v2.agent.looks.render`) and its unit is always refunded: nothing
is drawn here.

The route answers `OPTIONS` preflight with `204` and allows any
origin, so it can be called directly from a storefront page, as the
looks and pose endpoints can.

## Example request

```bash
curl -X POST "https://stylor.ai/api/v2/agent/looks/piece" \
  -H "Authorization: Bearer $STYLOR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "product": {
    "title": "Top - Cheyenne Western Shirt",
    "sku": "23WS1801-HTB-1",
    "variant": "Horse Trails Buckskin / XS"
  },
  "store": {
    "origin": "https://lovleiranch.com"
  },
  "country": "US"
}'
```

## Request body

Content type `application/json`, required.

- `product` (object, required): The product on the page. A `title` or a `sku` is needed.
  - `title` (string): The product's title, as the store has it.
  - `sku` (string): The SKU of the variant chosen on the page.
  - `variant` (string): That variant's title, its options joined as Shopify writes them, e.g. `Rust / XS`.
  - `colour` (string): Optional. The colour chosen on the page, as the store names it. Looked for first, so a colour written with a stroke of its own (`Cobalt / Sky`) is found; without it the colour is read from `variant`.
- `datasetIds` (array<string>): Optional. Limit the search to these datasets; they win over `store`.
- `store` (object): Optional. The store the page is on, as on the chat.
  - `origin` (string): The page's origin.
  - `shop` (string): Shopify's permanent shop domain.
- `country` (string): Optional. The shopper's two-letter country, for the price and what can be bought there.

### Examples

#### From a product page

The product a Shopify product page shows, in the colour chosen on it.

```json
{
  "product": {
    "title": "Top - Cheyenne Western Shirt",
    "sku": "23WS1801-HTB-1",
    "variant": "Horse Trails Buckskin / XS"
  },
  "store": {
    "origin": "https://lovleiranch.com"
  },
  "country": "US"
}
```

## Responses

### 200

The piece.

Content type `application/json`:

- `piece` (object, required)
  - `itemId` (string, required): The catalogue item the page's product is.
  - `name` (string, required): Its name in the catalogue.
  - `colour` (string, nullable, required): The colour chosen on the page, as the catalogue names it, or `null`. Send it to the looks endpoint in `colours`.
- `datasetIds` (array<string>, required): The datasets of the store the product was found in. Pass them to the looks endpoint with the render.

Example:

```json
{
  "piece": {
    "itemId": "59989728-3920-4be9-9ae3-38031aa72d3f",
    "name": "Top - Cheyenne Western Shirt",
    "colour": "Horse Trails Buckskin"
  },
  "datasetIds": [
    "eabedae0-47a5-4233-a7cc-3ddc01fa4254"
  ]
}
```

## Errors

Every error is a JSON object with an `error` string. Match on the status code; the message is written for people.

| Status | Message | When |
| --- | --- | --- |
| 403 | This store does not offer seeing pieces on your own photo. | "See it on you" is switched off for the store, or for the organization. |
| 404 | This product is not in the catalogue yet, or cannot be bought where you are. | No active item in scope is that product, or it is sold out or not sold in the shopper's country. |
| 422 | product needs a title or a sku. | `product` is missing, or carries neither a `title` nor a `sku`. |

### Shared authentication, quota and rate-limit errors

| Status | Message | When |
| --- | --- | --- |
| 401 | API key missing. | The `Authorization` header is absent, is not `Bearer <key>`, or the key does not have three dash-separated segments. |
| 401 | Authentication failed: public key is invalid or not recognized. | A public-key endpoint was called with a key that does not exist. |
| 401 | Authentication failed: private key provided instead of a public key. | A public-key endpoint was called with an `sgpt-sk-…` key. |
| 401 | Authentication failed: this key's organization no longer exists. | The organization that owned the key has been deleted. |
| 401 | Authentication failed: API key could not be read. Regenerate the key. | The key's embedded organization data could not be decrypted or parsed. |
| 403 | This API key cannot be used from <origin>. Add the website to the key's allowed websites in the Stylor dashboard. | The key is limited to its allowed websites and the request's `Origin` is not one of them. |
| 403 | This API key only accepts requests from its allowed websites. Allow apps and servers on the key, or call from one of its websites. | The key is limited to its allowed websites and the request has no `Origin` (an app or a server). |
| 403 | This API key is for Stylor's own use only. Use another key, or change its public key access in the Stylor dashboard. | The key is set to Stylor's own use and the request does not come from a Stylor site. |
| 403 | Demo mode is not supported on this API route. Check the documentation for available demo endpoints. | The demo key was sent to an endpoint that opts out of demo mode. |
| 403 | No active subscription for this organization. | The organization that owns the key has no active subscription. |
| 403 | No active quota for this organization's plan this billing period. | The organization has no quota record for the current billing period. A failure to read the quota is a `500`, never this. |
| 403 | No entitlement for "<resource>". | The plan's quota template has no entry for this endpoint. |
| 403 | Access denied to "<resource>". | The plan's quota template turns this endpoint off. |
| 429 | Monthly quota exceeded for "<resource>" (<quota>/<quota> used). | The billing-period quota for this endpoint is exhausted. `Retry-After` gives the seconds until the period resets. |
| 429 | RPM limit exceeded for "<resource>" (<used>/<limit> used). | More than the allowed requests per minute were sent. `Retry-After` is 60. |
| 500 | Internal server error. | An unexpected failure on the server. The message is always this string; details are logged, never returned. Quota consumed by the request is refunded, as it is for every 5xx response. |
