Vai al contenuto
Docs menuContent / Content SDK
On this page
  1. Install and generate types
  2. Read pages
  3. Preview drafts
  4. Not yet

Content SDK

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

Install and generate types

Terminal
npm i lefinenpx lefine loginnpx 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:

lib/content.ts
// lib/content.tsimport { 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.

MethodReturns
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.
Next step · BrainAsk Brain
Was this page helpful?