A Beginner’s Guide to Using APIs in Data Science

Your first data science project probably involved a perfectly clean CSV file. You loaded it into a notebook, ran .describe(), and felt like a pro. In the real world, data isn’t a static file you download. It’s alive. It’s streaming from Twitter, updating on stock market servers, and being generated by users every millisecond. That is why you need to learn how to work with APIs, especially for data collection. In this article, I’ll take you through a beginner’s guide to using APIs in Data Science.

What’s the Big Deal with APIs, Anyway?

An API, or Application Programming Interface, is just a fancy term for a waiter.

Let’s say you’re at a restaurant. You (the client) want a steak. You don’t just walk into the kitchen (the server) and start cooking. That would be chaos. Instead, you’re given a menu (the API documentation) that lists what you’re allowed to order.

You give your order, “I’ll have the 12-ounce ribeye”, to the waiter. It is an API request.

The waiter takes your order to the kitchen, which prepares your food. The waiter then brings your steak back to you. It is the API response.

That’s it. An API is just a stable, reliable menu that lets different software systems talk to each other without exposing the entire messy kitchen.

For us in data science, this is everything. It means we can order data from Twitter, Google Maps, a weather service, or our own company’s database, and get a clean, predictable plate of data back.

A Practical Example on Using APIs for Data Science Tasks

Enough talk. Let’s fetch some real data in Python. This is the exact workflow I use all the time. I will use a brilliant Python library called requests. It makes ordering data from any platform incredibly simple.

First, you’ll need to install it. Open your terminal or command prompt and type: pip install requests.

Our Mission: Fetching Fake Blog Posts

We’ll use a wonderful free service called JSONPlaceholder. It’s a fake API built specifically for testing and teaching. It requires no API key or setup.

Our endpoint will be: https://jsonplaceholder.typicode.com/posts. Endpoint is the URL you send your request to. It’s like the specific item on the menu. A server might have /users to get user data, /posts to get blog posts, or /weather to get a forecast.

Our endpoint in this example will give us 100 fake blog posts. Let’s go.

Step 1: Make the Request

In a Python script or Jupyter Notebook, we’ll import the library and make our GET request:

import requests
import pandas as pd

# This is the "menu item" we want to order
url = "https://jsonplaceholder.typicode.com/posts"

# We send our "waiter" (requests) to "get" the data from that URL
response = requests.get(url)

print(response)
<Response [200]>

Step 2: Check the Waiter (Status Code)

The response object we got back has a status_code. This is the first thing you should always check. It’s the waiter telling you if the order went through. Here:

  • 200: means “OK.” This is what you want!
  • 404: means “Not Found.” You asked for a menu item that doesn’t exist.
  • 403 / 401: means “Forbidden / Unauthorized.” You don’t have permission. (This is where you’d usually need an API Key, which we’ll cover in a bit.)

Since we got 200, we’re good to go!

Step 3: Get the Data (as JSON)

The requests library has a built-in .json() method that automatically converts the server’s response into a Python dictionary or list:

# Check if the request was successful
if response.status_code == 200:
    # Convert the response data into a Python list of dictionaries
    data = response.json()
    
    # Let's peek at the first item
    print(data[0])
else:
    print(f"Failed to get data. Status code: {response.status_code}")
{'userId': 1, 'id': 1, 'title': 'sunt aut facere repellat provident occaecati excepturi optio reprehenderit', 'body': 'quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto'}

Look at that! It’s just a list of dictionaries. We can work with this.

Step 4: The Pandas DataFrame

That list of dictionaries can be loaded directly into a Pandas DataFrame in one line of code:

df = pd.DataFrame(data)

# Let's see what we've got!
print(df.head())
   userId  id                                              title  \
0 1 1 sunt aut facere repellat provident occaecati e...
1 1 2 qui est esse
2 1 3 ea molestias quasi exercitationem repellat qui...
3 1 4 eum et est occaecati
4 1 5 nesciunt quas odio

body
0 quia et suscipit\nsuscipit recusandae consequu...
1 est rerum tempore vitae\nsequi sint nihil repr...
2 et iusto sed quo iure\nvoluptatem occaecati om...
3 ullam et saepe reiciendis voluptatem adipisci\...
4 repudiandae veniam quaerat sunt sed\nalias aut...

And just like that, you’ve gone from a live web server to a clean, analysis-ready Pandas DataFrame. You can now run:

  1. df.info()
  2. df.groupby(‘userId’).count()
  3. Or feed this data into a machine learning model.

You just did data science on live data.

Most real APIs (like Twitter or OpenWeatherMap) require an API Key. This is just a long, unique string (“ak72…z9p”) that they give you to prove who you are. You can learn how to work with such APIs with these example projects:

  1. Music Recommendation System Using Spotify API
  2. YouTube Video Chaptering (YouTube API + NLP)
  3. Building a Multi-Agent System using Gemini API

Final Words

This skill is the doorway to modern AI. Today, you’re fetching blog posts. Tomorrow, you’re pulling live financial data to train a forecasting model. Next week, you’re sending a prompt to an LLM API and getting a response back for your Generative AI app. I hope you liked this article on a beginner’s guide to using APIs in Data Science. Feel free to ask valuable questions in the comments section below. You can follow me on Instagram for many more resources.

Aman Kharwal
Aman Kharwal

AI/ML Engineer | Published Author. My aim is to decode data science for the real world in the most simple words.

Articles: 2022

2 Comments

  1. Hello
    I wish you are good
    I can’t use you web site . I don’t find all subject that you put it in your Instagram story . Can you help me to find all your articles or codes ??

    Thank you for your answer

Leave a Reply

Discover more from AmanXai by Aman Kharwal

Subscribe now to keep reading and get access to the full archive.

Continue reading