Detect API End Point Sample Code in Python

As discussed in the Integration Guide, you need to send all of your visitor's HTTP Headers to the Detect API. The most important modern addition to this is including all the Client Hints headers as well.

Requesting all Client Hints headers

Your visitor's Web Browsers won't send all their possible Client Hint HTTP headers to your site by default; it's necessary to specifically request that they send them to you. So for you to be able to use the Detect API properly, you have to request them from your visitors; you do this by including some HTTP Response Headers on your site. This will cause your visitor's browsers to send you their Client Hints as they make requests to your servers.

How you add response HTTP Headers depends on your Python framework

So, to receive the Client Hints headers from your visitors, you first must request them from your visitors by adding HTTP Response Headers to your site's responses, but how you make your site do that, depends on the frame work you are using for your site.

Adding Client Hints HTTP Response Headers in Bottle

@app.route('/', method="GET") def show_index(): bottle.response.headers['accept-ch'] = "Sec-Ch-Ua,Sec-Ch-Ua-Full-Version,Sec-Ch-Ua-Platform,Sec-Ch-Ua-Platform-Version,Sec-Ch-Ua-Arch,Sec-Ch-Bitness,Sec-Ch-Ua-Model,Sec-Ch-Ua-Mobile,Device-Memory,Dpr,Viewport-Width,Downlink,Ect,Rtt,Save-Data,Sec-Ch-Prefers-Color-Scheme,Sec-Ch-Prefers-Reduced-Motion,Sec-Ch-Prefers-Contrast,Sec-Ch-Prefers-Reduced-Data,Sec-Ch-Forced-Colors" bottle.response.headers['critical-ch'] = "Sec-Ch-Ua,Sec-Ch-Ua-Full-Version,Sec-Ch-Ua-Platform,Sec-Ch-Ua-Platform-Version,Sec-Ch-Ua-Arch,Sec-Ch-Bitness,Sec-Ch-Ua-Model,Sec-Ch-Ua-Mobile,Device-Memory,Dpr,Viewport-Width,Downlink,Ect,Rtt,Save-Data,Sec-Ch-Prefers-Color-Scheme,Sec-Ch-Prefers-Reduced-Motion,Sec-Ch-Prefers-Contrast,Sec-Ch-Prefers-Reduced-Data,Sec-Ch-Forced-Colors" return bottle.template('homepage')

Visitors to your site who are using a browser that supports Client Hints should now be including all their Client Hints headers in their requests to your site. So when you want to detect the browser they're using, you need to take their headers, bundle them up, and send the request to the API. This is quite easy to do with the powerful Python Requests library:

import requests # -- add all their request headers to a list headers_to_send = [] for header_name in bottle.request.headers: headers_to_send.append({ "name": header_name, "value": bottle.request.headers.get(header_name), }) # -- set up the request headers = { 'X-API-KEY': "YOURAPIKEYHERE", } post_data = { "headers": headers_to_send, } # -- make the request to the whatismybrowser server result = requests.post("https://api.whatismybrowser.com/api/v3/detect", data=json.dumps(post_data), headers=headers) # -- analyse the API response print(result.json()) print(result.json().get("detection").get("simple_software_string")) # etc

Download a working sample file

bottle-detect.py