As somebody working in data, you’ll encounter times where you need to work with a system through its API. With APIs, you can do things like pulling data from the system, editing the data in the system, etc. It’s more common for analysts to pull data than making changes to the system through its API. Data engineers or software engineers may do more complex stuff with APIs. In short, it’s good to know how to work with an API. In this post, I’ll provide a simple Python script to pull data from.
What is an API?
API stands for “application programming interface”. I don’t like throwing all the jargons to make things complicated, so here’s a sentence that describes what it is.
It’s an interface through which two systems can communicate each other.
Essentially, systems usually have their APIs, and for data people like us, what we need to know is how to work with APIs. We may not build an API for a system, but there are times where we need to ingest data from a system or make changes to the data that’s in the system through its API. It’s an advantage if you know how to work with APIs!
What are API endpoints?
One important concept to understand is what API endpoints are and what they are for. They are basically the specific entry points that the developer who built the API specified, by which we can execute certain operations on the system. There should be a detailed documentation on the endpoints if a system has an API. We better read it through to make sure we know how to properly work with the API.
Pull Data from an API
Here’s a Python script to pull data through star-wars API which is publicly available here in this website. For working with APIs, Python has an awesome package that’s widely used called “requests”. It’s what I’m using in this example.
import requests
url = 'https://swapi.dev/api/people/1'
response = requests.get(url)
response_json = response.json()
name = response_json['name']
height = response_json['height']
gender = response_json['gender']
print(response_json)
print()
print(name)
print(height)
print(gender)
You use what’s called “GET” request because what we’re interested in is to pull data. You use “GET” operation when you want to pull data through an API. There is other operations like “POST”, “PUT”, etc, but that’s for another time.
And here’s the output that I got from the script:
Most APIs return the response in json format. And to extract specific values, you use braces in Python.
With this public API, the process of pulling data is very straight forward, but you should note that there would usually be some id and secret you may have to pass through when you do “GET” request. But for the simplicity sake, I won’t demonstrate that part in this post (you essentially a few other parameters in “.get()” in the script).
Conclusion
That’s all I had for this post. Hope this helps you get started in working with APIs! Link to the code in Github