@contentstack/delivery-sdk

中文文档
5.6.2发布于 2026/11/02MITTypeScriptESM + CJS构建来源已验证近 30 天下载 14.9万

Contentstack ## Typescript Content Delivery SDK for Contentstack

原始使用文档
文档目录12 节
  1. Typescript Content Delivery SDK for Contentstack
  2. Prerequisite
  3. Setup and Installation
  4. Key Concepts for using Contentstack
  5. Contentstack Typescript SDK
  6. Advanced Queries
  7. Working with Images
  8. Pagination
  9. Caching
  10. Development
  11. Helpful Links
  12. The MIT License (MIT)

Contentstack

Typescript Content Delivery SDK for Contentstack

Contentstack is a headless CMS with an API-first approach. It is a CMS that developers can use to build powerful cross-platform applications in their favorite languages. Build your application frontend, and Contentstack will take care of the rest. Read More.

Contentstack provides Typescript SDK to build application on top of Typescript. Given below is the detailed guide and helpful resources to get started with our Typescript SDK.

The Typescript SDK can also be used to create Node.js and React native applications.

Prerequisite

You need Node.js version 18 or later installed to use the Contentstack TypeScript SDK.

Setup and Installation

Node.js uses the Typescript SDK to create apps. To use the Typescript SDK, install it via npm:

npm i @contentstack/delivery-sdk

To import the SDK in your project, use the following command:

import contentstack from '@contentstack/delivery-sdk'

To initialize the SDK, you will need to specify the API Key, Delivery Token, and Environment Name of your stack.

    import contentstack from '@contentstack/delivery-sdk'
    const stack = contentstack.stack({ apiKey: "apiKey", deliveryToken: "deliveryToken", environment: "environment" });

Key Concepts for using Contentstack

Stack

A stack is like a container that holds the content of your app. Learn more about Stacks.

Content Type

Content type lets you define the structure or blueprint of a page or a section of your digital property. It is a form-like page that gives Content Managers an interface to input and upload content. Read more.

Entry

An entry is the actual piece of content created using one of the defined content types. Learn more about Entries.

Asset

Assets refer to all the media files (images, videos, PDFs, audio files, and so on) uploaded to Contentstack. These files can be used in multiple entries. Read more about Assets.

Environment

A publishing environment corresponds to one or more deployment servers or a content delivery destination where the entries need to be published. Learn how to work with Environments.

Contentstack Typescript SDK

Initializing your SDK

You will need to specify the API key, Delivery Token, and Environment Name of your stack to initialize the SDK:

    const stack = contentstack.stack({ apiKey: "apiKey", deliveryToken: "deliveryToken", environment: "environment" });

Once you have initialized the SDK, you can start getting content in your app.

Region and Host Configuration

The SDK supports region and custom host configuration to connect to different Contentstack data centers or custom endpoints.

Region Support

You can specify a region to connect to the appropriate Contentstack data center. Available regions are:

  • US (default) - Uses cdn.contentstack.io
  • EU - Uses eu-cdn.contentstack.com
  • AU - Uses au-cdn.contentstack.com
  • AZURE_NA - Uses azure-na-cdn.contentstack.com
  • AZURE_EU - Uses azure-eu-cdn.contentstack.com
  • GCP_NA - Uses gcp-na-cdn.contentstack.com
  • GCP_EU - Uses gcp-eu-cdn.contentstack.com
import contentstack, { Region } from '@contentstack/delivery-sdk'

// Using region enum
const stack = contentstack.stack({
  apiKey: "apiKey",
  deliveryToken: "deliveryToken",
  environment: "environment",
  region: Region.EU
});

// Using region string
const stack = contentstack.stack({
  apiKey: "apiKey",
  deliveryToken: "deliveryToken",
  environment: "environment",
  region: "eu"
});

Custom Host Support

You can specify a custom host to connect to a custom endpoint. If a custom host is provided, it takes priority over the region-based host.

const stack = contentstack.stack({
  apiKey: "apiKey",
  deliveryToken: "deliveryToken",
  environment: "environment",
  host: "custom-cdn.example.com"
});

// Custom host with region (host takes priority)
const stack = contentstack.stack({
  apiKey: "apiKey",
  deliveryToken: "deliveryToken",
  environment: "environment",
  region: Region.EU,
  host: "custom-cdn.example.com"
});

Querying content from your stack

To get a single entry, you need to specify the content type as well as the ID of the entry.

import { BaseEntry } from '@contentstack/delivery-sdk'
interface BlogPostEntry extends BaseEntry {
  // custom entry types
}
const result = await stack
                      .contentType(contentType_uid)
                      .entry(entry_uid)
                      .fetch<BlogPostEntry>();

To retrieve multiple entries of a content type, you need to specify the content type uid. You can also specify search parameters to filter results.

import { BaseEntry, FindEntry } from '@contentstack/delivery-sdk'
interface BlogPostEntry extends BaseEntry {
  // custom entry types
}
const result = await stack.contentType("contentType1Uid").entry()
                    .query()
                    .find<BlogPostEntry>()

Advanced Queries

You can query for content types, entries, assets and more using our Typescript API Reference.

Typescript API Reference Doc

Working with Images

We have introduced Image Delivery APIs that let you retrieve images and then manipulate and optimize them for your digital properties. It lets you perform a host of other actions such as crop, trim, resize, rotate, overlay, and so on.

For example, if you want to crop an image (with width as 300 and height as 400), you simply need to append query parameters at the end of the image URL, such as, https://images.contentstack.io/owl.jpg?crop=300,400. There are several more parameters that you can use for your images.

Read Image Delivery API documentation.

Following are Image Delivery API examples.

const url = 'www.example.com';
const transformObj = new imageTransform().bgColor('cccccc');
const transformURL = url.transform(transformObj);

Pagination

If the result of the initial sync (or subsequent sync) contains more than 100 records, the response would be paginated. It provides pagination token in the response. You will need to use this token to get the next batch of data.

const query = stack.contentType("contentTypeUid").entry().query();
const pagedResult = await query
                            .paginate()
                            .find<BlogPostEntry>(); 
// OR
const pagedResult = await query
                            .paginate({ skip: 20, limit: 20 })
                            .find<BlogPostEntry>();

Caching

To use cache policies (CACHE_THEN_NETWORK, CACHE_ELSE_NETWORK, NETWORK_ELSE_CACHE), you must provide a persistence store via cacheOptions.persistenceStore. Install the optional persistence plugin and pass a PersistenceStore instance:

npm install @contentstack/persistence-plugin
import contentstack, { Policy } from '@contentstack/delivery-sdk';
import { PersistenceStore } from '@contentstack/persistence-plugin';

const stack = contentstack.stack({
  apiKey: 'your-api-key',
  deliveryToken: 'your-delivery-token',
  environment: 'your-environment',
  cacheOptions: {
    policy: Policy.CACHE_THEN_NETWORK,
    persistenceStore: new PersistenceStore({ storeType: 'localStorage', maxAge: 86400000 }),
  },
});

See @contentstack/persistence-plugin for options (storeType, maxAge, custom storage).

Development

When contributing or running tests from the repository, run npm run build before running tests. The published package includes a pre-built dist/ directory.

The MIT License (MIT)

Copyright © 2012-2026 Contentstack. All Rights Reserved

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

版本与发布记录

@contentstack/delivery-sdk 共发布 57 个版本,首次发布于 2024/01/25;过去 12 个月发布了 24 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
5.6.2latest2026/11/028 天前
5.6.12026/10/061 个月前
5.6.02026/08/122 个月前
5.5.22026/08/053 个月前
5.5.12026/08/033 个月前
5.5.02026/07/293 个月前

其他发布通道:beta → 4.0.0-beta.6。

关于 @contentstack/delivery-sdk 的常见问题

@contentstack/delivery-sdk 是什么?

## Typescript Content Delivery SDK for Contentstack

如何安装 @contentstack/delivery-sdk?

在项目目录运行 npm install @contentstack/delivery-sdk,或使用 pnpm add / yarn add / bun add。

@contentstack/delivery-sdk 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@contentstack/delivery-sdk 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@contentstack/delivery-sdk 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=18。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@contentstack/delivery-sdk 可以商用吗?

@contentstack/delivery-sdk 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@contentstack/delivery-sdk 的下载量有多少?

根据 npm 官方下载统计,@contentstack/delivery-sdk 在 2026/10/11 – 2026/11/09 这 30 天内被下载 149,120 次,日均约 4,971 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@contentstack/delivery-sdk 还在维护吗?

最新版本 5.6.2 发布于 2026/11/02(8 天前);至今共发布 57 个版本,其中过去 12 个月发布了 24 个;首次发布于 2024/01/25。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。