🌦 Create Weather API Account
Website: https://www.weatherapi.com/
🔹 Step 1 — Create Account
- Open https://www.weatherapi.com
- Click Sign Up
- Register using Gmail
- Verify your email
- Go to Dashboard
🔹 Step 2 — Copy API Key
Inside the dashboard you will see:
Copy it.
Example (not real):
1234567890abcdef
🔐 What is API Key?
API Key = Secret Code
It tells WeatherAPI:
Who is requesting data
Identifies you as the user making the API call.
How many requests used
Tracks your usage against the free tier limit.
Whether access is allowed
Validates your permission to access the API.
🌍 Test API in Browser
Use this format in your browser address bar:
https://api.weatherapi.com/v1/current.json?key=YOUR_API_KEY&q=Chennai
YOUR_API_KEY with your actual API key, then press Enter.You will see a JSON response like this:
{
"location": {
"name": "Chennai"
},
"current": {
"temp_c": 32,
"humidity": 60
}
}
⚛ Create React Project (Using Vite)
Open your terminal and run:
npm create vite@latest weather-project
▸ Framework: React
▸ Variant: JavaScript
Then run the following commands:
cd weather-project
npm install
npm run dev
📦 Install Required Packages
Inside the project folder, run:
npm install axios
📁 Create .env File (Important)
Inside the project root directory, create a new file named:
Add the following content:
VITE_WEATHER_API_KEY=your_api_key_here
your_api_key_here with your actual API key from WeatherAPI.com. Save the file.
VITE_ prefix is required for Vite to expose environment variables to your app. Without it, the variable will not be accessible.🔌 Connect API in React
Open the file:
Replace its entire content with:
import { useState } from "react";
import axios from "axios";
function App() {
const [city, setCity] = useState("");
const [weather, setWeather] = useState(null);
const API_KEY = import.meta.env.VITE_WEATHER_API_KEY;
const getWeather = async () => {
const response = await axios.get(
`https://api.weatherapi.com/v1/current.json?key=${API_KEY}&q=${city}`
);
setWeather(response.data);
};
return (
<div style={{ textAlign: "center", marginTop: "50px" }}>
<h1>Weather App</h1>
<input
type="text"
placeholder="Enter city"
onChange={(e) => setCity(e.target.value)}
/>
<button onClick={getWeather}>Search</button>
{weather && (
<div>
<h2>{weather.location.name}</h2>
<p>Temperature: {weather.current.temp_c}°C</p>
<p>Humidity: {weather.current.humidity}%</p>
<p>Wind Speed: {weather.current.wind_kph} km/h</p>
</div>
)}
</div>
);
}
export default App;
🧠 What does this code do?
| Code | Explanation |
|---|---|
useState("") | Stores the city name the user types |
useState(null) | Stores the weather data from API |
import.meta.env | Reads the API key from .env file |
axios.get(...) | Sends a GET request to WeatherAPI |
setWeather(response.data) | Stores the response in state |
{weather && (...)} | Shows weather info only when data exists |
Save the file and restart your dev server:
npm run dev
🎨 Improve UI (Optional)
To enhance the look and feel, install these optional packages:
npm install framer-motion recharts react-icons
framer-motion
Adds smooth animations and transitions to your components.
recharts
Create beautiful, responsive charts to visualize weather data.
react-icons
Thousands of icons to make your UI more expressive.
🐙 Push to GitHub
Then run these commands in your terminal:
git init
git add .
git commit -m "Initial weather project"
git branch -M main
git remote add origin https://github.com/yourusername/weather-project.git
git push -u origin main
yourusername with your actual GitHub username.
.env file is listed in .gitignore so your API key is never pushed to GitHub!🌐 Deploy on Netlify
🔹 Deployment Steps
- Login with your GitHub account
- Click "Add new site"
- Select "Import from GitHub"
- Select your weather-project repository
🔹 Build Settings
| Build command: | npm run build |
| Publish directory: | dist |
🔹 Add Environment Variable
| Key: | VITE_WEATHER_API_KEY |
| Value: | Your actual API key |
Click Deploy.
You will get a URL like:
https://random-name.netlify.app