User-Agent Info is a simple free web service API that do OS detection, Browser detection and Device detection via User-Agent string. The core of this api is based on uap-core Browserscope’s user agent parser which is good enough for most use cases. The excellent open source uap-core project has a single YAML file that contains regular expression patterns for parsing user-agent strings and can be easily integrated in most web applications that runs on PHP, Java, Ruby, Python, Perl, C# and more.

Features

  • OS Detection
  • Browser Detection
  • Device Detection
  • SSL Encryption

Various Use Cases

  • Tailored Advertising
  • Targeted Content
  • User Exeperience

API Usage via Bash/cURL

curl -X POST -H "Content-Type: application/json" \
  -d '{"ua":"Mozilla/5.0 (iPad; CPU OS 5_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Mobile/9B176"}' \
  https://api.useragentinfo.co/device

API Response

{
  "device_model":"iPad",
  "device_brand":"Apple",
  "device_type":"iPad",
  "os":"iOS",
  "os_version":"5.1",
  "browser":"Mobile Safari UI/WKWebView",
  "browser_version":"5.1"
}
Need to deploy your awesome Python, PHP or Ruby web application to the cloud? Check out Digital Ocean, they provide realiable and affordable servers. Their tutorials are also a great resource so you can deploy webapps easily.


How to do device detection in various programming languages?

Device detection in PHP

You can get your website visitor user-agent string via $_SERVER['HTTP_USER_AGENT'].

<?php
// $useragent = $_SERVER['HTTP_USER_AGENT'];
$useragent = 'Mozilla/5.0 (iPad; CPU OS 9_3_2 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13F69 Safari/601.1';
$url = "https://api.useragentinfo.co/device";

$data = array('ua' => $useragent);

$headers = array();
$headers[] = "Content-Type: application/json";

$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));

$json_response = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);

if ($status != 200 ) {
    die("Error: call to URL $url failed with status $status, response $json_response, curl_error " . curl_error($curl) . ", curl_errno " . curl_errno($curl));
}

$result = json_decode($json_response, true);

if ($result['device_type'] === 'iPad') {
  echo "It's an iPad\n";
}

echo $json_response;
curl_close($curl);
?>

Device detection in Python

Flask and Django are two of the most popular Python web frameworks. With Flask you can get the user-agent string via request.headers.get('User-Agent'). In Django via request.META.get('HTTP_USER_AGENT', '').

# python-requests must be installed
import requests

headers = {
    "Content-Type": "application/json",
}
payload = {
    "ua": "Mozilla/5.0 (iPad; CPU OS 5_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Mobile/9B176"
}
url = "https://api.useragentinfo.co/device"
r = requests.post(url, headers=headers, json=payload)
result = r.json()
if result["device_type"] == "iPad":
    print("It's an iPad")
print(result)

Device detection in Ruby

In Ruby on Rails, you can get the user-agent string via request.user_agent.

require 'net/https'
require 'json'
require 'uri'

useragent = 'Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_1 like Mac OS X) AppleWebKit/603.1.30 (KHTML, like Gecko) Version/10.0 Mobile/14E304 Safari/602.1'

body = {"ua" => useragent}

uri = URI.parse("https://api.useragentinfo.co")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
request = Net::HTTP::Post.new("/device")
request.add_field('Content-Type', 'application/json')
request.body = body.to_json
response = http.request(request)
json = JSON.parse(response.body)

if json['device_type'] == 'iPhone'
  print("It's an iPhone\n")
end

puts response.body