Was this page helpful?

Getting Started with Contentful and Python

This guide will show you how to get started using our Python client library to consume content.

Contentful's Content Delivery API (CDA) is a read-only API for retrieving content from Contentful. All content, both JSON and binary, is fetched from the server closest to a user's location by using our global CDN.

We publish client libraries for various languages to make developing applications easier.


This tutorial assumes that you understand the Contentful data model.


For every request, clients need to provide an API key, which is created per space and used to delimit applications and content classes.

You can create an access token using the Contentful web app or the Content Management API.


Install the 'contentful' client with pip:

pip install contentful
Note: On some systems, particularly if you're not using virtualenv you may have to use sudo to install the client library.

Or add the client library to your requirements.txt file:


And run pip install -r requirements.txt to install the client and all dependencies.

Setting up the Contentful client

Once you have installed the package, you can use it inside your application.

Initialize the client

You need an API key and a space ID to initialize a client. You can use the API key and space ID pre-filled below from our example space or replace them with your own values.

import contentful

client = contentful.Client('<space_id>', '<access_token>')

Getting your content

Contentful separates content between entries, which contain your data and relationships with other content or images, and assets, which represent static content, like images, and are served as files. Read more in our content model guide.


With the client created, you can now start consuming data from the CDA.

For example, to request all entries in a space:

entries = client.entries()

for entry in entries:
    print(getattr(entry, 'product_name', 'Not a product'))
Not a product
Not a product
Whisk Beater
Hudson Wall Cup
Not a product
Not a product
Not a product
Not a product
SoSo Wall Clock
Not a product
Not a product
Not a product
Playsam Streamliner Classic Car, Espresso

Or to request a single entry:

entry_id = '<entry_id>'
classic_car = client.entry(entry_id)
Playsam Streamliner Classic Car, Espresso

You can specify any of the query parameters accepted by the API, for example:

products_by_price = client.entries({'content_type': '<product_content_type_id>', 'order': 'fields.price'})

for entry in products_by_price:
Hudson Wall Cup
Whisk Beater
Playsam Streamliner Classic Car, Espresso
SoSo Wall Clock

Using your entry as a Python object

Once you have your entry, you can use it as a Python object that follows standard Python conventions:

print("it costs {0}".format(product.price))
print("I am tagged with {0}".format(' and '.join(product.tags)))
Playsam Streamliner Classic Car, Espresso
it costs 44
I am tagged with wood and toy and car and sweden and design

You can form complicated queries and interaction with your entries:

products_with_many_tags = [ product for product in client.entries({'content_type': '<product_content_type_id>', 'include': 2}) if product.tags.size > 2 ]
for product in products_with_many_tags:
   print("I am tagged with {0}".format(' and '.join(product.tags)))
   print("My brand is: {0}".format(product.brand.company_name))
I am tagged with vase and flowers and accessories
My brand is: Normann Copenhagen
I am tagged with wood and toy and car and sweden and design
My brand is: Playsam
I am tagged with kitchen and accessories and whisk and scandinavia and design
My brand is: Normann Copenhagen
I am tagged with home décor and clocks and interior design and yellow and gifts
My brand is: Lemnos

In this example you added the include: 2 parameter, which allows the API to resolve links to other related entries.

Using assets

You query assets in a similar way to entries, but the CDA offers more specific features, such as filtering by the type of file. You can also use our Images API, that allows you to manipulate images as you retrieve them.

To query a single asset:


To query all assets in a space:

assets = client.assets()

for asset in assets: