05 – Real-World Python Projects – REST API Integration

๐ŸŽฏ Project Objective

To build a Python application that can interact with REST APIs to retrieve, manipulate, and display data in real-time.

Skills Demonstrated:

  • Sending GET and POST requests
  • Handling JSON responses
  • Integrating external data into Python projects
  • Automating API-based workflows

Project: REST API Integration App

Project Description

The REST API Integration app demonstrates how to fetch data from online APIs and use it within Python programs. Examples include:

  • Getting weather data from OpenWeatherMap
  • Fetching cryptocurrency prices from CoinGecko API
  • Retrieving COVID-19 statistics
  • Sending automated messages or notifications

Real-Life Example: Fetch current weather data for a city.


Python Example Code โ€“ Fetching Weather Data

import requests

api_key = "your_api_key"  # Get from https://openweathermap.org/api
city = "London"
url = f"http://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}&units=metric"

response = requests.get(url)
data = response.json()

if response.status_code == 200:
    print(f"Weather in {city}:")
    print("Temperature:", data['main']['temp'], "ยฐC")
    print("Humidity:", data['main']['humidity'], "%")
    print("Condition:", data['weather'][0]['description'])
else:
    print("Error fetching data:", data.get("message", "Unknown error"))

โœ… Outputs: Temperature, humidity, and weather condition for the specified city.


Example โ€“ Fetch Cryptocurrency Prices

import requests

url = "https://api.coingecko.com/api/v3/simple/price?ids=bitcoin,ethereum&vs_currencies=usd"
response = requests.get(url)
data = response.json()

for coin, info in data.items():
    print(f"{coin.title()} Price: ${info['usd']}")

Example โ€“ POST Request to an API

import requests

url = "https://jsonplaceholder.typicode.com/posts"
payload = {
    "title": "Python API Test",
    "body": "This is a test post from Python",
    "userId": 1
}

response = requests.post(url, json=payload)
print("Response:", response.json())

โœ… Demonstrates sending data to an API endpoint.


โœ… Key Features

  • Fetch data from public REST APIs
  • Handle JSON responses
  • Extract and process specific fields
  • Send GET and POST requests
  • Store or display API data for analysis

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *