Get Started with JavaScript Management SDK

This guide will help you get started with Contentstack JavaScript Management SDK (that uses Content Management APIs) to manage apps powered by Contentstack. This includes operations such as creating, updating, deleting, and fetching content of your Contentstack account.

Prerequisite

You need Node.js version 10 or above installed to use the Contentstack JavaScript Management SDK. 

Installation

Node

Install it via npm:

npm i @contentstack/management

To import the SDK, use the following command:

import contentstack from '@contentstack/management'
contentstackClient = contentstack.client() 

Authentication

To use this SDK, you need to authenticate users. You can do this by using an authtoken, credentials, or a management token (stack-level token). Let's discuss them in detail.

Authtoken

An authtoken is a read-write token used to make authorized CMA requests, and it is a user-specific token. 

contentstackClient = contentstack.client({ authtoken: 'AUTHTOKEN' }) 

Login

To log in to Contentstack, provide your credentials in the following code:

contentstackClient.login({ email: 'EMAIL', password: 'PASSWORD'})
.then((response) => {
    console.log(response.notice)
    console.log(response.user)
}) 

Management Token

Management tokens are stack-level tokens, with no users attached to them. 

contentstackClient.stack({ api_key: 'API_KEY', management_token: 'MANAGEMENT_TOKEN' })
.fetch()
.then((stack) => {
    console.log(stack)
}) 

Contentstack Management JavaScript SDK: 5-minute Quickstart

Initializing your SDK

To use the JavaScript CMA SDK, you need to first initialize it. To do this, use the following code: 

import contentstack from '@contentstack/management'

var contentstackClient = contentstack.client({ authtoken: 'AUTHTOKEN' }) 

Fetch Stack Details

To fetch your stack details through the SDK, use the following lines of code: 

contentstackClient.stack({ api_key: 'API_KEY' })
.fetch()
.then((stack) => {
    console.log(stack)
}) 

Create an Entry

You can use the following lines of code to create an entry in a specific content type of a stack through the SDK:

var entry  = {
    title: 'Sample Entry',
    url: '/sampleEntry'
}

contentstackClient.stack({ api_key: 'API_KEY' }).contentType('CONTENT_TYPE_UID').entry().create({ entry })
.then((entry) => {
    console.log(entry)
})

Upload Assets

Use the following code snippet to upload assets to your stack through the SDK:

var asset  = {
    upload: 'path/to/file',
    title: 'Asset Title'
}

contentstackClient.stack({ api_key: 'API_KEY' }).asset().create({ asset })
.then((asset) => {
    console.log(asset)
}) 

Limitations

  • We have a URL size limitation of 8KB on API Requests that hit our CDN services. Any Request URL that goes above this size limit will receive the 400 - Bad request error response. Please make sure you limit the size of your API Requests.
  • The JavaScript Management SDK does not support multiple content types referencing in a single query.
  • Currently, the JavaScript Management SDK does not support retrieving details of Global Fields.

Further Reading

Was this article helpful?

Thanks for your feedbackSmile-icon

On This Page

top-arrow