SDK: @dotcms/client Library

The @dotcms/client is a powerful JavaScript/TypeScript SDK designed to simplify the integration of dotCMS content into your applications. Whether building dynamic websites or content-driven apps, this SDK offers an intuitive API for seamless and type-safe content retrieval, enabling you to create engaging experiences effortlessly by accessing and displaying content from dotCMS.

When to Use It:#

  • Building headless frontends that need dotCMS content
  • Building content-driven apps that need to access dotCMS content
  • Developing multi-language or personalized experiences
  • Implementing dynamic navigation and page structures

Key Benefits:#

  • Simplified Development: Write less code with intuitive methods and builders
  • Type Safety: Built-in TypeScript definitions prevent runtime errors
  • Universal Compatibility: Works in both browser and Node.js environments
  • Security First: Handles authentication and requests securely
  • Developer Experience: Rich autocompletion and documentation

📋 Migrating from Alpha Version? If you're upgrading from the alpha version of @dotcms/client, please see our Migration Guide for step-by-step instructions and troubleshooting tips.

Getting Started#


Prerequisites & Setup#

Get a dotCMS Environment#

  • Recommended: dotCMS Evergreen
  • Minimum: dotCMS v25.05
  • Best Experience: Latest Evergreen release

Environment Setup#

For Production Use:

For Testing & Development:

For Local Development:

Create a dotCMS API Key#

[!TIP] Make sure your API Token has read-only permissions for Pages, Folders, Assets, and Content. Using a key with minimal permissions follows security best practices.

This integration requires an API Key with read-only permissions for security best practices:

  1. Go to the dotCMS admin panel.
  2. Click on System > Users.
  3. Select the user you want to create the API Key for.
  4. Go to API Access Key and generate a new key.

For detailed instructions, please refer to the dotCMS API Documentation - Read-only token.

Installation#

Install the SDK and required dependencies:

npm install @dotcms/client@latest @dotcms/types@latest

[!TIP] If you are working with pure JavaScript, you can avoid installing the @dotcms/types package.

Your First API Call#

Here's a basic setup of the dotCMS Client SDK to help you get started:

import { createDotCMSClient } from '@dotcms/client';

// Create a client instance
const client = createDotCMSClient({
    dotcmsUrl: 'https://your-dotcms-instance.com',
    authToken: 'your-auth-token', // Optional for public content
    siteId: 'your-site-id' // Optional site identifier
});

// Start using the client!
const { pageAsset } = await client.page.get('/about-us');
console.log(pageAsset.page.title);

Example Projects#

While there isn't a dedicated example project specifically for the client SDK, you can see it in action within these full-stack examples:

These examples demonstrate how to use the client SDK as part of a complete web application.

How-to Guides#


How to Fetch Complete Pages#

The client.page.get() method is your primary tool for retrieving full page content. Here's how to use it effectively:

Basic Page Fetching#

// Fetch a page with its layout and content
const { pageAsset } = await client.page.get('/about-us');
console.log(pageAsset.page.title);

Customizing Page Requests#

// Fetch with specific language and persona
const { pageAsset } = await client.page.get('/about-us', {
    languageId: '2',
    fireRules: true,
    personaId: '1234'
});
// Pull in additional content with GraphQL
const { pageAsset, content } = await client.page.get('/about-us', {
    graphql: {
        content: {
            blogPosts: `
                BlogCollection(limit: 3) {
                    title
                    urlTitle
                }
            `,
            navigation: `
                DotNavigation(uri: "/", depth: 2) {
                    href
                    title
                }
            `
        }
    }
});

How to Query Content Collections#

The client.content.getCollection() method uses a fluent builder pattern for querying content:

Basic Collection Queries#

// Fetch first 10 blog posts
const blogs = await client.content.getCollection('Blog').limit(10).page(1);

Filtering and Sorting#

// Filter by title and sort by date
const filtered = await client.content
    .getCollection('Blog')
    .query((qb) => qb.field('title').equals('dotCMS*'))
    .limit(5)
    .sortBy([{ field: 'publishDate', direction: 'desc' }]);

Complex Queries#

// Multiple filters with operators
const products = await client.content
    .getCollection('Product')
    .query((qb) => qb
        .field('category').equals('electronics')
        .and()
        .field('price').raw(':[100 TO 500]')
        .not()
        .field('discontinued').equals('true')
    )
    .limit(10);

How to Work with GraphQL#

GraphQL allows you to fetch exactly the data you need in a single request:

Fetching All Page Fields#

// Use _map to get all page fields including custom ones
const { pageAsset } = await client.page.get('/about-us', {
    graphql: {
        page: '_map'
    }
});

Querying Relationships#

// Fetch related content using fragments
const { pageAsset } = await client.page.get('/blog-post', {
    graphql: {
        page: `
            containers {
                containerContentlets {
                    contentlets {
                        ... on Blog {
                            author {
                                title
                                email
                            }
                            category {
                                categoryName
                            }
                            tags {
                                tagName
                            }
                        }
                    }
                }
            }
        `
    }
});

Using Variables and Fragments#

// Reusable fragments with variables
const response = await client.page.get('/about-us', {
    graphql: {
        content: {
            blogPosts: `
                BlogCollection(limit: $limit) {
                    ...blogFragment
                }
            `
        },
        fragments: [
            `fragment blogFragment on Blog {
                title
                urlTitle
                blogContent {
                    json
                }
            }`
        ],
        variables: { limit: 5 }
    }
});

API Reference#


Client Initialization#

createDotCMSClient(config: DotCMSClientConfig): DotCMSClient

Parameters#

OptionTypeRequiredDescription
dotcmsUrlstringYour dotCMS instance URL
authTokenstringAuthentication token
siteIdstringSite identifier (falls back to default site if not specified)
requestOptionsRequestOptionsAdditional fetch options

Example#

const client = createDotCMSClient({
    dotcmsUrl: 'https://your-dotcms-instance.com',
    authToken: 'your-auth-token',
    siteId: 'your-site-id'
});

page.get() Method#

get<T extends DotCMSExtendedPageResponse = DotCMSPageResponse>(
  url: string,
  options?: DotCMSPageRequestParams
): Promise<DotCMSComposedPageResponse<T>>

Parameters#

ParameterTypeRequiredDescription
urlstringPage URL path
optionsDotCMSPageRequestParamsRequest customization options

Options#

OptionTypeDescription
languageIdstring | numberLanguage version of the page
modestringRendering mode: LIVE, PREVIEW_MODE
personaIdstringPersonalize content based on persona ID
graphqlDotCMSGraphQLParamsGraphQL options for extending response
fireRulesbooleanWhether to trigger page rules

Example#

const { pageAsset } = await client.page.get('/about-us');

content.getCollection() Method#

getCollection<T = DotCMSBasicContentlet>(
  contentType: string
): CollectionBuilder<T>

Parameters#

ParameterTypeRequiredDescription
contentTypestringContent type variable name

Builder Methods#

MethodArgumentsDescription
query()string | BuildQueryFilter content using query builder
limit()numberSet number of items to return
page()numberSet which page of results to fetch
sortBy()SortBy[]Sort by one or more fields
language()number | stringSet content language
depth()numberSet depth of related content

Example#

const blogs = await client.content.getCollection('Blog').limit(10).page(1);
get(
  uri: string,
  options?: {
    depth?: number;
    languageId?: number;
  }
): Promise<DotCMSNavigationItem[]>

Parameters#

ParameterTypeRequiredDescription
uristringNavigation root URI
optionsobjectNavigation options

Options#

OptionTypeDescription
depthnumberNumber of child levels to include
languageIdnumberLanguage ID for localized navigation names

Example#

const nav = await client.navigation.get('/', { depth: 2 });

Concepts & Architecture#


Key Concepts#

TermDescriptionDocumentation
pageAssetThe page data structure containing layout and contentPage API
contentletA single piece of content in dotCMSContent API
collectionA group of contentlets of the same typeContent API
graphqlQuery language used to extend API responsesGraphQL

Choosing the Right Method#

The dotCMS Client SDK provides three core methods for fetching data. Use this quick guide to decide which one is best for your use case:

MethodUse When You Need...Best For
client.page.get()A full page with layout, containers, and related contentRendering entire pages with a single request. Ideal for headless setups, SSR/SSG frameworks, and cases where you want everything—page structure, content, and navigation—tied to a URL path.
client.content.getCollection()A filtered list of content items from a specific content typePopulating dynamic blocks, lists, search results, widgets, or reusable components.
client.navigation.get()Only the site's navigation structure (folders and links)Standalone menus or use cases where navigation is needed outside of page context.

Start with page.get(): The One-Request Solution#

For most use cases, client.page.get() is all you need. It lets you retrieve:

  • The full page layout
  • Related content
  • Navigation structure

All in a single request using GraphQL.

Only use content.getCollection() or navigation.get() if you have advanced needs, like real-time data fetching or building custom dynamic components.

🔍 For comprehensive examples of advanced GraphQL querying including relationships and custom fields, see the How to Work with GraphQL section.

Architecture Overview#

The SDK follows a client-builder pattern with three main APIs:

  • Page API (client.page.get()) - Fetches complete page content with layout and containers
  • Content API (client.content.getCollection()) - Builder pattern for querying content collections
  • Navigation API (client.navigation.get()) - Fetches site navigation structure

All APIs support:

  • Type-safe responses with TypeScript
  • GraphQL query extensions
  • Localization and personalization
  • Browser and Node.js compatibility

Support & Contributing#


dotCMS Support#

We offer multiple channels to get help with the dotCMS Client SDK:

  • GitHub Issues: For bug reports and feature requests, please open an issue in the GitHub repository.
  • Community Forum: Join our community discussions to ask questions and share solutions.
  • Stack Overflow: Use the tag dotcms-client when posting questions.

When reporting issues, please include:

  • SDK version you're using
  • dotCMS version
  • Minimal reproduction steps
  • Expected vs. actual behavior

Enterprise customers can access premium support through the dotCMS Support Portal.

How To Contribute#

GitHub pull requests are the preferred method to contribute code to dotCMS. We welcome contributions to the DotCMS UVE SDK! If you'd like to contribute, please follow these steps:

  1. Fork the repository dotCMS/core
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

Please ensure your code follows the existing style and includes appropriate tests.

Licensing Information#

dotCMS comes in multiple editions and as such is dual-licensed. The dotCMS Community Edition is licensed under the GPL 3.0 and is freely available for download, customization, and deployment for use within organizations of all stripes. dotCMS Enterprise Editions (EE) adds several enterprise features and is available via a supported, indemnified commercial license from dotCMS. For the differences between the editions, see the feature page.

This SDK is part of dotCMS's dual-licensed platform (GPL 3.0 for Community, commercial license for Enterprise).

Learn more at dotcms.com.

Found an issue with this documentation? Report it on GitHub