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

To install it via npm:

npm i @contentstack/management

To import the SDK, use the following:

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 as follows:

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)
}) 

Initialize your SDK

To use the JavaScript CMA SDK, you need to first initialize it.

import contentstack from '@contentstack/management'

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

Proxy Configuration

Contentstack allows you to define HTTP proxy for your requests with the JavaScript Management SDK. A proxied request allows you to anonymously access public URLs even from within a corporate firewall through a proxy server.

Here is the basic syntax of the proxy settings that you can pass within fetchOptions of the JavaScript Management SDK:

import * as contentstack from '@contentstack/management'
const client = contentstack.client({
 proxy: {
   protocol: 'https',
   host: '127.0.0.1',
   port: 9000,
   auth: {
     username: 'username',
     password: 'password'
   }
 },
})

Fetch Stack Details

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

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

Create an Entry

You can use the following 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

^