Contentstack for Developers
Using the JavaScript Content Delivery SDK
Getting Started using the JavaScript Content Delivery SDK
To get started with the Content Delivery SDK and NodeJS, the first thing you need to do is to install it. You can do this by running the following command in your project directory:
npm install contentstack
Helpful Javascript Resources
Include the JS SDK in your projects
https://cdn.jsdelivr.net/npm/contentstack@latest/dist/web/contentstack.min.js
Once you have installed the SDK, you need to initialize it with your API key, delivery token, and environment name. Here's an example of how to do this:
const Contentstack = require('contentstack');
const Stack = Contentstack.Stack({
api_key: 'your_api_key',
delivery_token: 'your_delivery_token',
environment: 'your_environment'
});
With the SDK initialized, you can start retrieving content from Contentstack's API. The SDK provides several methods for fetching content, depending on your requirements.
If you want to retrieve a single entry, you can use the Entry method, as shown in the following example:
Stack.ContentType('your_content_type_uid').Entry('your_entry_uid').fetch()
.then(function success(entry) {
console.log(entry);
}, function error(err) {
console.log(err);
});
In this example, your_content_type_uid is the unique identifier of the content type you want to retrieve, and your_entry_uid is the unique identifier of the entry you want to retrieve. The fetch() method retrieves the entry and returns it as a JavaScript object.
If you want to retrieve multiple entries, you can use the Query method, as shown in the following example:
const stack = Contentstack.Stack({"api_key":"bltc2242b759e36fcb9","delivery_token":"cs1470a664abc4563bd43a604d","environment":"development"}); // use your api_key and delivery_token here from your Stack
const query = stack.GetEntry('blog_post').Query();
query.toJSON()
.find()
.then(
(result) => {
console.log(result[0]);
},
(error) => {
console.error(error);
},
);
In this example, your_content_type_uid is the unique identifier of the content type you want to retrieve. The find() method retrieves all the entries of the content type and returns them as an array of JavaScript objects.
The SDK also provides methods for filtering and sorting entries, as well as retrieving entries with specific fields. You can learn more about these methods in the Contentstack documentation.
There are also SDKs for most popular languages such as .Net, Java, PHP, Python, Ruby, iOS, Android, Dart, etc.
Documentation links:
