Contentstack - Javascript Delivery SDK
JavaScript 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 JavaScript Delivery SDK to build application on top of JavaScript. Given below is the detailed guide and helpful resources to get started with our JavaScript Delivery SDK.
The JavaScript Delivery SDK can also be used to create Node.js and React native applications.
Prerequisite
You need Node.js version 4.4.7 or later installed to use the Contentstack JavaScript Delivery SDK.
Setup and Installation
For JavaScript (Browser)
To use the JavaScript Delivery SDK, download it from here and include it in the <script> tag:
<script type="text/javascript" src="/path/to/contentstack.min.js"></script>;
To initialize the SDK, you will need to specify the API Key, Delivery Token, and Environment Name of your stack.
const Stack = Contentstack.Stack({ "api_key": "api_key", "delivery_token": "delivery_token", "environment": "environment" });
To initialize the SDK for European Region you want to set and use European region, refer to the code below:
const Stack = Contentstack.Stack({ "api_key": "api_key", "delivery_token": "delivery_token", "environment": "environment", "region": Contentstack.Region.EU });
For Node.js
Node.js uses the Javascript SDK to create apps. To use the JavaScript Delivery SDK, download it from here, OR install it via npm:
npm i contentstack
To import the SDK in your project, use the following command:
import contentstack from ‘contentstack’
To initialize the SDK, you will need to specify the API Key, Delivery Token, and Environment Name of your stack.
const Stack = Contentstack.Stack({ "api_key": "api_key", "delivery_token": "delivery_token", "environment": "environment");
To initialize the SDK for European Region you want to set and use European region, refer to the code below:
const Stack = Contentstack.Stack({ "api_key": "api_key", "delivery_token": "delivery_token", "environment": "environment", "region": Contentstack.Region.EU });
For React Native
React Native uses the Javascript SDK to create apps. To use the JavaScript Delivery SDK, download it from here, OR install ist via npm:
npm i contentstack
To import the SDK in your project, use the following command:
import contentstack from `contentstack/react-native`
To initialize the SDK, you will need to specify the API Key, Delivery Token, and Environment Name of your stack.
const Stack = Contentstack.Stack({ "api_key": "api_key", "delivery_token": "delivery_token", "environment": "environment" });
To initialize the SDK for European Region you want to set and use European region, refer to the code below:
const Stack = Contentstack.Stack({ "api_key": "api_key", "delivery_token": "delivery_token", "environment": "environment", "region": Contentstack.Region.EU };
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 JavaScript Delivery SDK: 5-minute Quickstart
Initializing your SDK
You will need to specify the API key, Access token, and Environment Name of your stack to initialize the SDK:
const Stack = Contentstack.Stack({ "api_key": "api_key", "delivery_token": "delivery_token", "environment": "environment" });
Once you have initialized the SDK, you can start getting content in your app.
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.
const Stack = Contentstack.Stack({ "api_key": "api_key", "delivery_token": "delivery_token", "environment": "environment" });
const Query = Stack.ContentType('blog').Entry("entry_uid");
Query.fetch()
.then(function success(entry) {
console.log(entry.get('title')); // Retrieve field value by providing a field's uid
console.log(entry.toJSON()); // Convert the entry result object to JSON
}, function error(err) {
// err object
});
Note: We have a method on query for a specific language. For example, consider the following query:
Stack.ContentType(type).Query().language('fr-fr').toJSON().find()
It will provide all entries of a content type published on the French locale.
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.
const Stack = Contentstack.Stack({ "api_key": "api_key", "delivery_token": "delivery_token", "environment": "environment" });
const Query = Stack.ContentType('blog').Query();
Query
.where("title", "welcome")
.includeSchema()
.includeCount()
.toJSON()
.find()
.then(function success(result) {
// result is array where -
// result[0] => entry objects
// result[result.length-1] => entry objects count included only when .includeCount() is queried.
// result[1] => schema of the content type is included when .includeSchema() is queried.
}, function error(err) {
// err object
});
Note: Currently, the JavaScript SDK does not support multiple content types referencing in a single query. For more information on how to query entries and assets, refer the Queries section of our Content Delivery API documentation.
Paginating Responses
In a single instance, the Get Multiple Entries query will retrieve only the first 100 items of the specified content type. You can paginate and retrieve the rest of the items in batches using the skip and limit parameters in subsequent requests.
const Stack = Contentstack.Stack({ "api_key": "api_key", "delivery_token": "delivery_token", "environment": "environment" });
let blogQuery = Stack.ContentType('example').Query();
let data = blogQuery.skip(20).limit(20).find()
data.then(function(result) {
// result is array where -
// result[0] => entry objects
},function (error) {
// error function
})
Querying Assets from your stack
To get a single asset, you need to specify the UID of the asset.
const Stack = Contentstack.Stack({ "api_key": "api_key", "delivery_token": "delivery_token", "environment": "environment" });
const Asset = Stack.Asset("");
Asset.fetch()
.then(function success(asset) {
console.log(asset.get('title')); // Retrieve field value by providing a field's uid
console.log(asset.toJSON()); // Convert the entry result object to JSON
}, function error(err) {
// err object
});
To retrieve multiple assets. You can also specify search parameters to filter results.
const Stack = Contentstack.Stack({ "api_key": "api_key", "delivery_token": "delivery_token", "environment": "environment" });
const Query = Stack.Asset().Query();
Query
.limit(20)
.toJSON()
.find()
.then(function success(result) {
// result is array where -
// result[0] => asset objects
}, function error(err) {
// err object
});
Cache Policies
You can set a cache policy on a stack and/or query object.
Setting a cache policy on a stack
This option allows you to globalize a cache policy. This means the cache policy you set will be applied to all the query objects of the stack.
//Setting a cache policy on a stack
Stack.setCachePolicy(Contentstack.CachePolicy.NETWORK_ELSE_CACHE)
Setting a cache policy on a query object
This option allows you to set/override a cache policy on a specific query object.
// setting a cache policy on a queryobject
Query.setCachePolicy(Contentstack.CachePolicy.CACHE_THEN_NETWORK)
Advanced Queries
You can query for content types, entries, assets and more using our JavaScript API Reference.
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
Following are Image Delivery API examples.
// Set the quality 100
const Stack = Contentstack.Stack({ "api_key": "api_key", "delivery_token": "delivery_token", "environment": "environment" });
const imageUrl = Stack.imageTransform(imageUrl, {
'quality': 100
})
// set the quality to 100, auto optimization, width and height
const Stack = Contentstack.Stack({ "api_key": "api_key", "delivery_token": "delivery_token", "environment": "environment" });
const imageUrl = Stack.imageTransform(imageUrl, {
'quality': 100,
'auto': 'webp',
'width': 100,
'height': 100
})
Helpful Links
The MIT License (MIT)
Copyright © 2016-2021 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.