@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()
Theget() 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
Querying — list()
Thelist() method retrieves a paginated list of documents matching the given filters.
list() Options
Return Type
Response Type
Bothget() 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:{ 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.
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.