Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Comprehensive Cloudflare platform skill covering Workers, D1, R2, KV, AI, Durable Objects, and security.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/graphql-api/configuration.md
1# GraphQL Analytics API Configuration23## Authentication45### API Token (Recommended)67| Permission | Scope | Use Case |8|------------|-------|----------|9| **Account Analytics: Read** | Account-wide | Workers, R2, KV, D1, DO, AI, Network Analytics |10| **Zone Analytics: Read** | Per-zone | HTTP requests, Firewall, DNS, Load Balancing |11| **All zones - Analytics: Read** | All zones | Multi-zone HTTP/Firewall/DNS queries |1213Create tokens at: [dash.cloudflare.com > Account API Tokens](https://dash.cloudflare.com/?to=/:account/api-tokens)1415```bash16# Verify token17curl -s https://api.cloudflare.com/client/v4/graphql \18-H "Authorization: Bearer $CF_API_TOKEN" \19-H "Content-Type: application/json" \20--data '{"query":"{ viewer { zones(filter: {zoneTag: \"ZONE_ID\"}) { httpRequestsAdaptiveGroups(limit: 1, filter: {datetime_gt: \"2025-01-01T00:00:00Z\"}) { count } } } }"}'21```2223### API Key + Email (Legacy)2425Not recommended. Use `X-Auth-Email` + `X-Auth-Key` headers instead of `Authorization: Bearer`.2627## Client Setup2829### curl3031```bash32curl -s https://api.cloudflare.com/client/v4/graphql \33-H "Authorization: Bearer $CF_API_TOKEN" \34-H "Content-Type: application/json" \35--data '{36"query": "query($zoneTag: string!, $start: Time!, $end: Time!) { viewer { zones(filter: {zoneTag: $zoneTag}) { httpRequestsAdaptiveGroups(filter: {datetime_gt: $start, datetime_lt: $end}, limit: 10, orderBy: [datetimeFiveMinutes_DESC]) { count dimensions { datetimeFiveMinutes } } } } }",37"variables": { "zoneTag": "ZONE_ID", "start": "2025-01-01T00:00:00Z", "end": "2025-01-02T00:00:00Z" }38}' | jq .39```4041### TypeScript / JavaScript4243```typescript44const GRAPHQL_ENDPOINT = "https://api.cloudflare.com/client/v4/graphql";4546async function queryGraphQL<T>(query: string, variables: Record<string, unknown> = {}): Promise<T> {47const response = await fetch(GRAPHQL_ENDPOINT, {48method: "POST",49headers: { Authorization: `Bearer ${process.env.CF_API_TOKEN}`, "Content-Type": "application/json" },50body: JSON.stringify({ query, variables }),51});52if (!response.ok) throw new Error(`HTTP ${response.status}: ${response.statusText}`);53const json = await response.json() as { data: T | null; errors?: { message: string }[] };54if (json.errors?.length) throw new Error(json.errors.map((e) => e.message).join("; "));55return json.data!;56}57```5859### Python6061```python62import requests, os6364def query_graphql(query: str, variables: dict = None) -> dict:65r = requests.post("https://api.cloudflare.com/client/v4/graphql",66headers={"Authorization": f"Bearer {os.environ['CF_API_TOKEN']}", "Content-Type": "application/json"},67json={"query": query, "variables": variables or {}})68r.raise_for_status()69result = r.json()70if result.get("errors"):71raise Exception("; ".join(e["message"] for e in result["errors"]))72return result["data"]73```7475### From a Cloudflare Worker7677Store the API token as a secret (`CF_API_TOKEN`). Use standard `fetch` to POST to `https://api.cloudflare.com/client/v4/graphql` with the same JSON body format as above. Always check `response.errors` — GraphQL returns 200 even on query failures.7879## GraphQL API Explorer8081Interactive explorer at [graphql.cloudflare.com](https://graphql.cloudflare.com/) — provides schema docs, autocomplete, variable panel, and shareable queries. Authenticates via your Cloudflare dashboard session.8283## Schema Introspection8485```graphql86# List zone-scoped datasets87{ __type(name: "zone") { fields { name description } } }8889# List account-scoped datasets90{ __type(name: "account") { fields { name description } } }9192# Discover dimensions for a dataset93{ __type(name: "ZoneHttpRequestsAdaptiveGroupsDimensions") {94fields { name type { name kind } }95} }9697# Discover filter operators for a dataset98{ __type(name: "ZoneHttpRequestsAdaptiveGroupsFilter_InputObject") {99inputFields { name type { name kind } }100} }101```102103## Finding Your Zone and Account IDs104105- **Zone ID**: Dashboard > select zone > Overview (right sidebar), or via API106- **Account ID**: Dashboard > Account Home URL, or via API107108```bash109curl -s https://api.cloudflare.com/client/v4/zones -H "Authorization: Bearer $CF_API_TOKEN" | jq '.result[] | {name, id}'110curl -s https://api.cloudflare.com/client/v4/accounts -H "Authorization: Bearer $CF_API_TOKEN" | jq '.result[] | {name, id}'111```112113## See Also114115- [README.md](README.md) - Overview, decision tree, dataset index116- [api.md](api.md) - Query structure, aggregation fields, filtering operators117- [patterns.md](patterns.md) - Common query patterns (time-series, top-N, per-product)118- [gotchas.md](gotchas.md) - Rate limits, sampling, troubleshooting119