Cloudflare Python SDK

4.3.1 · active · verified Thu Apr 09

The official Python library for interacting with the Cloudflare API. It provides a comprehensive client for accessing various Cloudflare services. The current stable version is 4.3.1. The library follows a frequent release cadence, often introducing new features and occasionally breaking changes due to underlying API updates.

Warnings

Install

Imports

Quickstart

This quickstart initializes the Cloudflare client using an API Token from an environment variable and then lists your Cloudflare zones. It demonstrates basic API interaction and error handling.

import os
import Cloudflare

# Ensure CLOUDFLARE_API_TOKEN is set in your environment
# Example: export CLOUDFLARE_API_TOKEN="your_api_token"

cf = Cloudflare.Cloudflare(token=os.environ.get('CLOUDFLARE_API_TOKEN', ''))

try:
    # Example: List all zones associated with the API Token
    zones = cf.zones.get()
    print(f"Found {len(zones)} zones:")
    for zone in zones:
        print(f"- {zone['name']} (ID: {zone['id']})")

    if zones:
        # Example: Get details for the first zone
        first_zone_id = zones[0]['id']
        zone_details = cf.zones.get(first_zone_id)
        print(f"\nDetails for {zone_details['name']}: {zone_details['status']}")

except Cloudflare.CloudflareAPIError as e:
    print(f"Cloudflare API Error: {e}")
except Exception as e:
    print(f"An unexpected error occurred: {e}")

view raw JSON →