Authentication overview

Purple Air offers a public API that allows developers to access its network of air quality sensors, providing real-time and historical data. Authentication for the Purple Air API is primarily managed through API keys, which serve as a credential to authorize requests and control access permissions. This system ensures that only authorized applications can query the extensive dataset collected by Purple Air's global sensor network.

The API key model is a common approach for web services, enabling straightforward integration while providing a mechanism for API providers to monitor usage and enforce rate limits. Users are typically issued unique keys that identify their application or user account when making requests to the Purple Air API endpoints. These keys are typically passed as part of the request headers or query parameters, depending on the specific API design, as detailed in the Purple Air API reference.

Purple Air distinguishes between different types of API keys based on their intended use:

  • Read Keys: Used for retrieving public sensor data. These keys are suitable for most applications that consume air quality information.
  • Write Keys: Required for managing Purple Air sensors, such as configuring device settings. These keys have elevated permissions and must be handled with greater care.

Understanding the scope of each key type is crucial for implementing secure and functional integrations with Purple Air's platform.

Supported authentication methods

Purple Air's API primarily relies on API keys for authentication. This method is widely adopted for its simplicity and effectiveness in controlling access to public APIs. While other authentication mechanisms exist, such as OAuth 2.0 or mutual TLS, Purple Air focuses on API keys to streamline developer integration for data access.

API Keys

API keys are unique identifiers that authenticate a user or application when making requests to an API. For Purple Air, API keys are generated through the user's account portal and are associated with specific permissions (read or write). When a request is made, the API key is included to verify the sender's identity and authorize the action.

The following table summarizes the authentication method supported by Purple Air:

Method When to Use Security Level
API Key (Read) Accessing public air quality data (read-only) Moderate (Requires secure key handling)
API Key (Write) Managing Purple Air sensors (read/write) High (Requires stringent key handling due to elevated permissions)

API keys are generally considered a foundational authentication mechanism. For enhanced security in some contexts, they can be combined with other measures like IP whitelisting or rate limiting. The IETF RFC 6750 defines bearer token usage, which shares principles with API key handling, emphasizing the importance of keeping such tokens confidential.

Getting your credentials

To access the Purple Air API, you need to obtain an API key. This process is initiated through your Purple Air account. Follow these steps to generate your credentials:

  1. Create a Purple Air Account: If you don't already have one, register for an account on the official Purple Air website.
  2. Navigate to API Key Management: Log into your Purple Air account. Within your user dashboard or profile settings, look for a section related to 'API Keys' or 'Developer Settings'. The exact navigation may vary, but it is typically found under account management sections, as described in the Purple Air documentation.
  3. Generate a New API Key: Select the option to generate a new API key. You will typically be prompted to specify the type of key you need (e.g., Read Key for data access, Write Key for sensor management). For most data retrieval purposes, a Read Key is sufficient.
  4. Copy Your API Key: Once generated, your API key will be displayed. It is crucial to copy this key immediately and store it securely. For security reasons, API keys are often shown only once and cannot be retrieved later if lost. If a key is lost, you will need to generate a new one.

Purple Air's documentation provides specific instructions for obtaining and using API keys, which should be consulted for the most up-to-date guidance. Remember that Write Keys grant control over your sensors and should be handled with extreme caution, ideally only used in secure, server-side environments.

Authenticated request example

Once you have obtained your Purple Air API Read Key, you can use it to make authenticated requests to retrieve air quality data. The API key is typically included as a header in your HTTP requests. Below is an example using curl to fetch data for a specific sensor:


curl -X GET \
  "https://api.purpleair.com/v1/sensors/12345?fields=humidity,temperature" \
  -H "X-API-Key: YOUR_PURPLEAIR_READ_API_KEY"

In this example:

  • YOUR_PURPLEAIR_READ_API_KEY should be replaced with your actual Purple Air Read API Key.
  • 12345 is a placeholder for a specific sensor ID. You would replace this with the ID of the sensor you wish to query.
  • fields=humidity,temperature specifies the data fields you want to retrieve for the sensor.
  • The -H "X-API-Key: ..." part adds the API key as a custom HTTP header named X-API-Key. This is the standard way Purple Air expects the key to be passed for authentication, as outlined in their API reference documentation.

Always ensure your requests are made over HTTPS to encrypt the communication and protect your API key during transit. Neglecting HTTPS can expose your credentials to interception.

Security best practices

Securing your Purple Air API keys and ensuring the integrity of your integrations is paramount. Adhering to security best practices helps prevent unauthorized access to your account and data.

  • Keep API Keys Confidential: Treat your API keys like passwords. Never embed them directly in client-side code (e.g., JavaScript in a web browser, mobile app code that can be easily decompiled) or commit them to public version control systems (like GitHub). Instead, store them in environment variables or a secure configuration management system on your server, as recommended by general API security guidelines from resources like Microsoft's API security best practices.
  • Use Environment Variables: For server-side applications, store API keys as environment variables. This prevents them from being hardcoded into your application's source code and makes it easier to manage different keys for various environments (development, staging, production).
  • Restrict Key Permissions: Generate API keys with the minimum necessary permissions. For data retrieval, use a Read Key. Only use Write Keys when you explicitly need to manage sensors, and ensure they are handled with the highest level of security.
  • Rotate API Keys Regularly: Periodically generate new API keys and revoke old ones. This practice reduces the risk associated with a compromised key, as its validity period will be limited. If you suspect a key has been compromised, revoke it immediately and generate a new one.
  • Monitor API Usage: Regularly review your API usage logs (if provided by Purple Air) for any unusual activity. Spikes in requests or requests from unexpected geographical locations could indicate a compromised key.
  • Use HTTPS/TLS: Always ensure that all communications with the Purple Air API are conducted over HTTPS (TLS). This encrypts the data in transit, protecting your API key and the data exchanged from eavesdropping. Purple Air API endpoints are designed to enforce HTTPS.
  • Implement IP Whitelisting (if available): If Purple Air offers IP whitelisting, configure your API keys to only accept requests from a predefined list of trusted IP addresses. This adds an extra layer of security, preventing unauthorized requests originating from other IP addresses, even if the key is compromised.
  • Error Handling: Implement robust error handling in your application. Avoid logging API keys in error messages or exposing them through debugging interfaces.
  • Secure Development Lifecycle: Integrate API key security into your overall secure development lifecycle, including code reviews and security testing, to identify and mitigate potential vulnerabilities before deployment.

By following these best practices, developers can significantly enhance the security posture of their applications integrating with the Purple Air API, protecting both their own systems and the integrity of the air quality data.