This step-by-step guide will help you get started with the Typescript SDK and build apps powered by Contentstack.
To get started with the Typescript Delivery SDK, you will need the following:
To effectively interact with the Content Delivery APIs, a secure authentication process is employed using a Delivery Token that is specific to a publishing environment. Consequently, when utilizing the TypeScript Delivery SDK, you need to provide the necessary delivery token during the SDK initialization process for making API requests.
To install the TypeScript SDK via npm, open the terminal and run the following command:
npm i @contentstack/delivery-sdk
To initialize the SDK, execute the following command:
import contentstack from '@contentstack/delivery-sdk';
const stack = contentstack.stack({
apiKey: "apiKey",
deliveryToken: "deliveryToken",
environment: "environment_name"
})Following are the details of the parameters that you need to pass in the above command:
To initialize the SDK with the latest features offered in the early access phase, include the early access parameter as shown in the following code:
const stack = contentstack.stack({
apiKey:"api_key",
deliveryToken:"delivery_token",
environment:"environment_name",
early_access: ["early_access_1", "early_access_2"]
});Custom Configurations offer developers the flexibility to customize and adapt the SDK's behavior to suit their specific needs.
Here's an overview of the common customizable configurations within a SDK.
Contentstack offers six regions; AWS North America (NA), AWS Europe (EU), Azure North America (Azure NA), Azure Europe (Azure EU), GCP North America (GCP NA), and AWS Australia (AWS AU) as data centers to store customers' account details and data. These regions are independent of each other and therefore have a dedicated set of instructions on how to use Contentstack SDKs.
Note: Users using the AWS NA region do not need to make any configuration changes as the SDK considers this region by default. This means, you need not pass the region parameter in the following code.
To utilize the SDKs in the AWS Europe, Azure NA, Azure EU, GCP NA, or AWS Australia regions, modify the SDK configuration as specified below, while keeping the rest of the instructions unchanged.
const stack = contentstack.stack({
apiKey: "apiKey",
deliveryToken: "deliveryToken",
environment: "environment_name",
region: Region.<<add_your_region>>
})
For setting the branch for AWS Europe, Azure North America, Azure Europe, GCP North America, or AWS Australia, you need to execute the following command and initialize the SDK in a particular branch.
Note: Users using the AWS NA region need not pass the region parameter in the following code.
const stack = contentstack.stack({
apiKey: "api_key",
deliveryToken: "delivery_token",
environment: "environment",
region: Region.<<add_your_region>>;
host: "<<add_your_host_URL>>",
branch: "branch"
})
Once you have initialized the SDK, you can start getting content in your app.
The cache policies allow you to define the source from which the SDK will retrieve the content. Depending on the chosen policy, the SDK can retrieve data from the cache, network, or both. You can set a cache policy on a stack and/or a query object.
Let's explore the different cache policies available for use:
| POLICIES | DESCRIPTION |
|---|---|
| IGNORE_CACHE (default) | For the IGNORE_CACHE policy, the SDK always retrieves data by making a network call without maintaining any cache. This is the default policy. |
| CACHE_ELSE_NETWORK | For the CACHE_ELSE_NETWORK policy, the SDK gets data from the cache, however, it makes a network call if it fails to retrieve data from the cache. |
| NETWORK_ELSE_CACHE | For the NETWORK_ELSE_CACHE policy, the SDK gets data using a network call. However, if the call fails, it retrieves data from the cache. |
| CACHE_THEN_NETWORK | For the CACHE_THEN_NETWORK policy, the SDK gets data from the cache and makes a network call. (A successful callback is invoked twice.) |
Note: Caching in SDK is performed through the SDK's local storage or memory storage instead of CDN.
This option allows you to apply a cache policy globally, ensuring that the cache policy you define is implemented across all query objects within the stack.
const stack = contentstack.stack({
apiKey: "apiKey",
deliveryToken: "deliveryToken",
environment: "environment_name",
cacheOptions: {
policy: "CACHE_THEN_NETWORK",
storeType: "memoryStorage"
}
})Contentstack SDKs are designed exclusively for read-only purposes and utilize Fastly, our robust and efficient CDN, to fetch and deliver content from the nearest server.
This section focuses explicitly on the Typescript Delivery SDK and provides an overview of basic queries. You will learn how to fetch data using the SDK and learn the techniques for efficient content retrieval.
To retrieve details of a single entry, execute the following code and specify the UID of the content type and the entry.
import { BaseEntry } from '@contentstack/delivery-sdk'
interface BlogEntry extends BaseEntry {
note: string;
photo: string;
// other props
}
const result = await stack
.contentType("contentType_uid")
.entry("entry_uid")
.fetch<BlogEntry>();To retrieve details of multiple entries, execute the following code and specify the content type UID.
import { BaseEntry } from '@contentstack/delivery-sdk'
interface BlogEntry extends BaseEntry {
note: string;
photo: string;
// other props
}
const result = await stack
.contentType("contentType_uid")
.entry()
.find<BlogEntry>();To retrieve details of a single asset, execute the following code and specify the asset UID.
import { BaseAsset } from '@contentstack/delivery-sdk'
interface BlogAsset extends BaseAsset {
// other props
}
const result = await stack.asset('asset_uid').fetch<BlogAsset>();To retrieve details of multiple assets, execute the following code.
import { BaseAsset } from '@contentstack/delivery-sdk'
interface BlogAsset extends BaseAsset {
// other props
}
const result = await stack.asset().find<BlogAsset>();In this section, we will explore advanced queries for the TypeScript Delivery SDK. Below examples show how to create complex queries and implement pagination effectively.
The query retrieves all entries that satisfy the query conditions made on referenced fields.
const query = stack.contentType("contentTypeUid").entry().query();
const result = await query.whereIn("brand").find<BlogEntry>();Users can paginate API responses. This means you can request and receive data from the API in smaller chunks or pages, rather than receiving the entire dataset in one go.
const query = stack.contentType("contentTypeUid").entry().query();
interface BlogEntry extends BaseEntry {
note: string;
photo: string;
// other props
}
const pagedResult = await query.paginate().find<BlogEntry>();
// for paginating use the next() or previous() method
const nextPageResult = await query.next().find<BlogEntry>();
const prevPageResult = await query.previous().find<BlogEntry>();