Contentstack JavaScript Delivery SDK
The Contentstack JavaScript SDK provides a robust interface for interacting with the Contentstack headless CMS, enabling developers to build various applications, including Node.js, React Native, and browser-based frontends. Currently stable at version 3.27.0, the SDK undergoes active development with frequent patch and minor releases addressing bug fixes, security updates, and feature enhancements. Its API-first design allows for seamless content delivery across platforms, differentiating it by focusing on developer experience and multi-platform support. Users should note the requirement for Node.js version 10.14.2 or higher for server-side implementations, though the TypeScript Delivery SDK documentation suggests Node.js 22 or later is preferred for the TypeScript variant. The SDK is read-only, focusing on content delivery and leveraging Fastly CDN for optimal performance.
Common errors
-
TypeError: Contentstack is not a function
cause Attempting to import `Contentstack` as a named export (`import { Contentstack } from 'contentstack';`) when it's a default export, or trying to directly call `Contentstack()` as a constructor when `Contentstack.Stack()` is the correct method for initialization.fixUse the correct default import: `import Contentstack from 'contentstack';` and then initialize with `Contentstack.Stack({...});`. -
Contentstack: api_key, delivery_token or environment is missing
cause The SDK initialization object is missing one or more of the required `api_key`, `delivery_token`, or `environment` parameters.fixEnsure all three required parameters (`api_key`, `delivery_token`, and `environment`) are provided in the object passed to `Contentstack.Stack()`. -
ReferenceError: Contentstack is not defined
cause In a browser environment, the Contentstack SDK script tag was not loaded, or the variable is being accessed before the script has executed and defined it globally. In Node.js, the import statement might be missing or incorrect.fixFor browsers, ensure `<script src="https://cdn.jsdelivr.net/npm/contentstack@latest/dist/web/contentstack.min.js"></script>` is loaded before usage. For Node.js/ESM, ensure `import Contentstack from 'contentstack';` is at the top of your file. -
Error: Request failed with status code 401 (Unauthorized)
cause The provided `api_key` or `delivery_token` is incorrect, expired, or does not have permissions to access the specified environment or content.fixVerify that your `api_key`, `delivery_token`, and `environment` are correct and have the necessary permissions in your Contentstack account. Ensure you are using a Delivery Token for content fetching. -
UND_ERR_SOCKET
cause Unhandled socket errors occurring during network connection drops, leading to process termination in older SDK versions.fixUpgrade the `contentstack` SDK to version `3.27.0` or higher to benefit from the fix for connection drop handling.
Warnings
- breaking A security vulnerability (CVE-2025-15284) related to the 'qs' dependency was fixed in `v3.26.4`. Prior versions are affected.
- gotcha Applications using `contentstack` versions prior to `3.27.0` may experience process crashes due to unhandled socket errors (`UND_ERR_SOCKET`) during network connection drops.
- gotcha The SDK requires Node.js version 10.14.2 or higher. Running on older Node.js environments may lead to unexpected errors, initialization failures, or runtime crashes. The TypeScript Delivery SDK specifically recommends Node.js 22 or later.
- gotcha Failing to correctly specify the region (e.g., `Contentstack.Region.EU`, `Contentstack.Region.AU`, `Contentstack.Region.AZURE_NA`, `Contentstack.Region.GCP_EU`) during SDK initialization can result in content fetching failures or increased latency if the default North American region is not desired.
- breaking The underlying Contentstack platform has deprecated Access Tokens for stacks created after December 16, 2020. While older stacks still support them, new stacks require Delivery Tokens for content fetching via the Delivery API.
Install
-
npm install contentstack -
yarn add contentstack -
pnpm add contentstack
Imports
- Contentstack
import { Contentstack } from 'contentstack';import Contentstack from 'contentstack';
- Stack
import { Stack } from 'contentstack';import Contentstack from 'contentstack'; const Stack = Contentstack.Stack({ ... }); - Region (Enum)
import { Region } from 'contentstack';import Contentstack from 'contentstack'; Contentstack.Region.EU;
Quickstart
import Contentstack from 'contentstack';
const API_KEY = process.env.CONTENTSTACK_API_KEY ?? '';
const DELIVERY_TOKEN = process.env.CONTENTSTACK_DELIVERY_TOKEN ?? '';
const ENVIRONMENT = process.env.CONTENTSTACK_ENVIRONMENT ?? '';
const CONTENT_TYPE_UID = 'blog'; // Replace with your content type UID
const ENTRY_UID = 'your_entry_uid'; // Replace with a specific entry UID
if (!API_KEY || !DELIVERY_TOKEN || !ENVIRONMENT) {
console.error('Missing Contentstack credentials. Please set CONTENTSTACK_API_KEY, CONTENTSTACK_DELIVERY_TOKEN, and CONTENTSTACK_ENVIRONMENT environment variables.');
process.exit(1);
}
const Stack = Contentstack.Stack({
api_key: API_KEY,
delivery_token: DELIVERY_TOKEN,
environment: ENVIRONMENT,
// Optional: Set a specific region, e.g., for Europe
// region: Contentstack.Region.EU
});
async function fetchEntry() {
try {
const query = Stack.ContentType(CONTENT_TYPE_UID).Entry(ENTRY_UID);
const entry = await query.toJSON().fetch();
console.log('Fetched Entry Title:', entry.get('title'));
console.log('Entry content:', entry.toJSON());
} catch (error) {
console.error('Error fetching entry:', error);
}
}
fetchEntry();