Skip to content

Vijay-Solomon/python-weather-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Python Weather API Backend

This is a simple, secure backend server built with Python and Flask. It acts as a secure proxy to fetch weather data from the OpenWeatherMap API.

Why use a backend?

This server solves a critical security problem: it hides your secret API key.

You should never put your secret API key directly into a frontend (HTML, JavaScript, React) file. If you do, anyone can steal it from your website's code and use it, which could cost you money.

The Correct Flow:

Frontend (Your App) asks this server for weather (e.g., /api/weather?city=London).

This Server (Python/Flask) receives the request.

It securely adds your secret API key and calls the OpenWeatherMap API.

OpenWeatherMap sends the weather data back to this server.

This server sends the clean data back to your frontend.

Features

Secure API Key: Your secret key is stored in a .env file and never exposed to the user.

Simple Endpoint: Get current weather by city with GET /api/weather.

Error Handling: Provides clear JSON error messages for common issues (e.g., "City not found").

Setup and Installation

Follow these steps to get the server running on your local machine.

  1. Clone the Repository

git clone https://github.com/your-username/python-weather-api.git cd python-weather-api

  1. Create a Virtual Environment

It's a best practice to use a virtual environment to manage project dependencies.

For macOS/Linux

python3 -m venv venv source venv/bin/activate

For Windows

python -m venv venv .\venv\Scripts\activate

  1. Install Dependencies

Install all the required Python packages from requirements.txt.

pip install -r requirements.txt

  1. Get Your API Key

Go to OpenWeatherMap and sign up for a free account.

Navigate to your account dashboard and find your API key.

  1. Create Your .env File

Rename the .env.example file to .env.

Open the .env file and paste your secret API key into it.

This is your new .env file

WEATHER_API_KEY=YOUR_SECRET_API_KEY_GOES_HERE

The .gitignore file is already set up to ignore .env, so you will never accidentally commit your key to GitHub.

How to Run the Server

With your virtual environment active and your .env file created, run the app:

This will start the server in development mode

flask run

You can also run it with pure Python:

python app.py

The server will be running at http://127.0.0.1:5000.

How to Use the API

You can now test the API endpoint using your browser or a tool like Postman.

GET /api/weather

Fetches the current weather for a specified city.

Query Parameter:

city (required): The name of the city you want to query.

Example Request: http://127.0.0.1:5000/api/weather?city=London

✅ Success Response (200)

{ "base": "stations", "clouds": { "all": 40 }, "cod": 200, "coord": { "lat": 51.5085, "lon": -0.1257 }, "dt": 1678886400, "id": 2643743, "main": { "feels_like": 9.2, "humidity": 76, "pressure": 1012, "temp": 10.0, "temp_max": 11.1, "temp_min": 8.9 }, "name": "London", "sys": { "country": "GB", "id": 2019646, "sunrise": 1678868400, "sunset": 1678910400, "type": 2 }, "timezone": 0, "visibility": 10000, "weather": [ { "description": "scattered clouds", "icon": "03d", "id": 802, "main": "Clouds" } ], "wind": { "deg": 240, "speed": 5.14 } }

❌ Error Response: City Not Found (404)

Request: http://127.0.0.1:5000/api/weather?city=FakeCityName

{ "error": "City not found" }

❌ Error Response: Missing City (400)

Request: http://127.0.0.1:5000/api/weather

{ "error": "City parameter is required" }

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages