Skip to main content
The @mdcms/sdk package provides a type-safe TypeScript client for querying content from the MDCMS API. It handles authentication headers, response parsing, pagination, and error handling.

Installation

Client Setup

Create a client instance with your server URL, API key, project, and environment:

Configuration Options


Querying — get()

The get() method retrieves a single document by ID, slug, or path. It returns the full ContentDocumentResponse object.

By ID

By Slug

With Locale and References

get() Options

When using slug or path, the query must match exactly one document. If multiple documents match (e.g., same slug in different locales without specifying locale), the SDK throws an MdcmsClientError with code AMBIGUOUS_RESULT.

Querying — list()

The list() method retrieves a paginated list of documents matching the given filters.

list() Options

Return Type


Response Type

Both get() and list() return ContentDocumentResponse objects with the following fields:

Error Handling

The SDK provides two error classes for distinguishing between server errors and client-side issues:

MdcmsApiError

Thrown when the server returns a non-2xx response. Properties:

MdcmsClientError

Thrown for client-side issues that prevent a valid response. Error codes:

Next.js Integration

The SDK is designed to work seamlessly with Next.js App Router for both static generation and server-side rendering.

Client Setup

Create a shared client instance:

Blog Page Example

Blog Index Example


Draft Preview

Studio mints a short-lived preview token before loading a configured host preview route. The SDK can verify that token and fetch the matching draft document in one call:
For lower-level control, verify the request first and then choose your own draft fetch:
When the preview route renders inside Studio’s iframe, post { type: "mdcms:live-preview-ready" } from client-side code after the page is ready. Studio keeps the pane in a loading state until that message arrives and shows the fallback link if the route never signals readiness.
The server-side API key used for private preview routes needs content:read:draft. Keep that API key and MDCMS_PREVIEW_TOKEN_SECRET in server-only environment variables, force dynamic rendering, and send Cache-Control: private, no-store or the framework equivalent.
Some projects intentionally expose drafts on public or separately protected preview deployments. MDCMS allows that pattern. If unpublished content must stay private, do not treat ?preview=true alone as authorization; require a valid MDCMS preview token, a host session, or another server-side gate before fetching with draft: true.