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.

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

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}")

Features

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

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.0.tar.gz (3.8 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.0-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: cloudflare_analytics-0.2.0.tar.gz
  • Upload date:
  • Size: 3.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.10.2 {"installer":{"name":"uv","version":"0.10.2","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.0.tar.gz
Algorithm Hash digest
SHA256 1c052cdfcca253b519aa7eed62429d24e14e27b88c603e1fac7aefac681c9775
MD5 08e97aba83efe1c898cff37f4eb525b7
BLAKE2b-256 2c3b5d86340181ba82df2bd25463a2102a7767b35f8662e46a411c66b31f62a8

See more details on using hashes here.

File details

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

File metadata

  • Download URL: cloudflare_analytics-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 5.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.10.2 {"installer":{"name":"uv","version":"0.10.2","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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 00ddb54e0dec240a49a5fe9385185170776cbfb4441bbd1a49282c788a7f5540
MD5 fab8cfc36aa07a072f6ba19460b3d06b
BLAKE2b-256 f60a0a26cd317da0da08d5c2339d3561362904128540232e8b4256d9c44b83d3

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