Was this article helpful?
Thanks for your feedback
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.
You need Node.js version 10 or above installed to use the Contentstack JavaScript Management SDK.
To install it via npm:
npm i @contentstack/management
To import the SDK, use the following:
import contentstack from '@contentstack/management' contentstackClient = contentstack.client()
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.
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' })
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 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) })
To use the JavaScript CMA SDK, you need to first initialize it.
import contentstack from '@contentstack/management' var contentstackClient = contentstack.client({ authtoken: 'AUTHTOKEN' })
For Setting the 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'})
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' } }, })
To fetch your stack details through the SDK, use the following:
contentstackClient.stack({ api_key: 'API_KEY' }) .fetch() .then((stack) => { console.log(stack) })
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) })
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) })
Was this article helpful?
Thanks for your feedback