Interacting with APIs - Fetching Data from the Web
24th October 2025 By Gururaj
blog

Using APIs to retrieve data from the web is a powerful and structured alternative to web scraping. APIs, or Application Programming Interfaces, allow different systems to communicate with each other in a standardized way, providing access to data or services in a format that’s easy to work with, typically JSON or XML. Unlike scraping, which involves parsing HTML and can break if a website’s structure changes, APIs are designed to deliver consistent, reliable data, making them a preferred choice for developers. In this explanation, we’ll dive deeper into how to use the requests library in Python to interact with a public API, handle JSON responses, and explore practical examples using a weather API and a quote API.

Why Use APIs?

APIs are built by service providers to share data or functionality in a controlled manner. They often come with documentation that outlines available endpoints, required parameters, and the structure of the response data. This makes APIs more predictable and efficient than scraping. For example:

  • Consistency: APIs provide structured data (like JSON) that doesn’t depend on a website’s layout.
  • Reliability: APIs are less likely to break due to changes in a website’s design.
  • Ease of Use: JSON data is straightforward to parse and integrate into applications.
  • Access Control: Many APIs require authentication (e.g., an API key), ensuring secure and authorized access.

Public APIs, like those for weather, quotes, or news, are often free (with usage limits) and great for learning or building small projects.

Getting Started with the requests Library

The requests library in Python simplifies making HTTP requests to APIs. It handles tasks like sending GET or POST requests, passing parameters, and processing responses. To use it, you need to install it first (if not already installed):

bash
 
pip install requests
 
 

Once installed, you can import it in your Python script:

python
 
import requests
 
 

Example 1: Fetching Weather Data

Let’s explore how to use a public weather API, such as OpenWeatherMap, which provides current weather data, forecasts, and more. To use OpenWeatherMap, you typically need to sign up for a free API key, which allows limited requests per day.

Steps:

  1. Get an API Key: Sign up at OpenWeatherMap to get a free API key.
  2. Choose an Endpoint: For example, the endpoint for current weather data is https://api.openweathermap.org/data/2.5/weather.
  3. Make a Request: Use the requests library to send a GET request with parameters like the city name and API key.
  4. Parse the JSON Response: Extract relevant data, such as temperature or weather conditions.

Here’s a sample Python script to fetch the current weather for a city:

python
 
import requests

# API key and city
api_key = "your_api_key_here"  # Replace with your OpenWeatherMap API key
city = "London"
url = f"https://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}&units=metric"

# Send GET request
response = requests.get(url)

# Check if the request was successful
if response.status_code == 200:
    # Parse JSON data
    data = response.json()
    # Extract relevant information
    temperature = data['main']['temp']
    weather = data['weather'][0]['description']
    print(f"Current weather in {city}: {temperature}°C, {weather}")
else:
    print(f"Error: {response.status_code}")
 
 

Explanation:

  • URL Construction: The URL includes the city name, API key, and a parameter (units=metric) to get temperature in Celsius.
  • Response Handling: The response.json() method converts the API’s JSON response into a Python dictionary.
  • Data Extraction: The JSON structure contains nested fields like main.temp for temperature and weather[0].description for conditions.
  • Error Checking: Always verify the response status code (200 means success).

Sample Output:

text
 
Current weather in London: 12.5°C, light rain
 
 

Tips for Weather APIs:

  • OpenWeatherMap provides other endpoints for forecasts, historical data, or air pollution metrics.
  • Be mindful of rate limits (e.g., 60 calls per minute for free accounts).
  • Explore similar APIs like WeatherAPI or AccuWeather for different features.

Example 2: Fetching Random Quotes

For a lighter example, let’s use a public quote API, such as the Quotable API (https://api.quotable.io), which doesn’t require an API key and returns random quotes in JSON format.

Steps:

  1. Choose an Endpoint: Use https://api.quotable.io/random to get a random quote.
  2. Make a Request: Send a GET request using requests.
  3. Parse the JSON: Extract the quote content and author.

Here’s a sample script:

python
 
import requests

# API endpoint for a random quote
url = "https://api.quotable.io/random"

# Send GET request
response = requests.get(url)

# Check if the request was successful
if response.status_code == 200:
    # Parse JSON data
    data = response.json()
    # Extract quote and author
    quote = data['content']
    author = data['author']
    print(f"Quote: {quote}")
    print(f"Author: {author}")
else:
    print(f"Error: {response.status_code}")
 
 

Explanation:

  • Simple Endpoint: No authentication is needed, making it beginner-friendly.
  • JSON Structure: The response includes fields like content (the quote) and author.
  • Error Handling: Checking the status code ensures the request succeeded.

Sample Output:

text
 
Quote: The only way to do great work is to love what you do.
Author: Steve Jobs
 
 

Tips for Quote APIs:

  • Quotable allows filtering by tags or authors (e.g., https://api.quotable.io/quotes?author=albert-einstein).
  • Other quote APIs include ZenQuotes or TheySaidSo for variety.

Working with JSON Data

Both examples return data in JSON format, which Python’s requests library converts into a dictionary. Here are some tips for handling JSON:

  • Accessing Data: Use dictionary methods like data['key'] or data.get('key') to safely access fields.
  • Nested Data: For nested JSON (e.g., data['main']['temp']), check the API documentation for the structure.
  • Error Handling: APIs may return errors (e.g., 404 for invalid city, 401 for invalid API key). Always check response.status_code.
  • Pretty Printing: Use the json library to print JSON for debugging:
    python
     
    import json
    print(json.dumps(data, indent=2))
     
     

Best Practices for Using APIs

  1. Read Documentation: APIs vary in endpoints, parameters, and authentication. Always check the official docs.
  2. Handle Rate Limits: Free APIs often limit requests (e.g., 1000/day). Track usage to avoid errors.
  3. Secure API Keys: Store API keys in environment variables or a config file, not in your code.
python
 
import os
api_key = os.getenv("OPENWEATHER_API_KEY")
 
 
  1. Error Handling: Use try-except blocks to manage network issues or invalid responses:
python
 
try:
    response = requests.get(url)
    response.raise_for_status()  # Raises an error for bad status codes
except requests.RequestException as e:
    print(f"Request failed: {e}")
 
 
  1. Respect Terms of Service: Follow the API provider’s rules to avoid being blocked.

Exploring Other Public APIs

Beyond weather and quotes, many public APIs are available for learning:

  • News API: Fetch news headlines (e.g., https://newsapi.org).
  • NASA API: Access space-related data like Mars rover photos.
  • REST Countries: Get country data like population or capital (e.g., https://restcountries.com).
  • PokéAPI: Retrieve Pokémon data for fun projects (e.g., https://pokeapi.co).

Find more at public-apis.io or GitHub’s public API lists.

Practical Applications

Using APIs with requests opens up many possibilities:

  • Build a weather dashboard that updates in real-time.
  • Create a bot that posts daily quotes to social media.
  • Combine APIs (e.g., weather + news) for a personalized app.
  • Store API data in a database (e.g., SQLite) for analysis.

Conclusion

 

Using the requests library to interact with public APIs is a straightforward way to access structured web data. By working with JSON responses from APIs like OpenWeatherMap or Quotable, you can build reliable, dynamic applications without the fragility of web scraping. Start by experimenting with free APIs, follow their documentation, and handle responses carefully to create robust projects. If you’d like, I can provide more examples or help you build a specific API-based project!