microCMS JavaScript SDK
The `microcms-js-sdk` is the official JavaScript SDK client designed to interact with microCMS Content APIs and Management APIs from both Node.js and browser environments. Currently at stable version `3.4.0`, it receives regular updates, with several minor and patch releases in recent months, alongside a significant major update to v3.0.0. Key differentiators include its comprehensive support for microCMS's List and Object API formats, methods for common operations like content retrieval (`getList`, `getListDetail`, `getObject`), content creation, updating, and deletion. It also offers features like `getAllContents` and `getAllContentIds` for advanced retrieval patterns and a Management API client for tasks such as image uploads (introduced in v3.1.0). The SDK is type-safe as it ships with TypeScript definitions, making it suitable for modern JavaScript and TypeScript projects. Users should be aware of the breaking changes introduced in v3.0.0, particularly the shift to `global fetch` and a Node.js v18+ runtime requirement.
Common errors
-
ReferenceError: fetch is not defined
cause Attempting to use `microcms-js-sdk` v3.0.0 or later in a Node.js environment older than v18, which does not natively provide the `fetch` API.fixUpgrade your Node.js version to 18.0.0 or higher. Alternatively, if upgrading is not immediately possible, you may need to explicitly polyfill `fetch` globally in your application's entry point, though this is not officially supported by the SDK maintainers for v3+. -
TypeError: client.getList is not a function
cause This error often occurs when `createClient` is not properly imported or initialized, or when the `client` object is not correctly instantiated before attempting to call its methods. This can happen with incorrect CommonJS `require` syntax in an ESM module, or a failed global script load in a browser.fixVerify that `createClient` is imported correctly based on your module system (ESM `import` for modern Node.js/bundlers, CommonJS `require` for older Node.js, or `microcms.createClient` for browser UMD). Ensure `createClient` is called and its result is assigned to a variable (`client`) before attempting to use `client.getList` or other methods. -
TypeError: Cannot read properties of undefined (reading 'serviceDomain')
cause The configuration object passed to `createClient` is missing `serviceDomain` or `apiKey`, or these properties are `undefined` due to incorrect environment variable loading or typos.fixEnsure that `serviceDomain` and `apiKey` are correctly provided as strings when initializing the client. Double-check environment variable names and fallback values. For example: `serviceDomain: process.env.MICROCMS_SERVICE_DOMAIN || 'your_domain'`.
Warnings
- breaking Version 3.0.0 introduced significant breaking changes, primarily the removal of `cross-fetch` and the `customFetch` option. The SDK now relies entirely on the native `global fetch` API. Projects that previously used `customFetch` for polyfills or custom HTTP clients must adjust.
- breaking As of v3.0.0, the minimum supported Node.js version has been elevated to v18.0.0 or higher. Older Node.js runtimes will not be able to run this SDK version due to its reliance on native `fetch` and `URLSearchParams` without polyfills.
- breaking The `qs` package, previously used for query parameter serialization, was removed in v3.0.0. The SDK now exclusively uses `URLSearchParams()` for query string handling. While this should be largely transparent for standard usage, custom or complex query parameter structures might behave differently.
- security Version 3.4.0 included security releases labeled 'march-2026-security-releases'. Users should update to the latest patch version to ensure all recent security patches are applied.
- gotcha When using the SDK via CDN (e.g., `cdn.jsdelivr.net`), a warning is explicitly provided that the hosting service is not related to microCMS. For production environments, self-hosting the UMD bundle is recommended to ensure stability and control over asset delivery.
Install
-
npm install microcms-js-sdk -
yarn add microcms-js-sdk -
pnpm add microcms-js-sdk
Imports
- createClient
const { createClient } = require('microcms-js-sdk');import { createClient } from 'microcms-js-sdk'; - createClient
const { createClient } = require('microcms-js-sdk'); - microcms global object
import { createClient } from 'microcms-js-sdk';<script src="https://cdn.jsdelivr.net/npm/microcms-js-sdk/dist/umd/microcms-js-sdk.min.js"></script> <script> const { createClient } = microcms; </script>
Quickstart
import { createClient } from 'microcms-js-sdk';
interface MyContent {
id: string;
title: string;
description: string;
createdAt: string;
updatedAt: string;
publishedAt: string;
revisedAt: string;
}
const client = createClient({
serviceDomain: process.env.MICROCMS_SERVICE_DOMAIN ?? 'YOUR_SERVICE_DOMAIN',
apiKey: process.env.MICROCMS_API_KEY ?? 'YOUR_API_KEY',
});
async function fetchContentList() {
try {
const response = await client.getList<MyContent>({
endpoint: 'blogs',
queries: {
limit: 5,
offset: 0,
orders: '-publishedAt',
fields: 'id,title,publishedAt',
filters: 'publishedAt[less_than]2026-12-31T23:59:59.999Z'
}
});
console.log('Fetched content list:', JSON.stringify(response, null, 2));
return response;
} catch (error) {
console.error('Error fetching content:', error);
throw error;
}
}
fetchContentList();