Skip to main content

Cloudflare Analytics GraphQL API client

Project description

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:

Option 1: Global Client (Recommended)

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

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

cloudflare_analytics-0.2.1.tar.gz (3.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cloudflare_analytics-0.2.1-py3-none-any.whl (5.1 kB view details)

Uploaded Python 3

File details

Details for the file cloudflare_analytics-0.2.1.tar.gz.

File metadata

  • Download URL: cloudflare_analytics-0.2.1.tar.gz
  • Upload date:
  • Size: 3.9 kB
  • Tags: Source
  • Uploaded using 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}

File hashes

Hashes for cloudflare_analytics-0.2.1.tar.gz
Algorithm Hash digest
SHA256 6d340a4a98607846192566660f177ac2f625c139e866ee87a4bf4890a3b39f6b
MD5 4a787fea1552d6a72ca210e2c6c466cc
BLAKE2b-256 044a8d6dc09bd97624e0ccf13ff81c071a5de0f1712e654bfe9603653f35610d

See more details on using hashes here.

File details

Details for the file cloudflare_analytics-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: cloudflare_analytics-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 5.1 kB
  • Tags: Python 3
  • Uploaded using 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}

File hashes

Hashes for cloudflare_analytics-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1f50c6a39eb8ababda4319b6686b65205fc4a55971d5fa9c6e9addf2445132e4
MD5 e0d4c082e2209982144ba8968a350ac2
BLAKE2b-256 0ebbd082a08e187d6f73ab2ddc9633ba2f4defbba76029a4a9368bfb1a296827

See more details on using hashes here.

Supported by

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