User Agent Parser API: The Complete Guide

Need to understand your visitors' browsers and devices? This guide covers everything you need to know about parsing user agent strings via API, including browser detection, OS identification, and implementation examples.

What is a User Agent?

A user agent is a string that identifies the client software making an HTTP request. It typically contains information about the browser, operating system, device type, and rendering engine. Web servers and applications use this information to customize responses.

Example: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36

User Agent Structure

User agent strings typically contain several components:

  • Browser: Name and version of the web browser
  • Engine: Rendering engine (Blink, Gecko, WebKit)
  • Operating System: OS name and version
  • Device: Device type and model (for mobile)
  • Platform: Hardware platform information
Pro Tip: User agent strings are notoriously difficult to parse correctly due to historical compatibility hacks. Use a reliable parser API instead of regex.

Using the User Agent Parser API

TinyFn provides an endpoint to parse user agent strings:

API Request
POST https://api.tinyfn.io/v1/parse/user-agent
Headers: X-API-Key: your-api-key
Content-Type: application/json

{
  "user_agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 17_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.0 Mobile/15E148 Safari/604.1"
}
Response
{
  "browser": {
    "name": "Safari",
    "version": "17.0",
    "major": "17"
  },
  "engine": {
    "name": "WebKit",
    "version": "605.1.15"
  },
  "os": {
    "name": "iOS",
    "version": "17.0"
  },
  "device": {
    "type": "mobile",
    "vendor": "Apple",
    "model": "iPhone"
  },
  "is_mobile": true,
  "is_tablet": false,
  "is_desktop": false,
  "is_bot": false
}

Parameters

Parameter Type Description
user_agent string The user agent string to parse (required)

Code Examples

JavaScript / Node.js

const response = await fetch('https://api.tinyfn.io/v1/parse/user-agent', {
  method: 'POST',
  headers: {
    'X-API-Key': 'your-api-key',
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    user_agent: navigator.userAgent
  })
});
const data = await response.json();
console.log(`Browser: ${data.browser.name} ${data.browser.version}`);
console.log(`OS: ${data.os.name} ${data.os.version}`);
console.log(`Mobile: ${data.is_mobile}`);

Python

import requests

user_agent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36"

response = requests.post(
    'https://api.tinyfn.io/v1/parse/user-agent',
    json={'user_agent': user_agent},
    headers={'X-API-Key': 'your-api-key'}
)
data = response.json()
print(f"Browser: {data['browser']['name']}")
print(f"OS: {data['os']['name']}")

cURL

curl -X POST "https://api.tinyfn.io/v1/parse/user-agent" \
  -H "X-API-Key: your-api-key" \
  -H "Content-Type: application/json" \
  -d '{"user_agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/120.0.0.0"}'

Parsed Data Fields

  • browser: Browser name, version, and major version number
  • engine: Rendering engine name and version
  • os: Operating system name and version
  • device: Device type, vendor, and model
  • is_mobile: Boolean indicating mobile device
  • is_tablet: Boolean indicating tablet device
  • is_bot: Boolean indicating crawler/bot

Best Practices

  1. Don't rely solely on UA: Use feature detection when possible
  2. Cache results: Parse each unique UA once and cache the result
  3. Handle bots: Detect and handle bot traffic appropriately
  4. Stay updated: UA patterns change; use an updated parser

Try the User Agent Parser API

Get your free API key and start parsing user agents in seconds.

Get Free API Key

Ready to try TinyFn?

Get your free API key and start building in minutes.

Get Free API Key