How to Use Python Code for Pulling API Data Efficiently

Do you ever feel like you need a superpower to get the information you need? Especially when you're really into Python? APIs are pretty much that superpower! APIs (Application Programming Interfaces) let your code "talk" to other systems and get exactly what you need. They can help you come up with a new app, find the next big market trend, or even automate your morning weather report.

This guide? It's your own, step-by-step guide to using Python to get data from an API, with lots of real-world examples. And if you're feeling brave, we'll even show you how to put that data directly into a PostgreSQL database.

What is an API?

Okay, let’s think about this: you’re dining at your favorite restaurant. You can see the menu at this point. That menu? It’s somewhat like an API. It shows you all of the delicious things you can get (the data and services available). You tell your waiter (that's your application making the request) exactly what you want and - voila! - it’s at your table. There’s no need for you to know how it’s made, what spices the chef uses, you just want it (the data). APIs are used in a very similar way. They are the rules that allow your app to “order” particular services and data from other software or systems. Pretty neat, huh?

what is api?

How Do APIs Actually Work?

The interaction your application has with the API is usually like a conversation - a request and a response. In the following sections we'll address these topics.

  • You Ask: Your application sends a message (that we call a "request") to the API server . This message contains some important items of information:

  • An API endpoint: You can think of this as the address you send the post office to get your data (it's simply a specific URL)

  • An HTTP method: The method tells the API you what you want to do. Do you want to ask for data? Then use GET. Do you want to send new data? That's a POST. Do you want to update something? Then you need to use PUT. Are you deleting something? Use DELETE. I think you understand.

  • Potential extra items: This is maybe a search term or possibly an API key to confirm you belong there.

  • They think: The API server gets your request, determines what you are asking to do, and scuttles off to find the data or perform the action you want it to do.

  • They Reply: Take a look, eventually the API server sends a message (a "response") back to your application. This usually contains:

    • The requested output (which is very often conveniently included in a format like JSON or XML).

    • An HTTP status code - this will let you know if things went smoothly or if things went awry.

Why Python and APIs are a perfect pair

Python and APIs are a perfect pair

Python is the best programming language to work with APIs – and it’s easy to see why! Python is easy to learn and use, has a plethora of libraries built for you, and an enormous, supportive community behind it. When you want to work with APIs in your Python programs, look for opportunities to:

  • Get Outside Data: Maybe grab some fresh weather, public information, or current stock prices? APIs are perfect for getting fresh data from sources outside your program.

  • Automation of Boring Stuff: Tired of doing your project over and over again? Use APIs to automate your 'boring' code to talk to web services, like posting on social media, emailing, or managing your cloud account.

  • Link Your Applications: Using Python to connect with other software applications, databases (like PostgreSQL!), or web applications.

  • Build Cool Apps: Your application pulls fresh Data constantly from an online service.

  • Maturity: Add something new like integrating for new features or data sources into an existing Python project.

API's are a critical aspect of the technology powering many parts of the internet. Here are some examples of important tools and services that use APIs:

  • Social Media Tools: Facebook, Twitter, Instagram, and LinkedIn all provide APIs, which are made available to developers so that they can integrate social media with their app functionality. Developers can use the APIs for automated posting, ISO data, or interacting with users.

  • Payment Gateways: API-enabled services, like PayPal, Stripe, and Square let e-commerce websites, and shoppers, do business in a secure way by completing the transaction via an API.

  • Maps: API is also part of Google Maps and OpenStreetMap so developers can embed the map, get search results, and get directions in their applications.

  • Cloud Services: AWS, Google Cloud, Microsoft Azure, and other cloud services provide API's so developers can manage virtual machines, cloud storage, databases, etc.

  • Data Aggregators: Some services (e.g., weather data, stock price data, news) aggregate plenty of data sources by calling a ton of API's.

When and Why Use API with Python

Because Python is clean, has rich libraries and lots of community support, it is a wonderful language to work with APIs. In your Python programs, you may want to consider using APIs if you need:

Python REST API

  • More Data than You Can Shake a Stick At : You get to leverage a huge, massive data lake that is bigger than just what's already on your computer.

  • Real-time Greatness: You are going to build applications that are consuming live data, and building things like dashboards or alert mechanisms.

  • Speed of Development: Why build everything when web services already have that functionality? Use their APIs and you can use their pre-built functions to save tons of time.

  • Clean Architecture: They allow you to keep your application loosely-coupled from specific data sources and this can make your code manageable and flexible.

  • Moving at the Speed of Business: You easily allow your app to engage with other pieces of software, even if they were developed by other people.

The Benefits of Using APIs in Python Projects

APIs improve Python Projects by:

  • Increasing Data Availability: It provides additional data and information resources that your application can access beyond the local data your application has access to.

  • Creating Real-time Functionality: It means you'll be able to develop applications that will respond to live data updates, such as: live dashboards and alerts.

  • Reducing Time to Develop: Utilizing APIs will allow you to build upon the features available through the web services and most likely not have to develop them on your end.

  • Increasing Modularity: It allows you to design your application to not to depend on the specific data sources, promoting flexibility and progress in data integration.

  • It encourages Collaboration: Apis enable other applications to communicate with yours to allow your applications to work with independently developed software components.

Configuring Your Python Environment for APIs

Before making your first API request, make sure your Python environment is configured. The most commonly used library in Python for making HTTP requests is requests. If it's not already installed, you can install it from your terminal or command line via pip:

pip install requests

And for handling JSON data (which you'll see a lot of!), Python's json module is already built-in, so you don't need to install anything extra for that. Easy peasy!

Making Your First API Request in Python

Okay, now we will put our money where our mouth is! We are going to make a very simple GET request to a public API. We will be using the JSONPlaceholder API, which is a free, public and fake API that is great for testing.

import requests
import json

# Define the API endpoint
api_url = "https://jsonplaceholder.typicode.com/posts/1"

# Make a GET request
response = requests.get(api_url)

# Check if the request was successful (status code 200)
if response.status_code == 200:
    # Parse the JSON response
    data = response.json()
    print("API Data:")
    print(json.dumps(data, indent=4)) # Pretty print the JSON
else:
    print(f"Error: Unable to fetch data. Status code: {response.status_code}")

See? Not so scary, right?

Quick Chat About API Status Codes (What Do Those Numbers Mean?)

The little numbers you see in an API response? They can be really important! They specify exactly what happened with your request. You can think of them as normalized messages from the server. Here are the numbers you're likely to see the most:

API response codes

  • 200 OK: "Sure, I can do that! Everything is alright". This is the response you want to see - your request was successful.

  • 201 Created: "Success! I even created something new for you." You will generally see this response when you send data to create something.

  • 204 No Content: "Success! But there was nothing to send back." The server did what it was supposed to do, but did not have any data to send back to you.

  • 400 Bad Request: "Uh oh, there is something wrong with your request." This could be something you typed, or a message that has invalid data.

  • 401 Unauthorized: "Hold on! You need to authenticate to verify who you are." You attempted to access something without proper credentials.

  • 403 Forbidden: "Sorry, that is not allowed." The server knows who you are, but does not allow you permission.

  • 404 Not Found: "I can't find it!" Whatever you requested simply is not present.

  • 500 Internal Server Error: "Oops, I messed up!" The server encountered an unexpected condition.

Connecting to an API

Connecting to an API is simply creating the correct web address (the "endpoint" we discussed), and then we have our trusty requests library. This library handles the http request (GET or POST).

import requests

# For demonstration, let's use the Open-Meteo Weather API
base_url = "https://api.open-meteo.com/v1/forecast"
params = {
    "latitude": 52.52,
    "longitude": 13.41,
    "current_weather": True
}

try:
    response = requests.get(base_url, params=params)
    response.raise_for_status()  # Raise an exception for HTTP errors (4xx or 5xx)
    print("Successfully connected to the API!")
except requests.exceptions.RequestException as e:
    print(f"Error connecting to API: {e}")

2. Retrieve the information from the API

This is all connected now it's time to get the actual data from the response object we received.

# Continuing from the previous example
# response object is available here
if response.status_code == 200:
    raw_data = response.text  # Get the raw response content as text
    print("\nRaw API Data:")
    print(raw_data[:200]) # Print first 200 characters for brevity
else:
    print("Failed to get data from API.")

3. Convert the data to a JSON format

Generally speaking, most APIs will respond to you with data formatted in something called JSON (JavaScript Object Notation). JSON is very popular because it is compact and easy to read. Luckily for you, the requests library will make it easy to parse!

import json # Ensure json is imported

# Continuing from the previous example
if response.status_code == 200:
    json_data = response.json() # Parse the JSON content directly
    print("\nParsed JSON Data:")
    print(json.dumps(json_data, indent=4)) # Pretty print for readability
else:
    print("Failed to parse data into JSON.")

4. Extract the data and print it

Now that you have your data in a JSON format, it**'s** just like dictionaries and lists in python, and you can navigate into it to find just the pieces of data you want.

# Continuing from the previous example
if response.status_code == 200:
    json_data = response.json()

    # Extracting specific data (example for Open-Meteo)
    if 'current_weather' in json_data:
        current_weather = json_data['current_weather']
        print("\nExtracted Current Weather Data:")
        print(f"Temperature: {current_weather['temperature']} {json_data['current_weather_units']['temperature']}")
        print(f"Wind Speed: {current_weather['windspeed']} {json_data['current_weather_units']['windspeed']}")
        print(f"Weather Code: {current_weather['weathercode']}")
    else:
        print("Current weather data not found in the response.")

API Data Extraction: Getting the Data Just Right

"API data extraction" sounds all fancy, but it really just means retrieving data from an API with your code, cleaning it, and organizing it into the best format for your needs. This usually includes:

  • Authentication: Sometimes you have to show an "id" (i.e., your API key or token) to get to the good stuff.

  • Pagination: If an API has lots of data, it might send the data to you in separate chunks, like pages of a book. You'll want to know how to ask for the next "page."

  • Error Handling: Things can go wrong! You'll want your code to politely handle all those status codes while managing any network bumps.

  • Data Transformation: The raw output may not be exactly how you would want it. This is where you'll transform the API data into the desired format for your app or database.

Going further: saving your API data in PostgreSQL

So you have acquired data from an API. What do you do now? One really common thing would be to save the data somewhere like a PostgreSQL database. This isn't really "pulling API data" but, would be the perfect next step toward making the data meaningful. Enter, Python's psycopg2 The library will be great use here.

Saving your API data in PostgreSQL

Context: Let's pull some weather data from an API and then save it to our PostgreSQL database.

Install the psycopg2-binary Package

Before diving in, install the PostgreSQL adapter:

pip install psycopg2-binary

Then, here's some basic code to get that data inserted:

import psycopg2
import requests
import json

# --- API Data Pull (as shown before) ---
api_url = "https://api.open-meteo.com/v1/forecast"
params = {
    "latitude": 52.52,
    "longitude": 13.41,
    "current_weather": True
}
response = requests.get(api_url, params=params)
response.raise_for_status()
weather_data = response.json()

# --- PostgreSQL Connection and Insertion ---
db_config = {
    "host": "localhost",
    "database": "your_database_name",
    "user": "your_username",
    "password": "your_password"
}

try:
    conn = psycopg2.connect(**db_config)
    cur = conn.cursor()

    # Create table if it doesn't exist (run once)
    cur.execute("""
        CREATE TABLE IF NOT EXISTS weather_logs (
            id SERIAL PRIMARY KEY,
            temperature DECIMAL,
            windspeed DECIMAL,
            weather_code INTEGER,
            timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP
        );
    """)
    conn.commit()

    if 'current_weather' in weather_data:
        temp = weather_data['current_weather']['temperature']
        wind = weather_data['current_weather']['windspeed']
        w_code = weather_data['current_weather']['weathercode']

        cur.execute(
            "INSERT INTO weather_logs (temperature, windspeed, weather_code) VALUES (%s, %s, %s)",
            (temp, wind, w_code)
        )
        conn.commit()
        print("Weather data successfully inserted into PostgreSQL.")
    else:
        print("No current weather data to insert.")

except psycopg2.Error as e:
    print(f"Error connecting to or interacting with PostgreSQL: {e}")
finally:
    if conn:
        cur.close()
        conn.close()
        print("PostgreSQL connection closed.")

Important: Remember to swap out your_database_name, your_username, and your_password With your actual PostgreSQL details!

What You Can Do Next

With the Observations data now in your database, you have some awesome next steps:

Set Up Data Extraction

You could add some automation to log your weather data by scheduling the execution of the python application to run using a cron (Linux/macOS) or Task Scheduler (Windows).

Visualize the Data

You can connect your PostgreSQL database to a data visualization application like Tableau or Power BI and create reports, or you can use Python libraries like matplotlib or seaborn to visualize it.

Add Data Validation

You may want to add data validation by performing checks for outliers or missing values before you insert the data into your table. There are some checks that you can add to your data validation, which should improve the data quality.

Index Your Table

If you are logging a lot of observations a day, you may improve the turnaround time of your SELECT queries by indexing your log table specifically on the timestamp field.

Why You Should Validate Your APIs

validate API

APIs are a key part of many modern-day applications. Whether you're consuming an API from someone else or exposing your own, validation testing is a vital part to ensure the reliability, performance and correctness of your APIs. Here are some of the reasons why testing your APIs is important:

  • Catch bugs early — So you don’t ship broken endpoints.

  • Validate performance — To ensure that your APIs perform under load.

  • Verify against regressions — To ensure that updates and changes to APIs don’t somehow break existing behaviour.

  • Validate security — To ensure that unauthorized users can’t access sensitive data.

Why Is It Important to Test Your APIs?

APIs are an essential part of modern applications. Whether you are building an internal service or exposing an endpoint to external consumers, app testing makes sure that:

  • The API is working properly.

  • Input validation is enforced.

  • Authentication and authorization are working.

  • Return data formats and response payloads are consistent.

  • Changes don't result in regressions.

Without testing, a bug could break working integrations or open the door for a security issue.

How to Test Your APIs

Manual API Testing

Manual API Testing When using manual automation tools such as Postman, cURL or HTTPie, a tester is working through the API end points as user of the application, submitting requests and viewing the response.

When do you manual test?

  • During original development or prototyping

  • To validate edges and error handling

  • For Exploratory Testing

Negatives: Does not scale for large applications

Repetitive testings take time to execute

It does not fit into our Continuous Integration process for the application

Manual Testing

What Is Automated API Testing?

Automated testing is just writing (or generating) scripts that automate the testing of your API by sending repeated requests to test the functionality of the new feature. Automated tests are run consistently and can be integrated into CI/CD pipelines.

The Importance of Automated Testing

Reduces manual labor and human error Ensures new changes do not break existing features Provides assurance that you are continuously delivering Provides fast feedback to developers Will scale along with your application!

How it works?

Test Cases: the definition of an API request and what the expected response is Tests run via scripts or test runners If a test fails it will typically provide a failed response immediately, so this prevents a bad deployment Tests are typically version controlled and run automatically during builds You can either write these tests by hand while using existing libraries such as pytest or you could use existing tools to perform automated testing with little configuration.

Keploy: Automated API Testing Without Writing Tests

Keploy streamlines and automates API testing by capturing real traffic and creating test cases on-demand. Keploy captures API requests and responses during the normal use of an application. It later replays these requests and responses as tests.

Keploy API Testing

How Keploy can Help?

Records real user behavior as test cases Provides mocks of the dependencies (databases, external apis) Can replay captured tests in either a staging or CI environment No coding or writing test scripts required Works with several frameworks and programming languages

Best Use Cases

Teams that want to automate tests out of the box with little to no configuration APIs that change so often that writing tests out would take too long Applications with many integrations or ongoing changes in the real flows of users

  1. How to Automate Test Case Generation for Faster API Testing - https://keploy.io/blog/community/test-case-generation-for-faster-api-testing

  2. API integration – Importance and Best Practices - https://keploy.io/blog/community/api-integration-importance-and-best-practices

Conclusion

So, there you have it! APIs are tremendously significant in the software world today. They are your door of entry to be able to access a whole new world of data and services, and honestly, Python has the best ecosystem for retrieving data from APIs with an abundance of utility libraries at your disposal, like the requests library and many great data tools. The basics of learning APIs and how to send requests, parse responses, and parse that data into dataframes will allow you to unveil amazing potential for your Python projects! You can go from simply pulling some data to building rich applications that store data in databases like PostgreSQL. So get back in there and leverage APIs! Last, create web applications that will be dynamic, data driven, and connected! You got this!

FAQs

  1. What data format do most APIs use?

    JSON stands for JavaScript Object Notation. People find it rather easy to read and it is smoothly parsed by machines. XML is still relevant in today's world, but JSON is king these days.

  2. Do I need an API key every time I want to have access?

    Not necessarily! A lot of public APIs, especially open data APIs, do not ask for a key. But the API may ask for a key if the data you are working with is a user's data, if it needs permission of some type, or if the service has a cost (fee) associated with usage.

  3. What is the difference with a GET vs a POST request?

    You might look at it like this; with a GET request you are just requesting info, you are requesting data from a server and typically passing any additional info right in the URL. A POST request is for data sent to the server for the server to create or update something, and that data comes from the body of your request and not the URL.

  4. How do I handle large data from an API?

    Many API's use a paging concept. This means the data returned can be in smaller chunks, and you will need to make many API requests, usually just by changing some parameters, for example, page limit in your call.

  5. What are rate limits of API's? And how do I handle them?

    APIs may limit you on the number of requests you can make (e.g. 100 requests per minute). It you exceed the limit and make an API call you will not receive anything (the API service thinks your are spam). To handle it, you can add delays (sleep time, e.g. time.sleep() in Python) between your requests or you can use libraries that deal with rate limits for you.

  6. Can I use Python for APIs that require an authentication process?

    Yes, many third-party libraries provide simple access to an API using the requests library which supports typical forms of authentication (API keys (modern API calls send these via headers or query parameters), basic authentication (username/password) and OAuth). Like all API calls, check the API documentation for any requirements of the authentication method.

0
Subscribe to my newsletter

Read articles from Mohamed Abdullah F directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Mohamed Abdullah F
Mohamed Abdullah F

GSoC 2025 @Alaska