Get Started with TypeScript Delivery SDK and Live Preview
This guide will help you get started with Contentstack TypeScript Delivery SDK to build apps powered by Contentstack.
Prerequisites
To get started with TypeScript, you will need the following:
- NodeJS version 20 or later
Initializing the Stack with Live Preview
Since the Live Preview Utils SDK is responsible for communication, you need to initialize it within your stack.
Use the following command to initialize the stack:
import contentstack from 'contentstack/delivery-sdk'
const Stack = contentstack.stack({
apiKey: 'api_key',
deliveryToken: 'delivery_token',
environment: 'environment',
live_preview: {
preview_token: <preview_token>',
enable: true,
host: 'rest-preview.contentstack.com',
}
})
Note: By default, the host parameter points to the North America endpoint. If your website is hosted on the European data center, then pass the European endpoint against the host parameter.
For Server-side Rendered Websites
If you have a Server-side Rendered (SSR) website, use the following code to get the Live Preview hash key:
import express from 'express'
const app = express()
app.use((req, response, next) => {
// this will get live_preview hash and ContentType to request
Stack.livePreviewQuery(req.query)
next()
});
To install and initialize the Live Preview Utils SDK, you can refer to our SSR Live Preview Setup documentation.
For Client-side Rendered Websites
If your website is rendered on the client-side, you can refer to our CSR Live Preview Setup documentation.
Query Request
Contentstack SDKs let you interact with the Content Delivery APIs and retrieve content from Contentstack. They are read-only in nature. The SDKs fetch and deliver content from the nearest server via Fastly, our powerful and robust CDN.
To get an entry, you need to specify the content type UID and the UID of the entry.
Stack.contentType('content_type_uid').entry('entry_uid').fetch()
Stack.contentType('content_type_uid').entry().find()