Skip to main content

Contentful Delivery API Client

Project description

Contentful Python Delivery Library

Join Contentful Community Slack Join Contentful Community Forum

contentful.py - Contentful Python Delivery Library

This repository is actively maintained MIT License Build Status

What is Contentful?

Contentful provides a content infrastructure for digital teams to power content in websites, apps, and devices. Unlike a CMS, Contentful was built to integrate with the modern software stack. It offers a central hub for structured content, powerful management and delivery APIs, and a customizable web app that enable developers and content creators to ship digital products faster.

This library is intended to replace the former unofficial Python CDA library. The old library can still be found at: https://github.com/contentful-labs/contentful.py

Core Features

Getting Started

In order to get started with the Contentful Python library you’ll need not only to install it, but also to get credentials which will allow you to have access to your content in Contentful.

Installation

Install Contentful from the Python Package Index:

pip install contentful

Your first request

The following code snippet is the most basic one you can use to get some content from Contentful with this library:

import contentful

client = contentful.Client(
  'cfexampleapi',  # This is the space ID. A space is like a project folder in Contentful terms
  'b4c0n73n7fu1'  # This is the access token for this space. Normally you get both ID and the token in the Contentful web app
)

# This API call will request an entry with the specified ID from the space defined at the top, using a space-specific access token.
entry = client.entry('nyancat')

Using this library with the Preview API

This library can also be used with the Preview API. In order to do so, you need to use the Preview API Access token, available on the same page where you get the Delivery API token, and specify the host of the preview API, such as:

client = contentful.Client('cfexampleapi', 'b4c0n73n7fu1', api_url='preview.contentful.com')

You can query for entries, assets, etc. very similar as described in the Delivery API Documentation. Please note, that all methods of the Python client library are snake_cased, instead of JavaScript’s camelCase.

Authentication

To get your own content from Contentful, an app should authenticate with an OAuth bearer token.

You can create API keys using the Contentful web interface. Go to the app, open the space that you want to access (top left corner lists all the spaces), and navigate to the APIs area. Open the API Keys section and create your first token. Done.

Don’t forget to also get your Space ID.

For more information, check the Contentful REST API reference on Authentication.

Documentation & References

To help you get the most out of this library, we’ve prepared all available client configuration options, reference documentation, tutorials and other examples that will help you learn and understand how to use this library.

Configuration

The client constructor supports several options you may set to achieve the expected behavior:

client = contentful.Client(
    SPACE_ID,
    ACCESS_TOKEN,
    # ... your options here ...
)

Name

Default

Description

space_id

Required. Your space ID.

access_token

Required. Your access token.

environment

‘master’

Your environment ID.

api_url

'cdn.contentful.com'

Set the host used to build the request URIs.

default_locale

'en-US'

Defines default locale for the client.

secure

True

Defines whether to use HTTPS or HTTP. By default we use HTTPS.

authorization_as_header

True

Sets the authentication mechanisms, if False will send authentication via query string.

raise_errors

True

Determines whether errors are raised or returned.

content_type_cache

True

Determines if content type caching is enabled automatically or not, allowing for accessing of fields even when they are not present on the response.

raw_mode

False

If enabled, API responses are not parsed and the raw response object is returned instead.

gzip_encoded

True

Defines whether to use gzip compression for requests.

Reference documentation

Basic queries
content_types = client.content_types()
cat_content_type = client.content_type('cat')
nyancat = client.entry('nyancat')
entries = client.entries()
assets = client.assets()
nyancat_asset = client.asset('nyancat')
# Create an asset key that expires in 24 hours
expires_at = int(time.time() + (24 * 60 * 60))
asset_key = client.create_asset_key(expires_at)  # Returns policy and secret for embargoed assets
Embargoed Assets

You can create asset keys for accessing embargoed assets. Asset keys consist of a policy and a secret that are valid for a specified time period:

import time

# Create an asset key that expires in 24 hours
expires_at = int(time.time() + (24 * 60 * 60))
asset_key = client.create_asset_key(expires_at)

# The asset key contains:
# asset_key.policy - The policy for accessing the asset
# asset_key.secret - The secret key for authentication

For more information, check the Asset Keys API Reference.

Filtering options

You can pass the usual filter options to the query:

client.entries({'content_type': 'cat'}) # query for a content-type by its ID (not name)
client.entries({'sys.id[ne]': 'nyancat'}) # query for all entries except 'nyancat'
client.entries({'include': 1}) # include one level of linked resources
client.entries({'content_type': 'cat', 'include': 1}) # you can also combine multiple parameters

To read more about filtering options you can check our search parameters documentation.

The results are returned as contentful.resource.Resource <contentful.resource.Resource> objects. Multiple results will be returned as list.

Accessing fields and sys properties
content_type = client.content_type('cat')
content_type.description # "Meow."

System Properties behave the same and can be accessed via the #sys method.

content_type.id # => 'cat'
entry.type # => 'Entry'
asset.sys # { 'id': '...', 'type': '...' }

Entry fields also have direct accessors and will be coerced to the type defined in it’s content type.

entry = client.entry('nyancat')
entry.lives # 1337
entry.fields() # { 'name': '...', 'lives': '...', ... }
Accessing tags
nyancat = client.entry('nyancat')
nyancat._metadata['tags'] # => [<Link[Tag] id='nyCampaign'>]

Tags can be accessed via the #_metadata method.

Using different locales

Entries can have multiple locales, by default, the client only fetches the entry with only its default locale. If you want to fetch a different locale you can do the following:

entries = client.entries({'locale': 'de-DE'})

Then all the fields will be fetched for the requested locale.

Contentful Delivery API also allows to fetch all locales, you can do so by doing:

entries = client.entries({'content_type': 'cat', 'locale': '*'})

# assuming the entry has a field called name
my_spanish_name = entries.first.fields('es-AR')['name']

When requesting multiple locales, the object accessor shortcuts only work for the default locale.

Assets

There is a helpful method to add image resize options for an asset image:

client.asset('happycat').url()
# => "//images.contentful.com/cfexampleapi/3MZPnjZTIskAIIkuuosCss/
#     382a48dfa2cb16c47aa2c72f7b23bf09/happycatw.jpg"

client.asset('happycat').url(w=300, h=200, fm='jpg', q=100)
# => "//images.contentful.com/cfexampleapi/3MZPnjZTIskAIIkuuosCss/
#     382a48dfa2cb16c47aa2c72f7b23bf09/happycatw.jpg?w=300&h=200&fm=jpg&q=100"
Entries

Entries can have fields in it’s default locale accessible with accessor methods:

nyancat = client.entry('nyancat')
nyancat.name
# 'Nyan Cat'
Property Accessors

This library provides a simple API to interact with resources that come from the API, by abstracting the underlying JSON structure of the objects, and exposing all the relevant fields as object properties.

For all resources, sys properties will be available as top level properties, for example:

space = client.space()
space.id
# will return the value of space.sys['id']

In the case of Entries and Assets, as well as having sys available as properties, also all the fields on present on fields will be available as properties, for example:

entry = client.entry('nyancat')
entry.name
# 'Nyan Cat'
# this is equivalent to entry.fields()['name']

asset = client.assets()[0]
asset.file['details']['size']
# will return the size of the image
# this is equivalent to asset.fields()['file']['details']['size']

Other resources, which contain top level properties other than sys or fields, have those available as object properties, for example:

locale = client.locales()[0]
locale.default
# True

Advanced Concepts

Logging

To use the logger, use the standard library logging module:

import logging
logging.basicConfig(level=logging.DEBUG)

client.entries()
# INFO:requests.packages.urllib3.connectionpool:Starting new HTTPS connection (1): cdn.contentful.com
# DEBUG:requests.packages.urllib3.connectionpool:"GET /spaces/cfexampleapi/entries HTTP/1.1" 200 1994
Proxy example
client = contentful.Client(
    'cfexampleapi',
    'b4c0n7n37fu1',
    proxy_host='127.0.0.1',
    proxy_port=8000,
    proxy_username='username',
    proxy_password='secrect_password'
)
Synchronization

The client also includes a wrapper for the synchronization endpoint. You can call it either with initial=True or with a previous sync_token, additional options are described in the API Documentation:

sync = client.sync({'initial': True}) # Returns all content currently in space
# <SyncPage next_sync_token='w5ZGw6JFwqZmVcKsE8Kow4grw45QdybCnV_Cg8OASMKpwo1UY8K8bsKFwqJrw7DDhcKnM2RDOVbDt1E-wo7CnDjChMKKGsK1w5zCrA3CnU7CgEvDtsK6w7B2wrRZwrwPIgDCjVo8PMOoUcK2wqTCl8O1wpY8wpjCkGM'>

sync.items
# [<Entry[1t9IbcfdCk6m04uISSsaIK] id='5ETMRzkl9KM4omyMwKAOki'>,
#   <Entry[1t9IbcfdCk6m04uISSsaIK] id='7qVBlCjpWE86Oseo40gAEY'>,
#   <Entry[1t9IbcfdCk6m04uISSsaIK] id='ge1xHyH3QOWucKWCCAgIG'>,
#   <Entry[1t9IbcfdCk6m04uISSsaIK] id='4MU1s3potiUEM2G4okYOqw'>,
#   <Asset id='1x0xpXu4pSGS4OukSyWGUK' url='//images.contentful.com/cfexampleapi/1x0xpXu4pSGS4OukSyWGUK/cc1239c6385428ef26f4180190532818/doge.jpg'>,
#   <Entry[dog] id='jake'>,
#   <Entry[cat] id='happycat'>,
#   <Entry[dog] id='6KntaYXaHSyIw8M6eo26OK'>,
#   <Entry[human] id='finn'>,
#   <Entry[cat] id='nyancat'>,
#   <Asset id='jake' url='//images.contentful.com/cfexampleapi/4hlteQAXS8iS0YCMU6QMWg/2a4d826144f014109364ccf5c891d2dd/jake.png'>,
#   <Asset id='happycat' url='//images.contentful.com/cfexampleapi/3MZPnjZTIskAIIkuuosCss/382a48dfa2cb16c47aa2c72f7b23bf09/happycatw.jpg'>,
#   <Asset id='nyancat' url='//images.contentful.com/cfexampleapi/4gp6taAwW4CmSgumq2ekUm/9da0cd1936871b8d72343e895a00d611/Nyan_cat_250px_frame.png'>,
#   <Entry[cat] id='garfield'>]


sync = client.sync({'initial': True, 'type': 'Deletion'}) # Only returns deleted entries and assets
# <SyncPage next_sync_token='w5ZGw6JFwqZmVcKsE8Kow4grw45QdybCnV_Cg8OASMKpwo1UY8K8bsKFwqJrw7DDhcKnM2RDOVbDt1E-wo7CnDjChMKKGsK1w5zCrA3CnU7CgEvDtsK6w7B2wrRZwrwPIgDCjVo8PMOoUcK2wqTCl8O1wpY8wpjCkGM'>

sync.items
# [<DeletedEntry id='4rPdazIwWkuuKEAQgemSmO'>,
#    <DeletedAsset id='5c6VY0gWg0gwaIeYkUUiqG'>,
#    <DeletedAsset id='finn'>,
#    <DeletedAsset id='3MZPnjZTIskAIIkuuosCss'>,
#    <DeletedAsset id='4gp6taAwW4CmSgumq2ekUm'>,
#    <DeletedAsset id='1uf1qqyZuEuiwmigoUYkeu'>,
#    <DeletedAsset id='4hlteQAXS8iS0YCMU6QMWg'>,
#    <DeletedEntry id='CVebBDcQsSsu6yKKIayy'>]

sync = sync.next(client) # equivalent to client.sync(sync_token=sync.next_sync_token)

Tutorials & other resources

  • This library is a wrapper around our Contentful Delivery REST API. Some more specific details such as search parameters and pagination are better explained on the REST API reference, and you can also get a better understanding of how the requests look under the hood.

  • Check the Contentful for Python page for Tutorials, Demo Apps, and more information on other ways of using Python with Contentful

Reach out to us

Join our Community Forum Join Community Forum

Join our Community Slack Join Community Slack

You found a bug or want to propose a feature?

Create an issue Create Issue

You need to share confidential information or have other questions?

Submit a support ticket Submit Support Ticket

Get involved

Make a pull request PRs Welcome

We appreciate any help on our repositories. For more details about how to contribute see our CONTRIBUTING.md document.

License

Copyright (c) 2016 Contentful GmbH. See LICENSE for further details.

Code of Conduct

We want to provide a safe, inclusive, welcoming, and harassment-free space and experience for all participants, regardless of gender identity and expression, sexual orientation, disability, physical appearance, socioeconomic status, body size, ethnicity, nationality, level of experience, age, religion (or lack thereof), or other identity markers.

Read our full Code of Conduct

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

contentful-2.4.0.tar.gz (47.4 kB view details)

Uploaded Source

Built Distribution

contentful-2.4.0-py3-none-any.whl (32.7 kB view details)

Uploaded Python 3

File details

Details for the file contentful-2.4.0.tar.gz.

File metadata

  • Download URL: contentful-2.4.0.tar.gz
  • Upload date:
  • Size: 47.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: pdm/2.19.2 CPython/3.10.0 Darwin/24.3.0

File hashes

Hashes for contentful-2.4.0.tar.gz
Algorithm Hash digest
SHA256 b6152ee102643fe7e61e0d5752da76d99b27ffe9f84dd576444da7bb95aa49fd
MD5 a03a6f0d83809685aa05c34320daf5bd
BLAKE2b-256 27667d49e55c19f358cd1a861127c1794769d687500cb45ae1358ab72c96e8e6

See more details on using hashes here.

File details

Details for the file contentful-2.4.0-py3-none-any.whl.

File metadata

  • Download URL: contentful-2.4.0-py3-none-any.whl
  • Upload date:
  • Size: 32.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: pdm/2.19.2 CPython/3.10.0 Darwin/24.3.0

File hashes

Hashes for contentful-2.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d5838da595f49eddb7d14a802d012df9db62878bb570771e313674a2035b6982
MD5 b2374eed042c3d2383b2adf71945d2ea
BLAKE2b-256 80e94332470048b237c45a706832fb3cd3d8463670baf70d28eac5b0a5e9edf4

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page