Querying HTTP events by hostname with GraphQL
In this example, we are going to use the GraphQL Analytics API to query aggregated metrics about HTTP events by hostname over a specific period of time.
The following API call will request the number of visits and edge response bytes for the custom hostname hostname.example.com
over a four day period. Be sure to replace CLOUDFLARE_ZONE_TAG
and API_TOKEN
1 with your zone ID and API credentials, and adjust the datetime_geq
and datetime_leq
values as needed.
The returned results will be in JSON format (as requested), so piping the output to jq
will make them easier to read, like in the following example:
We are going to use the GraphQL Analytics API to query the top 10 consuming URLs from a zone, helping you identify the URLs with the highest resource usage. Here are some configuration instructions:
- To filter on a specific hostname, add the line
"clientRequestHTTPHost": "'$2'"
below"requestSource"
." - Replace
API_TOKEN
with your generated API token using theRead all resources
permissions. The script will only access zones available to the token's creator. - Pass the zone ID (
zoneTag
) as a parameterARG=$1
. - To calculate the current date and the date from 30 days ago, use
gdate
on Mac:CURRENTDATE=$(gdate -u +'%FT%TZ')
OLDDATE=$(gdate -d '-30 days' -u +'%FT%TZ')
.
- For specific dates within the last 30 days, set
CURRENTDATE
andOLDDATE
variables in the format"YYYY-MM-DDTHH:MM:SSZ"
.
-
Refer to Configure an Analytics API token for more information on configuration and permissions. ↩