Device Detection API: The Complete Guide

Need to identify device types for your application? This guide covers everything you need to know about device detection via API, including mobile identification, capability detection, and implementation examples.

What is Device Detection?

Device detection is the process of identifying the type of device accessing your application based on various signals, primarily the user agent string. This information helps deliver optimized experiences for different device capabilities.

Device detection goes beyond simple mobile/desktop categorization to identify specific device models, vendors, and capabilities.

Device Categories

Devices are typically classified into these categories:

Category Examples Characteristics
MobileiPhone, Android phonesSmall screen, touch, cellular
TabletiPad, Android tabletsMedium screen, touch
DesktopWindows PC, MacLarge screen, keyboard/mouse
Smart TVSamsung TV, Apple TVLarge screen, remote
ConsolePlayStation, XboxGaming controllers
WearableApple Watch, FitbitTiny screen, sensors
Pro Tip: Always combine device detection with responsive design. Device detection should enhance, not replace, fluid layouts.

Using the Device Detection API

TinyFn provides an endpoint to detect device information:

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

{
  "user_agent": "Mozilla/5.0 (iPad; CPU OS 17_0 like Mac OS X) AppleWebKit/605.1.15"
}
Response
{
  "device": {
    "type": "tablet",
    "vendor": "Apple",
    "model": "iPad",
    "is_mobile": false,
    "is_tablet": true,
    "is_desktop": false,
    "is_smart_tv": false,
    "is_bot": false
  },
  "capabilities": {
    "touch_enabled": true,
    "screen_size": "medium",
    "javascript": true,
    "cookies": true
  }
}

Parameters

Parameter Type Description
user_agent string User agent string (required)
client_hints object Client hints headers for enhanced detection

Code Examples

JavaScript / Node.js

const response = await fetch('https://api.tinyfn.io/v1/detect/device', {
  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();

if (data.device.is_mobile) {
  // Load mobile-optimized resources
  loadMobileStyles();
} else if (data.device.is_tablet) {
  // Tablet-specific layout
  loadTabletStyles();
}

Python

import requests

def detect_device(user_agent):
    response = requests.post(
        'https://api.tinyfn.io/v1/detect/device',
        json={'user_agent': user_agent},
        headers={'X-API-Key': 'your-api-key'}
    )
    return response.json()

# Example usage
ua = request.headers.get('User-Agent')
device_info = detect_device(ua)
print(f"Device type: {device_info['device']['type']}")

cURL

curl -X POST "https://api.tinyfn.io/v1/detect/device" \
  -H "X-API-Key: your-api-key" \
  -H "Content-Type: application/json" \
  -d '{"user_agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 17_0 like Mac OS X)"}'

Common Use Cases

  • Responsive Images: Serve appropriately sized images for each device
  • Feature Toggling: Enable/disable features based on capabilities
  • Analytics: Track device distribution in your audience
  • A/B Testing: Test different experiences per device type
  • App Promotion: Show app download banners on mobile only

Best Practices

  1. Use as enhancement: Build responsive first, then enhance with detection
  2. Cache results: Cache device detection results per user agent
  3. Handle unknowns: Always have fallback behavior for unknown devices
  4. Consider Client Hints: Use UA Client Hints for more reliable detection

Try the Device Detection API

Get your free API key and start detecting devices in seconds.

Get Free API Key

Ready to try TinyFn?

Get your free API key and start building in minutes.

Get Free API Key