cs-icon.svg

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 20 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.

import contentstack from '@contentstack/management'
contentstackClient = contentstack.client({ authtoken: 'AUTHTOKEN' })

Login

To log in to Contentstack, provide your credentials as follows:

import contentstack from '@contentstack/management'
contentstackClient = contentstack.client()
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.

import contentstack from '@contentstack/management'
contentstackClient = contentstack.client()
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' }) 

Initialization with Management Token and Branch

If you want to initialize SDK in a particular branch use the code given below:

import contentstack from '@contentstack/management'
contentstackClient = contentstack.client() 
contentstackClient.stack({ api_key: 'API_KEY', management_token: 'MANAGEMENT_TOKEN', branch_uid: 'BRANCH'})

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:

import contentstack from '@contentstack/management'
contentstackClient = contentstack.client({ authtoken: 'AUTHTOKEN' })
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'
}
import contentstack from '@contentstack/management'
contentstackClient = contentstack.client({ authtoken: 'AUTHTOKEN' })
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'
}
import contentstack from '@contentstack/management'
contentstackClient = contentstack.client({ authtoken: 'AUTHTOKEN' })
contentstackClient.stack({ api_key: 'API_KEY' }).asset().create({ asset })
.then((asset) => {
    console.log(asset)
}) 

Further Reading

Was this article helpful?
^