How to Fetch ISS Data with Python: A Beginner’s Guide to APIs
This tutorial explains what an API is, shows how to install and use the Python requests library to make GET requests, interprets common HTTP status codes, and demonstrates parsing JSON data from the Open Notify ISS API using the built‑in json module.
An API (Application Programming Interface) is a server-provided code endpoint that allows you to send or receive data, typically used for data retrieval.
To make API requests in Python, install the requests library using pip install requests or, if you use conda, conda install requests. Then import it with import requests.
Create a GET request with
response = requests.get("http://api.open-notify.org/this-api-response-exist/")and check the status code via print(response.status_code). A 404 status indicates the requested resource does not exist.
Common HTTP status codes include:
200 – Success
301 – Redirect
400 – Bad request
401 – Unauthorized
403 – Forbidden
404 – Not found
503 – Service unavailable
Always refer to the API documentation before making requests.
We will use the Open Notify API ( http://open-notify.org/ ) to retrieve data about the International Space Station. The endpoint http://api.open-notify.org/astros.json returns JSON data about astronauts currently in space.
Make the request:
response = requests.get("http://api.open-notify.org/astros.json")
print(response.status_code)
print(response.json())The response status 200 confirms success, and the JSON payload can be accessed with response.json().
JSON is the standard data format for APIs. In Python, the built‑in json module handles JSON serialization and deserialization. Use json.dumps() to convert a Python object to a JSON‑formatted string, and json.loads() to parse a JSON string back into a Python object.
Example helper to pretty‑print JSON:
import json
def jprint(obj):
text = json.dumps(obj, sort_keys=True, indent=4)
print(text)
jprint(response.json())This prints the formatted JSON showing six astronauts aboard the ISS, each represented as a dictionary within a list.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
21CTO
21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
