APIs power nearly every piece of software you interact with daily — weather apps, news feeds, payment systems. Python makes it easy to talk to them. This tutorial walks you through building a working API explorer from scratch, even if you have never written a line of Python that touches the internet before.
Building an API explorer means writing Python code that can reach out to a server, ask for data, and make sense of what comes back. You will use the requests library, which is the standard tool for this in Python, and you will work with JSON — the format virtually every public API uses to deliver its responses. By the end of this tutorial you will have a script that fetches live weather data and prints it in a readable way. Every pattern you learn here transfers directly to any other API you will ever work with.
What Is an API and How Does It Work?
An API — Application Programming Interface — is a defined way for one piece of software to ask another for data or to trigger an action. When your weather app shows you tomorrow's forecast, it is not storing that data itself. It is sending a request to a weather server's API and displaying whatever comes back. The server does not share its entire database with you; it exposes specific endpoints — URLs that accept certain inputs and return specific outputs.
The style of API you will work with here is called a REST API (Representational State Transfer). REST APIs communicate over HTTP, the same protocol your browser uses to load web pages. The most common operation is a GET request — you are asking the server to give you some data. The server responds with a status code (a number indicating success or failure) and a body (the actual data, nearly always in JSON format).
JSON stands for JavaScript Object Notation. Despite the name, it has nothing to do with JavaScript in practice. It is a plain-text format that represents data as key-value pairs, arrays, and nested objects — structures that map directly onto Python dictionaries and lists. A small JSON response looks like this:
This tutorial uses only GET requests because they read data without modifying anything on the server. Other HTTP methods — POST, PUT, DELETE — are used when you need to create or change data. For an API explorer focused on reading information, GET is all you need.
Paste any API URL directly into your browser to see the raw JSON response before you write any code. This lets you inspect the structure of the data and figure out which keys you actually want to extract in your script.
{
"city": "Houston",
"temperature": 82,
"unit": "fahrenheit",
"conditions": ["sunny", "breezy"]
}
"Simplicity is a great virtue but it requires hard work to achieve." — Guido van Rossum, creator of Python
Build the correct Python statement to send a GET request and parse the JSON response:
requests.get(url, params=params) — not requests.post(), which is for sending data to a server. The result is stored in response. To parse the JSON body, call response.json() (a method, not a property) and assign it to data. The order matters: you must make the request before you can parse the response.
Setting Up Your Python Environment
Before writing any code, you need one thing installed: the requests library. Python ships with a standard library that handles a lot, but its built-in HTTP tools are verbose. The requests library wraps them in a clean, readable interface that is the practical standard for API work.
Open a terminal and run the following command. If you are using a virtual environment — which is a good habit — activate it first.
pip install requests
To confirm the installation worked, run a quick check in a Python shell or script:
import requests
print(requests.__version__)
If a version number prints without error, you are ready. The table below shows the HTTP status codes you will see as you work with APIs. Knowing what each one means will save you time when something does not work as expected.
- Meaning
- The request was successful and the server returned the expected data.
- What to do
- Call
response.json()to parse the response body and use the data.
- Meaning
- The endpoint URL does not exist on the server, or the resource you requested could not be found.
- What to do
- Double-check the URL and any path parameters. Consult the API's documentation for the correct endpoint format.
- Meaning
- The API requires authentication (such as an API key) and you either did not provide one or it is invalid.
- What to do
- Check that you are passing your API key correctly — usually in a header or as a query parameter, depending on the API.
- Meaning
- You have exceeded the API's rate limit — the number of requests allowed per minute or per day on your account tier.
- What to do
- Slow down your requests. Add a
time.sleep()call between loops, or check the API docs for the rate limit and plan accordingly.
- Meaning
- Something went wrong on the server's side. This is not caused by your code.
- What to do
- Wait and retry. If it persists, check the API provider's status page or support channel.
This script tries to call the Open-Meteo API and print the current temperature. One line contains a bug that will cause it to fail. Click the line you think is wrong, then hit check.
response.text() to response.json(). The text attribute on a Response object is a plain string — calling it like a function with () raises a TypeError because strings are not callable. The correct method to parse a JSON response is response.json(), which returns a Python dictionary you can then index directly.
Reading and Navigating a JSON Response
Once you call response.json(), you have a Python dictionary (or list, depending on the API). The challenge for beginners is usually not making the request — it is figuring out how to pull specific values out of a nested response structure. This section gives you the tools to navigate any JSON response confidently.
Consider the Open-Meteo API response below. The current_weather key holds a nested dictionary. To reach the temperature, you chain two index lookups:
response.json() parses that JSON into a Python dictionary.How to Build a Simple API Explorer in Python
The steps below bring everything together into a complete, working API explorer script. Each step builds on the last. After step three, you will have a script that fetches live weather data for any coordinates you choose and prints it in a readable format.
-
Install the requests library
Run
pip install requestsin your terminal. If you are using a virtual environment, activate it first. Confirm the installation withpython -c "import requests; print(requests.__version__)". Any version number printed confirms it is ready. -
Choose a public API endpoint and define your parameters
Create a new Python file. Set
url = "https://api.open-meteo.com/v1/forecast"and define aparamsdictionary with the latitude and longitude of the location you want weather for. Add"current_weather": Trueto request the current conditions block from the API. -
Send the request, check the status, and print selected fields
Call
response = requests.get(url, params=params), then checkif response.status_code == 200before parsing. If it passes, calldata = response.json()and index into the dictionary to extract the values you want. Print them using an f-string for clean output. Wrap the whole thing in a function so you can call it with any coordinates.
import requests
def get_weather(latitude, longitude):
"""Fetch current weather for the given coordinates."""
url = "https://api.open-meteo.com/v1/forecast"
params = {
"latitude": latitude,
"longitude": longitude,
"current_weather": True
}
response = requests.get(url, params=params)
if response.status_code != 200:
print(f"Request failed with status {response.status_code}")
return None
data = response.json()
current = data.get("current_weather", {})
temperature = current.get("temperature", "N/A")
windspeed = current.get("windspeed", "N/A")
weathercode = current.get("weathercode", "N/A")
print(f"Temperature : {temperature} C")
print(f"Wind speed : {windspeed} km/h")
print(f"Weather code: {weathercode}")
return current
# Houston, TX
get_weather(29.76, -95.37)
This is a complete, working API explorer. Run it in any Python environment and it will print live weather data. To explore a different location, change the latitude and longitude values in the function call. The same pattern — URL, params dictionary, GET request, status check, JSON parse, key extraction — applies to virtually every REST API you will ever work with.
"The requests library was designed to make HTTP as simple as possible." — Python Requests Documentation
Python Learning Summary Points
- REST APIs communicate over HTTP. A GET request asks the server for data and the server responds with a status code and a JSON body. The status code tells you whether the request succeeded before you try to parse anything.
- The
requestslibrary handles GET requests with a single function call —requests.get(url, params=params)— and returns a Response object with a.json()method that parses the body directly into a Python dictionary or list. - Always check
response.status_code == 200before callingresponse.json(). Parsing a failed response can raise errors or return unexpected data. - Use
.get("key", default)instead of direct dictionary indexing when a field might not always be present in the response. This preventsKeyErrorexceptions and keeps your script resilient. - The pattern learned here — URL, params dictionary, GET request, status check, JSON parse, key extraction — transfers directly to any REST API, including authenticated ones where you add headers for an API key.
You now have a working API explorer and a pattern you can apply immediately to any public API. From here, the natural next steps are working with APIs that require authentication (passing an API key in a header), handling paginated responses (APIs that split large datasets across multiple pages), and wrapping your explorer in a simple loop or command-line interface so it accepts user input at runtime.
Frequently Asked Questions
An API (Application Programming Interface) is a set of rules that lets one application communicate with another over the internet. In Python, you typically interact with web APIs by sending HTTP requests using the requests library and reading the data the server sends back, usually in JSON format.
The most widely used library for calling APIs in Python is requests. You install it with pip install requests. It gives you simple methods like requests.get() to send HTTP GET requests and receive server responses.
JSON (JavaScript Object Notation) is a lightweight text format for storing and transferring structured data. APIs use it because it is easy for both humans to read and for programs to parse. Python can convert JSON responses directly into dictionaries and lists using the .json() method on a requests response object.
You send a GET request by calling requests.get(url) where url is the API endpoint string. The function returns a Response object. You can check response.status_code for success (200 means OK) and call response.json() to parse the returned JSON data into a Python dictionary.
A 200 status code means the HTTP request was successful and the server returned the expected response. Other common codes include 404 (resource not found), 401 (unauthorized, usually means a missing API key), and 500 (server error).
No. Many public APIs require no key for basic use, including Open-Meteo (weather data) and JSONPlaceholder (fake test data). These are ideal for beginners who want to practice reading API responses without signing up for credentials.
You pass query parameters by providing a params dictionary as the second argument to requests.get(). For example: requests.get(url, params={"latitude": 29.76, "longitude": -95.37}). The requests library automatically appends them to the URL as a query string.
response.json() is a method on the Response object returned by the requests library. It parses the response body as JSON and returns the result as a Python dictionary or list. It raises a json.JSONDecodeError if the response body is not valid JSON.