cs-icon.svg

Get Started with Python Management SDK

This step-by-step guide will help you get started with the Python Management SDK, which utilizes Content Management APIs (CMA), for managing Python applications powered by Contentstack.

Note: If you intend to only fetch data without performing any CRUD operations, please use the Contentstack Python Delivery SDK.

Prerequisites

To get started with the Python Management SDK, you will need the following:

Installation and Setup

Execute the following command to install the Python Management SDK:

pip install contentstack_management

To import the SDK, execute the following command:

import contentstack_management 
client = contentstack_management.Client()

Authentication

To use this SDK, you must authenticate yourself using one of the following methods during initialization:

  • Authtoken
  • Login Credentials (your login email and password)
  • OAuth Token

Let’s look at each of them in detail.

Authtoken

An AuthToken is a user-specific, read-write token used to make authorized Content Management API requests. You can retrieve an Authtoken by logging in to Contentstack using the “Log in to your account” request.

client = contentstack_management.Client(authtoken='authtoken')

Login

To log in to Contentstack using your credentials, you use the following lines of code:

client.login(email="email", password="password")

Management Token

Management Tokens are credentials that grant you read-write access to your stack's content. When used in conjunction with the stack API key, they enable authorized CMA requests for managing your stack's content.

result = client.stack(api_key='api_key', management_token='management_token' ).content_type('content_type_uid')
.fetch().json()
print(result)

Initialize your SDK

To initialize the SDK, execute the following command:

import contentstack_management 
client = contentstack_management.Client(authtoken='authtoken')

Basic Queries

This section focuses explicitly on the Python Management SDK and provides an overview of basic queries. You will learn how to retrieve the stack details, create an entry, or upload an asset.

Fetch Stack Details

To retrieve the details of your stack, execute the following command:

result = client.stack(api_key='api_key').fetch().json()
print(result)

Create Entry

To create an entry in a specific content type within a stack, execute the following command:

“# prepare your request body”
entry  = {
  title: 'Sample Entry',
  url: '/sampleEntry'
}
result = client.stack(api_key='api_key').content_types('content_type_uid').entry().create(entry)
print(result.json())

Upload Asset

To upload an asset within a stack, execute the following command:

“# prepare your request body as dictionary”
asset  = {
   upload: 'path/to/file',
   title: 'Asset Title'
}
asset = client.stack(api_key='api_key').assets()
result = asset.upload(asset)

More Resources

Python Management Github Repository

Next Steps

Python Management SDK API Reference

Was this article helpful?
^