Overview
Get current weather data and forecasts for any location
Integration Guide
Step 1: Get API Key
To use the Weather API, you need to sign up for an account and get an API key.
- Visit Weather API Sign Up
- Create an account and verify your email
- Navigate to your dashboard to find your API key
- Copy your API key for use in your application
Step 2: Set Up Your Project
Create the necessary files for your weather application:
- index.html - The HTML structure
- styles.css - CSS for styling
- weather.js - JavaScript for API integration
Step 3: Implement the Code
Copy the following code into your project files:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Weather API Example</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="weather-container">
<h1>Current Weather</h1>
<div id="weather-data">
<p>Loading weather data...</p>
</div>
<button id="refresh-btn">Refresh</button>
</div>
<script src="weather.js"></script>
</body>
</html>Step 4: Replace the API Key
Replace your_api_key_here in the JavaScript file with your actual API key.
Security Note
In a production environment, you should never expose your API key in client-side code. Use a server-side solution or environment variables instead.
Step 5: Test Your Implementation
Open your HTML file in a browser to test your weather application. You should see the current weather for London (or change the location in the code).
Error Handling
Common Errors
- 401 Unauthorized: Invalid API key or exceeded quota
- 400 Bad Request: Invalid location parameter
- 429 Too Many Requests: Rate limit exceeded
Rate Limiting
The free tier of Weather API allows 1,000,000 calls per month with a rate limit of 1 request per second.
To handle rate limiting:
- Implement exponential backoff for retries
- Cache responses when appropriate
- Monitor your usage in the Weather API dashboard
API Details
- Base URL
https://api.weatherapi.com/v1- Authentication
- API Key
- Documentation
- Official Docs
Endpoints
- GET
/current.jsonGet current weather data for a location
Parameters
key*string - Your API keyq*string - Location name, IP address, or coordinatesaqistring - Include air quality data (yes/no)
- GET
/forecast.jsonGet weather forecast for a location
Parameters
key*string - Your API keyq*string - Location name, IP address, or coordinatesdaysnumber - Number of days of forecast (1-10)aqistring - Include air quality data (yes/no)alertsstring - Include weather alerts (yes/no)