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

# Types and schema

> TypeScript types and zod schemas for every response, from the @depict-ai/v4-types package, and your store's own card type from GET /v4/{merchant}/schema.

The response contract is published as the npm package [`@depict-ai/v4-types`](https://www.npmjs.com/package/@depict-ai/v4-types). It has TypeScript types and zod schemas for every request and response, and a JSON Schema for other languages. Your store's [merchant attributes](https://docs.depict.ai/api-v4/attributes.md) differ from other stores', so the package can also build your store's own card type from `GET /v4/{merchant}/schema`.

## Install

```bash theme={null}
npm install @depict-ai/v4-types
```

The package is ESM only and depends on `zod` 4. It is MIT licensed.

## Response types

Each endpoint has a request and a response schema, named after it: `BrowseRequest` and `BrowseResponse`, `ProductsResponse`, `ContextResponse`, `RecommendationsResponse`, `SuggestResponse`, `ListingResponse`, `ListingTreeResponse`, `SchemaResponse` and `EventsResponse`. Errors are `ErrorResponse`. Each schema is also a type of the same name.

```ts theme={null}
import { BrowseResponse, CONTRACT_HEADER, CONTRACT_VERSION } from "@depict-ai/v4-types";

const res = await fetch("https://api.depict.ai/v4/acme/browse?market=se&query=jacket");
const page = BrowseResponse.parse(await res.json());
console.log(res.headers.get(CONTRACT_HEADER), CONTRACT_VERSION); // "6.1" "6.1"
```

* `Product` and `Variant` are the [product card](https://docs.depict.ai/api-v4/contract.md#product) and its [variants](https://docs.depict.ai/api-v4/contract.md#variant).
* `endpoints` lists every endpoint's method, path, and request and response schemas.
* `CONTRACT_VERSION` is the [contract version](https://docs.depict.ai/api-v4/contract.md#versioning) the package follows. The package's major and minor version match it, so `6.1.x` follows contract `6.1`.
* Response schemas accept fields they don't know, so a client built on `6.1` keeps working when the API adds fields.
* Without TypeScript, use the JSON Schema (draft 2020-12) in the package: `import schema from "@depict-ai/v4-types/schema.json" with { type: "json" }`. Every request and response is under `$defs`.

## Store schema

`GET /v4/{merchant}/schema` lists your store's [declared attributes](https://docs.depict.ai/api-v4/attributes.md#declared-attributes). These are the attributes your cards are built with. It takes no parameters.

```http theme={null}
GET /v4/acme/schema
```

```json theme={null}
{
  "request_id": "...",
  "contract_version": "6.1",
  "attributes": [
    {
      "key": "add_to_cart_id",
      "field": "str_add_to_cart_id",
      "level": "variant",
      "type": "string",
      "required": true,
      "pattern": "^[0-9]+-[0-9]+$",
      "role": "add_to_cart"
    },
    {
      "key": "designer_tag",
      "field": "str_designer_tag",
      "level": "product",
      "type": "string",
      "required": false
    }
  ]
}
```

| Field | Always there | Notes |
| - | - | - |
| `key` | yes | The attribute's name on the card, under `attributes` |
| `field` | yes | Where Depict stores the value: a type prefix (`str`, `num`, `bol` or `strs`), then `_`, then the `key`. You don't need it to read cards |
| `level` | yes | `variant`: the attribute is on each variant's `attributes`. `product`: it is on the card's `attributes` |
| `type` | yes | `string`, `number`, `boolean` or `string_list` |
| `required` | yes | Whether every variant, or every card for a `product` attribute, carries a valid value. See [Required attributes](https://docs.depict.ai/api-v4/attributes.md#required-attributes) |
| `pattern` | no | A regular expression every value matches, such as your cart's id format |
| `role` | no | `add_to_cart` on your add-to-cart id. At most one attribute has it, and it is always required |

* `attributes` is `[]` when your store has no declared attributes. Its cards are then the plain [`Product`](https://docs.depict.ai/api-v4/contract#product).
* The list changes only when Depict changes your store's attributes. Call this endpoint when you build your storefront or generate code, not on every page load.
* An unknown store is a 404 `unknown_merchant`, as on every endpoint.

## productSchemaFor

`productSchemaFor(attributes)` returns your store's card schema. It is the `Product` schema with your declared attributes added, and it fails a card that lacks a required attribute or has a value that breaks its `pattern`:

```ts theme={null}
import { productSchemaFor, SchemaResponse } from "@depict-ai/v4-types";

const schema = SchemaResponse.parse(
  await (await fetch("https://api.depict.ai/v4/acme/schema")).json()
);
const AcmeProduct = productSchemaFor(schema.attributes);

const card = AcmeProduct.parse(page.products[0]);
```

`responseSchemasFor(attributes)` returns every endpoint's response schema with your card in `browse`, `products` and `recommendations`.

`productSchemaFor` checks values while your code runs, but its type is the plain `Product`. To have your attributes typed too, generate a module.

## Generate a typed module

> [!NOTE]
>
> From `@depict-ai/v4-types` 6.1.0.

The package's command line tool reads `GET /v4/{merchant}/schema` and writes a TypeScript module for your store:

```bash theme={null}
npx @depict-ai/v4-types generate --merchant acme --out src/depict
```

| Option | Default | Notes |
| - | - | - |
| `--merchant` | | Your store's name, as in `/v4/{merchant}/...`. Required |
| `--out` | the current directory | The directory to write `<merchant>.ts` to |
| `--base-url` | `https://api.depict.ai` | The API to read from |

For the store above, `src/depict/acme.ts` is:

```ts theme={null}
// Generated by `npx @depict-ai/v4-types generate --merchant acme` from
// GET /v4/acme/schema (contract 6.1). Do not edit: run the command again
// when the store's attributes change.
import * as z from "zod";
import { productWith, type MerchantAttributeDeclaration } from "@depict-ai/v4-types";

/** The attributes acme's cards carry: the input to `productSchemaFor(declarations)`. */
export const declarations: MerchantAttributeDeclaration[] = [
  // ... as GET /v4/acme/schema lists them
];

/**
 * acme's card: `Product` with the declared attributes typed. It checks what
 * `productSchemaFor(declarations)` checks: a required attribute is on every variant (or card).
 */
export const AcmeProduct = productWith({
  product: z
    .looseObject({
      designer_tag: z.string().min(1).optional(),
    })
    .optional(),
  variant: z.looseObject({
    add_to_cart_id: z.string().min(1).regex(/^[0-9]+-[0-9]+$/),
  }),
});
export type AcmeProduct = z.infer<typeof AcmeProduct>;
```

Use it in place of `Product`:

```ts theme={null}
import { AcmeProduct } from "./depict/acme";

const card = AcmeProduct.parse(page.products[0]);
for (const variant of card.variants) {
  addToCartIds.push(variant.attributes.add_to_cart_id); // a string, on every variant
}
```

* The tool needs Node 18 or later. It installs nothing besides the package and `zod`.
* Commit the generated file, and run the command again when Depict changes your store's attributes.
* When the store is unknown, the API can't be reached or the answer isn't a schema, the tool prints one line saying what the API answered and exits with code 1.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.