Authentication overview
Userstack provides an API to accurately detect and parse User-Agent strings, delivering detailed information about client devices, operating systems, and browsers. To ensure secure and authorized access to this service, Userstack implements an API key-based authentication system. This approach grants each user a unique credential that must be included with every API request. The API key serves as the primary mechanism for identifying and validating the requesting application or user, thereby controlling access to Userstack's data and preventing unauthorized usage.
The system is designed for simplicity and efficiency, allowing developers to integrate Userstack's capabilities into their applications with minimal overhead. By requiring an API key, Userstack can track usage against subscription plans, enforce rate limits, and maintain the integrity of its service. This method aligns with common practices for web service authentication, balancing ease of use with necessary security measures for data access.
Supported authentication methods
Userstack exclusively uses API key authentication for accessing its User-Agent String API. This method involves generating a unique string of characters (the API key) that acts as a secret token. When an application makes a request to the Userstack API, this key must be included, typically as a query parameter in the request URL. The Userstack server then validates the provided key against its records to determine if the request is authorized. If the key is valid, the API processes the request and returns the requested data; otherwise, it returns an authentication error.
This single-method approach simplifies the authentication process for developers and reduces potential configuration complexities. While robust methods like OAuth 2.0 or mutual TLS offer higher security for sensitive operations, API keys are widely accepted and suitable for services where the primary concern is controlled access to data rather than delegated authorization for user data, as is the case for Userstack's public data API. For further understanding of API key security, consult the Cloudflare API Key Management documentation.
| Method | When to Use | Security Level |
|---|---|---|
| API Key (Query Parameter) | All Userstack API requests | Moderate (requires secure key handling) |
Getting your credentials
To begin using the Userstack API, you must first obtain your unique API key. This key is generated automatically upon successful registration for a Userstack account. Follow these steps to locate your API key:
- Sign Up or Log In: Navigate to the Userstack homepage and either register for a new account or log in to an existing one. A free tier is available, offering 10,000 requests per month, which includes API key access.
- Access Your Dashboard: After logging in, you will be redirected to your personal Userstack API Dashboard. This dashboard is the central hub for managing your account, viewing usage statistics, and accessing your API key.
- Locate the API Key: Your API key will be prominently displayed on the dashboard, typically labeled as "Your API Key" or similar. It is a long, alphanumeric string.
- Copy Your Key: Copy the entire API key. This key is sensitive and should be treated as a secret. Do not hardcode it directly into client-side code that could be publicly exposed, and avoid committing it to public version control repositories.
For detailed instructions and visual aids, refer to the Userstack API documentation, which provides a comprehensive guide to getting started and managing your account.
Authenticated request example
Once you have obtained your API key, you can integrate it into your API requests. The Userstack API requires the API key to be passed as a query parameter named access_key. All requests must be made over HTTPS to ensure the secure transmission of your API key and data.
Here's an example using curl, demonstrating how to make an authenticated request to the Userstack API:
curl "http://api.userstack.com/api/detect?access_key=YOUR_API_KEY&ua=Mozilla/5.0%20(Windows%20NT%2010.0;%20Win64;%20x64)%20AppleWebKit/537.36%20(KHTML,%20like%20Gecko)%20Chrome/124.0.0.0%20Safari/537.36"
In this example:
http://api.userstack.com/api/detectis the base endpoint for the Userstack detection API.access_key=YOUR_API_KEYis where you replaceYOUR_API_KEYwith the actual API key from your Userstack dashboard.ua=...is the URL-encoded User-Agent string that you want to analyze.
Userstack also provides official SDKs for PHP, Python, and jQuery, which abstract away the raw HTTP request details and simplify the integration of the API key. For instance, in Python, an API request might look like this:
import requests
api_key = "YOUR_API_KEY"
user_agent_string = "Mozilla/5.0 (iPhone; CPU iPhone OS 13_5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1.1 Mobile/15E148 Safari/604.1"
params = {
"access_key": api_key,
"ua": user_agent_string
}
response = requests.get("http://api.userstack.com/api/detect", params=params)
data = response.json()
print(data)
Remember to always use HTTPS for production environments to encrypt the transmission of your API key and protect it from interception. While the examples above use HTTP for brevity, Userstack's actual endpoints support and enforce HTTPS for all requests.
Security best practices
Securing your Userstack API key is crucial to prevent unauthorized access to your account and API usage. Adhering to these best practices will help maintain the integrity of your integrations:
- Use HTTPS exclusively: Always make API requests over HTTPS. This encrypts the communication between your application and the Userstack API, protecting your API key and data from eavesdropping during transit. Userstack's API explicitly supports and recommends HTTPS for all calls to ensure secure transmission.
- Store API keys securely: Never hardcode API keys directly into your application's source code, especially for client-side applications or publicly accessible repositories. Instead, store them in environment variables, secret management services (like AWS Secrets Manager or Google Secret Manager), or configuration files that are not publicly exposed. For server-side applications, retrieve the key at runtime. Guidance on secure credential storage is provided by organizations like the IETF in RFC 6749 regarding client credentials.
- Restrict API key exposure: Avoid exposing API keys in client-side code (e.g., JavaScript in web browsers) where they can be easily extracted by malicious actors. If your application needs to call Userstack from a client, consider proxying requests through your own secure backend server, which can add the API key before forwarding the request to Userstack.
- Implement IP whitelisting (if available): If Userstack offers IP whitelisting features, configure your API key to only accept requests originating from a predefined list of trusted IP addresses. This adds an extra layer of security, as even if your key is compromised, it cannot be used from unauthorized locations. Check the Userstack dashboard for this feature.
- Monitor API key usage: Regularly review your Userstack API usage statistics through your dashboard. Unusual spikes in requests or activity from unexpected locations could indicate a compromised API key. Promptly revoke and regenerate keys if suspicious activity is detected.
- Rotate API keys periodically: Although not strictly necessary for all API key implementations, rotating your API keys on a regular schedule (e.g., every 90 days) can reduce the risk associated with a long-lived, potentially compromised key. Check your Userstack dashboard for options to regenerate your API key.
- Understand rate limits: Familiarize yourself with Userstack's rate limits. While not a direct security measure, understanding these limits helps prevent denial-of-service attacks against your own application if a compromised key is used to make excessive requests.