Getting started overview
Integrating with the PM25.in API involves a sequence of steps to retrieve real-time and historical air quality data, primarily for locations within India. The process begins with account creation, followed by obtaining an API key, and then making authenticated HTTP requests to the API endpoints. Developers typically implement HTTP clients in their chosen programming language to interact with the API.
The PM25.in platform provides a developer API page that details the available endpoints, parameters, and expected response formats. While there are no official client libraries, standard HTTP request methods are sufficient for integration. The API uses a simple key-based authentication model, where the API key is included as a query parameter in each request.
Quick Reference Guide
| Step | What to Do | Where |
|---|---|---|
| 1. Create Account | Register on the PM25.in website. | PM25.in homepage |
| 2. Get API Key | Locate your unique API key in the user dashboard after registration. | PM25.in user dashboard (access after login) |
| 3. Understand Endpoints | Review available API endpoints and parameters. | PM25.in API Reference |
| 4. Construct Request | Formulate an HTTP GET request with your API key and desired parameters. | Your code editor, terminal, or API client |
| 5. Send Request | Execute the HTTP GET request. | Your application or command-line tool (e.g., cURL) |
| 6. Parse Response | Process the JSON data returned by the API. | Your application's data parsing logic |
Create an account and get keys
Access to the PM25.in API requires an active account and an associated API key. This key serves as the authentication credential for all API requests. The signup process is initiated on the PM25.in website.
- Navigate to the PM25.in Homepage: Open your web browser and go to pm25.in.
- Register for an Account: Look for a 'Sign Up' or 'Register' option. This usually involves providing an email address, setting a password, and agreeing to terms of service. An email verification step may be required to activate the account.
- Log In to Your Dashboard: Once registered and verified, log in to your newly created account.
- Locate API Key: Within your user dashboard, there should be a dedicated section for API access or developer settings. Your unique API key will be displayed here. It is a long alphanumeric string. Copy this key, as it will be used in every API call. Treat your API key like a password to prevent unauthorized access to your account and API quota Cloudflare API Key Properties documentation.
- Review API Plans (Optional): The PM25.in API offers various usage tiers, including a free tier supporting up to 5,000 requests per day. If your application requires higher request volumes, consider reviewing the paid plans available on the API pricing page.
It is important to store your API key securely and avoid embedding it directly into client-side code that could be publicly accessible. For server-side applications, storing the key in environment variables or a secure configuration management system is recommended.
Your first request
After obtaining your API key, you can make your first API request. The PM25.in API uses standard HTTP GET requests, and the API key is passed as a query parameter. For this example, we will request real-time air quality data for a specific city, which is a common starting point.
The base URL for the API is generally https://pm25.in/api/, and specific endpoints are appended to this URL. You can find detailed endpoint information on the PM25.in API reference page.
Example: Fetching Real-Time Data for a City
Let's assume you want to retrieve the latest air quality data for New Delhi. A typical endpoint for this might be /api/city/New-Delhi. The API key is usually passed as access_token.
API Endpoint Structure:
GET https://pm25.in/api/city/{city-slug}?access_token={your_api_key}
Replace {city-slug} with the URL-friendly name of the city (e.g., New-Delhi for New Delhi) and {your_api_key} with the key obtained from your dashboard.
Using cURL (Command Line)
cURL is a command-line tool and library for transferring data with URLs. It is useful for testing API endpoints quickly.
curl "https://pm25.in/api/city/New-Delhi?access_token=YOUR_API_KEY"
Replace YOUR_API_KEY with your actual API key. Executing this command in your terminal should return a JSON response containing air quality data for New Delhi.
Using Python
Python's requests library is a popular choice for making HTTP requests.
import requests
import json
api_key = "YOUR_API_KEY" # Replace with your actual API key
city_slug = "New-Delhi"
url = f"https://pm25.in/api/city/{city_slug}?access_token={api_key}"
try:
response = requests.get(url)
response.raise_for_status() # Raise an HTTPError for bad responses (4xx or 5xx)
data = response.json()
print(json.dumps(data, indent=2))
except requests.exceptions.RequestException as e:
print(f"An error occurred: {e}")
This Python script will make the request and print the pretty-formatted JSON response to your console.
Expected JSON Response Structure (Example)
{
"city": "New Delhi",
"data": [
{
"time": "2026-05-29T10:00:00Z",
"aqi": 180,
"pm25": 110,
"pm10": 150,
"o3": 40,
"co": 1.2,
"so2": 8,
"no2": 30
}
// ... potentially more data points
],
"status": "success"
}
The exact fields in the data array may vary based on the specific endpoint and available sensors. Refer to the PM25.in API documentation for specific response schemas.
Common next steps
Once you have successfully made your first API call and parsed the response, consider these common next steps to further integrate PM25.in data into your application:
- Explore Other Endpoints: The PM25.in API offers various endpoints beyond real-time city data, such as historical data, station-specific data, or forecasts. Review the full API reference to identify other datasets relevant to your project.
- Implement Error Handling: Robust applications should handle potential API errors gracefully. This includes checking HTTP status codes (e.g., 400 for bad request, 401 for unauthorized, 404 for not found, 500 for server errors) and parsing error messages from the API response. For example, a
requests.exceptions.HTTPErrorin Python can indicate a non-200 status code Mozilla HTTP status codes reference. - Manage API Key Security: Never hardcode your API key directly into publicly accessible client-side code or version control systems. For web applications, consider using a backend proxy that calls the PM25.in API securely. For server-side apps, use environment variables or a secrets management service.
- Monitor Usage: Keep track of your API request volume to stay within your chosen plan's limits. Your PM25.in dashboard may provide usage statistics. Plan for potential scaling if your application's user base grows.
- Set Up Caching: To reduce API calls and improve application performance, implement strategic caching of API responses, especially for data that doesn't change frequently. Adhere to any caching policies or rate limits specified in the PM25.in terms of service.
- Data Visualization: Consider how you will present the air quality data to your users. Libraries like D3.js, Chart.js, or mapping libraries (e.g., Leaflet, Google Maps API with a custom overlay) can help visualize the data effectively.
- Notifications and Alerts: If your application requires real-time alerts (e.g., when AQI exceeds a certain threshold), integrate a notification service like Twilio SendGrid for email or Twilio SMS for text messages Twilio Send SMS messages documentation.
Troubleshooting the first call
Encountering issues during your first API call is common. Here are some troubleshooting steps:
- Check API Key: Ensure your API key is correctly copied and included as
access_tokenin the URL. A common mistake is a typo or missing a character. Double-check for extra spaces. - Verify Endpoint URL: Confirm that the base URL and the specific endpoint path are accurate as per the PM25.in API documentation. Incorrect spelling or case sensitivity can lead to 404 Not Found errors.
- Review Query Parameters: Make sure all required query parameters (e.g.,
city-slug) are present and correctly formatted. Check for proper URL encoding if passing special characters. - Internet Connectivity: Confirm that your machine or server has active internet connectivity and can reach
https://pm25.in. - HTTP Status Codes:
400 Bad Request: Indicates that the server cannot process the request due to malformed syntax (e.g., incorrect parameters, invalid city slug). Review your request parameters against the API documentation.401 Unauthorized: Usually means your API key is missing or invalid. Re-check the API key in your dashboard and ensure it's correctly passed.403 Forbidden: Could mean your API key is valid but doesn't have permissions for the requested resource, or your daily request limit has been exceeded. Check your PM25.in dashboard for usage statistics.404 Not Found: The requested endpoint or resource does not exist. Verify the URL and specific endpoint path.429 Too Many Requests: You have exceeded the rate limit for your API plan. Wait for the designated period before retrying.5xx Server Errors: These indicate an issue on the PM25.in server side. While less common, if these persist, check the PM25.in website for service announcements or contact their support.
- Use an API Client: Tools like Postman or Insomnia provide a visual interface to construct and test API requests, which can help debug issues more easily than cURL or custom code.
- Check Documentation: The PM25.in API reference is the definitive source for endpoint details, required parameters, and response structures. Always consult it if you encounter unexpected behavior.