# Generated types

Download TypeScript types for your project's content model, see what the file contains and use it with the JavaScript client.

Mojimoto can write the TypeScript types for your content model for you. One request returns a `.ts` file with a type for every content type, every blocks field and every block, so your editor completes field names and a mistyped one fails the build.

The file is built from the content model as it is at the moment you ask. Download it again whenever the model changes.

## Download the file

There are two ways to get it, and both return the same file.

### With the CLI

```bash
npx @mojimoto/cli types \
  --endpoint https://mojimoto.com/api/v1 \
  --project acme \
  --token mjmt_your_read_token
```

```text
✓ Wrote mojimoto.generated.ts for project "acme" (83 lines).
```

Add `-o types/mojimoto.ts` to write it somewhere else. Every flag is on the [CLI](/docs/tools/cli) page.

### With the types endpoint

```http
GET /api/v1/{project}/types
```

```bash
curl -H "Authorization: Bearer mjmt_your_read_token" \
  https://mojimoto.com/api/v1/acme/types -o mojimoto.generated.ts
```

Any token that can read will do, including the project's public read token. The response is the file itself, sent as `application/typescript` with the filename `mojimoto.generated.ts`. Without a token the answer is `401`.

The **API keys** page of your project shows this `curl` command with your project id and public read token already filled in, under **Generate typed SDK**.

## What the file contains

This is the whole file for the sample site a new project can start with (see the [Quickstart](/docs/get-started/quickstart)): four content types, one of them with a blocks field.

```ts mojimoto.generated.ts
// Generated by Mojimoto - do not edit by hand.
// Project: acme

export interface MojimotoDocument<T extends string, D> {
  id: number
  uid: string | null
  type: T
  lang: string
  status: "draft" | "published"
  published_at: string | null
  updated_at: string
  data: D
}

// A resolved reference is the linked entry; unresolved, just its id + type.
export type MojimotoLink = MojimotoAnyDocument | { id: number; type: string; uid: string | null }

// A media field: the linked asset (url, alt, dimensions) plus how it
// should sit in its frame. id/alt/dimensions are null for legacy
// url-only values that predate asset linking.
export type MojimotoMedia = {
  id: number | null
  url: string
  alt: string | null
  width: number | null
  height: number | null
  content_type: string | null
  fit: "cover" | "contain"
}

// One block in a blocks field. Same shape as a document, so slice
// zones render blocks and referenced sections alike.
export type MojimotoBlock<T extends string = string, D = Record<string, unknown>> = {
  id: string
  type: T
  data: D
}

export type AuthorData = {
  name: string
  slug: string
  role: string
}

export type PageData = {
  title: string
  slug: string
  sections: PageSectionsBlock[]
}

export type PageSectionsHeroBlockData = {
  heading: string
  text: string
}

export type PageSectionsTextBlockData = {
  body: unknown
}

export type PageSectionsCallToActionBlockData = {
  label: string
  url: string
}

export type PageSectionsBlock = MojimotoBlock<"hero", PageSectionsHeroBlockData> | MojimotoBlock<"text", PageSectionsTextBlockData> | MojimotoBlock<"call_to_action", PageSectionsCallToActionBlockData>

export type PostData = {
  title: string
  slug: string
  published_on: string
  excerpt: string
  body: unknown
  author: MojimotoLink | null
}

export type SiteSettingsData = {
  site_name: string
  tagline: string
  footer_note: string
}

export type MojimotoAnyDocument = MojimotoDocument<"author", AuthorData> | MojimotoDocument<"page", PageData> | MojimotoDocument<"post", PostData> | MojimotoDocument<"site_settings", SiteSettingsData>;
```

### The shared types

The first four types are the same in every project:

| Type | What it describes |
| --- | --- |
| `MojimotoDocument<T, D>` | One delivered entry: its `id`, `uid`, `type`, `lang`, `status`, dates and `data`. `T` is the content type's api id and `D` the type of its fields. |
| `MojimotoLink` | The value of a reference field: the linked entry, or the stub `{ id, type, uid }` it collapses to when it is beyond the requested depth or has nothing published. |
| `MojimotoMedia` | The value of a media field: the asset's `url`, `alt`, size and file type, plus the `fit` chosen for it. |
| `MojimotoBlock<T, D>` | One block of a blocks field: an `id`, the block type and its fields. |

### The types for your model

After those come the types made from your content model:

- **One `Data` type per content type**, in alphabetical order of api id. The name is the api id in PascalCase plus `Data`, so `site_settings` becomes `SiteSettingsData`. It lists every field of the type across all its tabs.
- **For each blocks field**, a union named after the content type and the field, plus `Block`: the `sections` field on `page` becomes `PageSectionsBlock`. Each block type gets its own data type, such as `PageSectionsHeroBlockData`.
- **`MojimotoAnyDocument`**, the union of every content type's document. A project with no content types gets `never`.

### How each field type is typed

| Field type | TypeScript type |
| --- | --- |
| `symbol`, `text`, `slug`, `select`, `date` | `string` |
| `number` | `number` |
| `boolean` | `boolean` |
| `rich_text` | `unknown` |
| `media` | `MojimotoMedia \| null` |
| `reference` | `MojimotoLink \| null` |
| `references` | `MojimotoLink[]` |
| `location` | `{ lat: number; lon: number } \| null` |
| `json` | `Record<string, unknown>` |
| `blocks` | An array of that field's block union, such as `PageSectionsBlock[]` |

Three things the types do not tell you:

- **Whether a field is filled.** A `symbol` field is typed `string` whether or not it is required, and an entry that never had a value for it has no such key in `data`. Check before you use a field an editor may have left empty.
- **The options of a select field.** It is typed `string`, not as a union of its options.
- **Which types a reference allows.** `MojimotoLink` covers every content type in the project, so narrow it by `type` before reading its `data`.

## Use the types with the client

Every method of `@mojimoto/client` takes the document type as a type argument. Pair `MojimotoDocument` with a `Data` type from the file:

```ts app.ts
import { createClient, type MojimotoDocument } from '@mojimoto/client'
import type { PostData } from './mojimoto.generated'

type Post = MojimotoDocument<'post', PostData>

const cms = createClient({
  endpoint: 'https://mojimoto.com/api/v1',
  project: 'acme',
  token: process.env.MOJIMOTO_TOKEN!,
})

const posts = await cms.all<Post>({ type: 'post' })

posts[0].data.title // string
posts[0].data.titel // error: Property 'titel' does not exist on type 'PostData'
```

### Rich text

A `rich_text` field is typed `unknown`. Cast it to `MojiRichText`, which the client exports:

```ts
import type { MojiRichText } from '@mojimoto/client'

const body = posts[0].data.body as MojiRichText
```

Rendering it is covered on the [Rich text](/docs/frameworks/rich-text) page.

### References

A reference is the linked entry or a stub, so check which one you have:

```ts
const author = posts[0].data.author

if (author && 'data' in author && author.type === 'author') {
  author.data.name // string
}
```

### Blocks

Each block has a `type`, and checking it narrows `data` to that block's fields:

```ts
import type { PageSectionsBlock } from './mojimoto.generated'

function headingOf(section: PageSectionsBlock): string | null {
  if (section.type === 'hero') {
    return section.data.heading
  }

  return null
}
```

More on the client itself is on the [JavaScript client](/docs/frameworks/javascript) page.

## Regenerate when the model changes

The file only changes when the content model changes: a type or a field added, removed, renamed or given a different field type. When that happens, run the command again and commit the result, or have your build fetch it so the two cannot drift.

```bash
npm install --save-dev @mojimoto/cli
```

```json package.json
{
  "scripts": {
    "cms:types": "mojimoto types -o types/mojimoto.ts",
    "prebuild": "mojimoto types -o types/mojimoto.ts"
  }
}
```

The scripts read `MOJIMOTO_ENDPOINT`, `MOJIMOTO_PROJECT` and `MOJIMOTO_TOKEN` from the environment, so set those three where the build runs.

## Other ways to read the content model

- `GET /api/v1/{project}/content-types` returns the model as JSON, with every field's settings. It is part of the [Content Delivery API](/docs/api/delivery).
- The [MCP server](/docs/ai/mcp) serves this same TypeScript file to coding agents as the `mojimoto://content-model` resource.
- `npx @mojimoto/cli init` saves it into a Claude Code skill. See the [CLI](/docs/tools/cli) page.
