Contentful Python SDK

2.5.0 · active · verified Thu Apr 16

The Contentful Python SDK (`contentful.py`) is an actively maintained client library for the Contentful Content Delivery API (CDA). It enables Python applications to programmatically retrieve published content from Contentful spaces. Key features include content retrieval, synchronization, localization support, link resolution, and built-in rate-limiting recovery. The current stable version is 2.5.0.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart initializes the Contentful client using environment variables for credentials and fetches the first entry of content type 'cat'. Ensure you have `CONTENTFUL_SPACE_ID` and `CONTENTFUL_ACCESS_TOKEN` set, or it will fall back to a public example space.

import os
from contentful import Client

SPACE_ID = os.environ.get('CONTENTFUL_SPACE_ID', 'cfexampleapi') # Use example space for quick demo if not set
ACCESS_TOKEN = os.environ.get('CONTENTFUL_ACCESS_TOKEN', 'b4c0n73n7fu1') # Use example token for quick demo if not set

if not SPACE_ID or not ACCESS_TOKEN:
    print("Please set CONTENTFUL_SPACE_ID and CONTENTFUL_ACCESS_TOKEN environment variables.")
else:
    try:
        client = Client(SPACE_ID, ACCESS_TOKEN)
        entries = client.entries({'content_type': 'cat', 'limit': 1})

        if entries:
            first_cat = entries[0]
            print(f"Found entry: {first_cat.sys['id']}")
            print(f"Name: {getattr(first_cat, 'name', 'N/A')}")
            if getattr(first_cat, 'image', None):
                print(f"Image URL: {first_cat.image.url()}")
        else:
            print("No entries found for content type 'cat'.")

    except Exception as e:
        print(f"An error occurred: {e}")

view raw JSON →