Contentstack JavaScript Delivery SDK

3.27.0 · active · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the Contentstack SDK and fetch a single entry by its content type UID and entry UID. It includes essential error handling for missing credentials and showcases region configuration.

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();

view raw JSON →