# Content SDK

> Read your published pages in any JavaScript app, typed from your content types.

## Install and generate types

```bash
npm i lefine
npx lefine login
npx lefine content types
```

`content types` writes `lefine-content.ts`: one type per content type, and `lefineContent` with your project's ids (`ws_...`, `prj_...`) and languages. Run it again after you change a content type.

## Read pages

Create the client once, at module level:

```ts
// lib/content.ts
import { createContentClient } from "lefine/content";
import { lefineContent, type LefineContent } from "../lefine-content";

export const content = createContentClient<LefineContent>(lefineContent);
```

```ts
const posts = await content.list("posts", { locale: "de" });
const post = await content.get("posts", { locale: "pt-BR", slug: "hello" });

post?.data.title; // typed from your content type
```

A page has `id`, `slug`, `publishedAt`, `data`, `locale`, `isFallback` and `fallbacks`. `get` returns `null` when the page is not live.

| Method | Returns |
|---|---|
| `list(type, { locale })` | every live page of a content type |
| `get(type, { locale, slug })` | one page, or `null` (`id` works too) |
| `languages()` | languages with at least one live page |
| `localesOf(type, { slug })` | the languages one page is live in, for hreflang |

## Preview drafts

On the server only, with a key that has **Content** read access:

```ts
import { draftMode } from "next/headers";
import { createContentClient } from "lefine/content";
import { lefineContent, type LefineContent } from "../lefine-content";
import { content } from "@/lib/content";

const { isEnabled } = await draftMode();
const reader = isEnabled
  ? createContentClient<LefineContent>({ ...lefineContent, preview: { key: process.env.LEFINE_PREVIEW_KEY! } })
  : content;
```

In preview, `list` reads at most 50 pages.

## Not yet

- Globals are not delivered.
- A **File or image** field holds a media id; there is no URL helper yet.

Source: https://lefine.ai/en/docs/content/sdk/
