Get Started with Python SDK

This guide will help you get started with Contentstack Python SDK to build apps powered by Contentstack.

Pre-requisites

Installation and Setup

To use the Contentstack Python SDK with your existing project, perform the following steps:

  1. Open the terminal, create a project, and move inside it as follows:
    mkdir project_name
    cd project_name
  2. Create a virtual environment:
    python3 -m venv venv
  3. Activate the virtual environment:
    source  venv/bin/activate
  4. Install pip Contentstack as follows:
    pip install contentstack

Initialize SDK

Contentstack offers two regions (the US and European) as data centers to store customers' account details and data. Both regions are independent of each other and therefore have a dedicated set of instructions to use SDKs offered by Contentstack.

To use SDKs for the European region, you will have to make certain changes in the configuration of the SDK, as detailed below, and the rest of the instructions remain the same.

To initialize the SDK, the stack’s API key, delivery token, and name of the environment where you will publish the content, as shown in the snippet below:

import contentstack
stack = contentstack.Stack(api_key='API_key', access_token='delivery_token', environment='environment_id')

For Setting the European Region:

To set European region, refer to the code below:

import contentstack

config = Config()
config.region = ContentstackRegion.EU
stack = contentstack.Stack(api_key="API_key", access_token="delivery_token",environment= "environment_id", config)

Once you have initialized the SDK, you can query entries to fetch the required content.

Basic Queries

Get a Single Entry

To retrieve a single entry from a content type, use the code snippet given below:

import contentstack
stack = contentstack.Stack(api_key='API_key', access_token='delivery_token', environment='environment_id')

contentType = stack.content_type("content_type_uid")
blog_entry = content_type.entry("entry_uid")
blog_entry.fetch()

Get Multiple Entries

To retrieve multiple entries of a particular content type, use the code snippet given below:

import contentstack
stack = contentstack.Stack(api_key='API_key', access_token='delivery_token', environment='environment_id')

blog_query = stack.content_type("content_type_uid").query()
Response = blog_query.find()

These were examples of some of the basic queries of the SDK. For advanced queries, refer to Contentstack Python SDK API reference documentation.

More articles in "Use Python SDK"

On This Page

top-arrow