How to fetch data from API

What is API ?

API stands for Application programming interface.

API is like a messenger that allows different software applications to communicate with each other.It defines the methods and rules for how software components should interact, much like a menu in a restaurant.

For example :

When you use the IRCTC website to book a train ticket, you're essentially interacting with the IRCTC server through its API. You enter your journey details, like the origin and destination, and the API sends this information to the server. The server then checks for available trains and sends back a list of options through the API. When you choose a train and proceed to book a ticket, the API sends your booking details to the server, which confirms your booking and sends a confirmation back through the API. Essentially, the API acts as a messenger, facilitating smooth communication between you and the IRCTC server to make your booking experience seamless.

In this simple guide, we'll walk you through fetching data from an API using Python and converting it into a DataFrame, a handy data structure for analysis and manipulation.

Understanding APIs:

APIs (Application Programming Interfaces) are like messengers that let different software systems talk to each other. They allow us to access data or services provided by other applications or websites.

Choosing an API:

Start by finding an API that offers the data you need. Many APIs are freely available and cover a wide range of topics, like weather, finance, or social media.

Reading Documentation:

Every API comes with documentation that explains how to use it. It tells you what data you can get, how to ask for it, and what you'll get back. Understanding the documentation is key to using the API effectively.

Making Requests with Python:

We'll use the requests library in Python to interact with the API. First, install the library if you haven't already (pip install requests). Then, you can make a GET request to the API's URL to fetch data.

import requests

url = 'https://api.example.com/data'
response = requests.get(url)

# Check if the request was successful
if response.status_code == 200:
    data = response.json()  # Convert response to JSON format
else:
    print('Failed to fetch data:', response.status_code)

Converting Data into DataFrame:

Once we have the data, we can convert it into a DataFrame using the pandas library, which is a powerful tool for data analysis in Python.

import pandas as pd

# Convert JSON data to DataFrame
df = pd.DataFrame(data)

Now, df contains your data in a tabular format, making it easy to analyze and work with.

Example:

let's say we fetched TMDb (The Movie Database) data from API.Here's how you can convert it into a DataFrame:

import requests
import pandas as pd

def fetch_movies(api_key, page=1):
    url = f'https://api.themoviedb.org/3/discover/movie?api_key={api_key}&page={page}'
    response = requests.get(url)
    if response.status_code == 200:
        data = response.json()
        return data['results']
    else:
        print('Failed to fetch movie data:', response.status_code)
        return None

Replace 'YOUR_API_KEY' with your actual TMDb API key. You can obtain an API key by signing up on the TMDb website.

The fetch_movies function sends a request to the TMDb API to fetch a list of movies. The page parameter specifies the page number of the results (each page contains up to 20 movies).

TMDB API : https://developers.themoviedb.org/

In addition to the TMDb API, you can explore more free APIs on RapidAPI here .RapidAPI offers a wide range of APIs for various purposes, from weather data to finance information. Check it out to find APIs that suit your project needs and unlock new possibilities!