Skip to main content

What is an API?

An API (Application Programming Interface) is like a waiter at a restaurant. You tell it what you want, and it brings you the data. But APIs do more than just fetch information. They’re the bridges that connect your code to other systems. With APIs, you can:
  • Pull customer data from your CRM (Salesforce, HubSpot)
  • Get order information from Shopify or WooCommerce
  • Send messages through Slack or email services
  • Use AI models from OpenAI or Anthropic
In the context of AI, APIs are essential. They let you connect AI capabilities to real business data and systems.

requests Module

The requests module is a popular third-party Python library used to send HTTP requests. It simplifies communication with web servers and REST APIs.
Install the library using:

Common HTTP Methods

Example: GET Request

Example: POST Request

Useful Response Attributes

Common Status Codes

Let’s get real weather data using a free weather API:

First install requests: pip install requests

Understanding the response

The API sends back data in JSON format (like a Python dictionary):
The temperature is nested inside current, so to get it:
What is JSON? JSON (JavaScript Object Notation) is just a way to structure data, similar to CSV or Excel files. While CSV stores data in rows and columns, JSON uses key-value pairs like Python dictionaries.

Extract what you need

Now you can pull out specific information:

How it works

  1. You send a request to the API’s URL with parameters (like coordinates)
  2. The API processes your request and finds the data
  3. You receive JSON data back with the information
  4. You extract the specific parts you need

Practice & Exercises

To reinforce what you’ve learned in this section (making API requests, reading JSON response payloads, extracting nested properties, and querying coordinates dynamically), practice with these interactive notebooks:

Follow-Along Practice

Practice submitting coordinate parameters to weather APIs using requests, extracting nested dictionary properties, and writing query wrappers to compare temperature statistics.💻 VS Code | 🚀 Colab | 📥 Download

Practice Exercises

Test your knowledge with hands-on exercises on querying Indian cities, writing get_weather_details for temperature/wind outputs, and adding exception handling to API calls.💻 VS Code | 🚀 Colab | 📥 Download

What’s next?

Now that you know how to connect to web APIs, let’s learn how to store API keys and database credentials securely using environment variables.

Working with Environment Variables

Learn to manage secrets with .env files