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.
To get started with the Python Management SDK, you will need the following:
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()To use this SDK, you must authenticate yourself using one of the following methods during initialization:
Let’s look at each of them in detail.
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')To log in to Contentstack using your credentials, you use the following lines of code:
client.login(email="email", password="password")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)To initialize the SDK, execute the following command:
import contentstack_management
client = contentstack_management.Client(authtoken='authtoken')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.
To retrieve the details of your stack, execute the following command:
result = client.stack(api_key='api_key').fetch().json()
print(result)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())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)Python Management Github Repository