Work with API’s in Python

Introduction

Imagine you want to order a pizza. You call the pizza place, tell them what you want, and they deliver it to your door. You don’t need to know how they make the pizza or how they deliver it; you just need to know the number to call and how to place your order. This is similar to how APIs work in programming.

An API (Application Programming Interface) is a set of rules that allows one piece of software to talk to another. It defines how software components should interact.

Why Are APIs Required?

APIs are important because they allow different software systems to communicate with each other. This means:

  1. Efficiency: You can use existing functionalities without building them from scratch.
  2. Integration: Different systems and applications can work together seamlessly.
  3. Innovation: Developers can create new applications that leverage existing services.

How to Work with APIs

To work with an API, you usually:

  1. Make a request to the API.
  2. The API processes the request.
  3. The API sends back a response.

Example

Let’s say we have an API that gives us the weather information. Here’s a simple way to use it:

  1. Request: You ask the API for the weather in your city.
  2. Response: The API sends back the current weather details.

In Python, you can use the requests library to make API calls.

!pip install requests # if not installed this package

import requests

response = requests.get('https://wttr.in/Mumbai?format=j1')
print(response.json())

This code sends a request to the API and prints the weather information for Mumbai.

Few More Examples

Example 1: Social Media Integration

Description:

The JSONPlaceholder API provides fake data for testing and prototyping. It allows you to make requests and receive mock data, which is useful for developing and testing applications without needing access to real data.

Code:

import requests

response = requests.get('https://jsonplaceholder.typicode.com/posts/1')
print(response.json())

Explanation:

This code sends a request to the JSONPlaceholder API to get the details of a post with ID 1 and prints the response.

Example 2: Public APIs for Weather

Description:

The wttr.in API provides weather information in a simple and free format. It supports various locations and formats.

Code:

import requests

response = requests.get('https://wttr.in/Delhi?format=j1')
print(response.json())

Explanation:

This code sends a request to the wttr.in API to get the current weather information for Delhi and prints the response.

Example 3: Free Public API for Space Data

Description:

The Open Notify API provides information about the current location of the International Space Station (ISS).

Code:

import requests

response = requests.get('http://api.open-notify.org/iss-now.json')
print(response.json())

Explanation:

This code sends a request to the Open Notify API to get the current location of the ISS and prints the response.

API Methods

Common HTTP Methods

APIs use different HTTP methods to perform various actions:

  1. GET: Used to request data from a specified resource.
    • Example: requests.get(url)
  2. POST: Used to send data to a server to create/update a resource.
    • Example: requests.post(url, data)
  3. PUT: Used to update a resource.
    • Example: requests.put(url, data)
  4. DELETE: Used to delete a resource.
    • Example: requests.delete(url)

Example Usage:

GET Request

import requests

response = requests.get('https://jsonplaceholder.typicode.com/posts/1')
print(response.json())

POST Request

import requests

data = {'title': 'foo', 'body': 'bar', 'userId': 1}
response = requests.post('https://jsonplaceholder.typicode.com/posts', json=data)
print(response.json())

PUT Request

import requests

data = {'id': 1, 'title': 'foo', 'body': 'bar', 'userId': 1}
response = requests.put('https://jsonplaceholder.typicode.com/posts/1', json=data)
print(response.json())

DELETE Request

import requests

response = requests.delete('https://jsonplaceholder.typicode.com/posts/1')
print(response.status_code)

Conclusion

APIs are powerful tools that allow different software systems to communicate and work together. By understanding how to use APIs, you can create more efficient and innovative applications. Whether you’re integrating social media, retrieving weather information, using maps, or exploring space data, APIs make it all possible.


Leave a Reply