Skip to main content

Python Cloudflare Analytics Client

Release Notes Downloads GitHub CI Status License: MIT

A Python client for interacting with the Cloudflare GraphQL Analytics API. Simple, AI generated, hand polished.

Note: The official Cloudflare Python library does not support the GraphQL Analytics API endpoints. This library provides dedicated support for querying Cloudflare analytics data.

Installation

uv add cloudflare-analytics

Features

  • Simple, clean API for Cloudflare GraphQL Analytics
  • Built-in retry logic with exponential backoff
  • Type-safe responses using Pydantic models
  • Comprehensive error handling

Usage

There are two ways to initialize the client:

get_analytics_client returns a globally cached singleton instance. It also automatically checks for the CLOUDFLARE_API_TOKEN environment variable if no token is passed.

from cloudflare_analytics import get_analytics_client

# Uses CLOUDFLARE_API_TOKEN environment variable by default
client = get_analytics_client()

# Or pass it explicitly
client = get_analytics_client(api_token="your_cloudflare_api_token")

Option 2: Manual Instantiation

Use CloudflareAnalyticsClient directly if you need to manage multiple instances with different tokens or prefer to avoid global state.

from cloudflare_analytics import CloudflareAnalyticsClient

client = CloudflareAnalyticsClient(api_token="your_cloudflare_api_token")

Executing a Query

Once you have a client instance, you can execute GraphQL queries:

# Execute a GraphQL query
query = '''
query GetStreamMinutes($accountTag: string!, $start: Date, $end: Date) {
  viewer {
    accounts(filter: { accountTag: $accountTag }) {
      streamMinutesViewedAdaptiveGroups(
        filter: { date_geq: $start, date_lt: $end }
        orderBy: [date_ASC]
      ) {
        dimensions { date }
        sum { minutesViewed }
      }
    }
  }
}
'''

response = client.query(
    query,
    variables={
        "accountTag": "your_account_id",
        "start": "2025-10-01",
        "end": "2025-10-28"
    }
)

if response.errors:
    print(f"Errors: {response.errors}")
elif response.data:
    groups = response.data["viewer"]["accounts"][0]["streamMinutesViewedAdaptiveGroups"]
    for group in groups:
        minutes = group["sum"]["minutesViewed"]
        date = group["dimensions"]["date"]
        print(f"Date: {date}, Minutes: {minutes}")

Cloudflare API Key Setup

  • Manage Account > Account API tokens
  • The easiest way to get the account ID is to log into your dashboard and look at the first UID after the domain in the URL.

MIT License


This project was created from iloveitaly/python-package-template

Metadata

Release files for cloudflare-analytics 0.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for cloudflare-analytics 0.2.1
File Size Uploaded Provenance
cloudflare_analytics-0.2.1.tar.gz 3.9 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for cloudflare-analytics 0.2.1
File Interpreter ABI Platform
cloudflare_analytics-0.2.1-py3-none-any.whl Python 3 none any Details

Total release size: 9.0 kB

Release files / cloudflare_analytics-0.2.1.tar.gz

Download URL cloudflare_analytics-0.2.1.tar.gz
Size 3.9 kB
Tags Source
SHA-256 checksum
How to use checksums
6d340a4a98607846192566660f177ac2f625c139e866ee87a4bf4890a3b39f6b
BLAKE2b-256 checksum
How to use checksums
044a8d6dc09bd97624e0ccf13ff81c071a5de0f1712e654bfe9603653f35610d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.11.8 {"installer":{"name":"uv","version":"0.11.8","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / cloudflare_analytics-0.2.1-py3-none-any.whl

Download URL cloudflare_analytics-0.2.1-py3-none-any.whl
Size 5.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1f50c6a39eb8ababda4319b6686b65205fc4a55971d5fa9c6e9addf2445132e4
BLAKE2b-256 checksum
How to use checksums
0ebbd082a08e187d6f73ab2ddc9633ba2f4defbba76029a4a9368bfb1a296827
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.11.8 {"installer":{"name":"uv","version":"0.11.8","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Security for cloudflare-analytics 0.2.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page