Make your first Radar API request
To make your first request to Cloudflare’s Radar API, you must obtain your API token first. Create a Custom Token, with User > User Details in the Permissions group, and select Read as the access level.
Once you have the token, you are ready to make your first request to Radar’s API at https://api.cloudflare.com/client/v4/radar/
.
Example using cURL
In the following example, we will access the global percentage distribution of device types (like mobile and desktop traffic) for the last seven days. For more information, refer to Get device types summary endpoint:
curl -X GET "https://api.cloudflare.com/client/v4/radar/http/summary/device_type?dateRange=7d&format=json" \-H "Authorization: Bearer <API_TOKEN>"
A successful response will look similar to the following:
{"success": true,"errors": [],"result": {"summary_0": {"desktop": "58.223483","mobile": "41.725833","other": "0.050684"},"meta": {"dateRange": {"startTime": "2022-10-26T14:00:00Z","endTime": "2022-11-02T14:00:00Z"},"normalization": "PERCENTAGE",...}}}
This response means that 41% of the requests are classified as coming from mobile devices, while 58% are desktop traffic.
The previous example returns all traffic from bots and humans. However, you can access just the traffic classified as coming from humans (the default in
Cloudflare Radar) by adding botClass=LIKELY_HUMAN
. You can also access traffic coming only from bots with botClass=LIKELY_AUTOMATED
(refer to bot classes for more information). For example:
curl -X GET "https://api.cloudflare.com/client/v4/radar/http/summary/device_type?dateRange=7d&botClass=LIKELY_AUTOMATED&format=json" \-H "Authorization: Bearer <API_TOKEN>"
Running the above, can you find any differences between both in the distribution of mobile versus desktop traffic?
Use Python
Python has become one of the standard languages in data analysis. Here is a quick example on how to chart the same data using
Requests and
Pandas libraries. Here, we are using format=csv
in the parameters to make it easier for Pandas to import.
import ioimport requestsimport pandas as pdcf_api_url = "https://api.cloudflare.com/client/v4"params = "dateRange=7d&format=csv"my_token = "xxx" # TODO replacer = requests.get(f"{cf_api_url}/radar/http/summary/device_type?{params}",headers={"Authorization": f"Bearer {my_token}"})df = pd.read_csv(io.StringIO(r.text))df.plot(kind="bar", stacked=True)
Notebooks
A notebook is a web-based interactive computing application, where text, code, and code outputs, like charts, can be combined into a single document. Refer to Radar’s companion colaboratory notebook for more examples on how the API can be used in your own projects.
Next steps
Refer to Make comparisons to learn how to compare data.