Why Using APIs is Like Playing with LEGO
APIs are a way to use other people’s code and data as building blocks for your own code.
You may have heard the term “API” in most coding tutorials that you’ve read. If you Google “API,” you’ll get a big paragraph about how it stands for “Application Programming Interface” and uses “REST” and so on, as if that makes it any clearer.
All you need to know about APIs is that they are a bunch of functions you can use to automate tasks and get data. For example, you can:
- Use the Gmail API to read and filter your email with code
- Use the Reddit API to create a bot that automatically converts metric units to imperial with code
- Use the Fitbit API to grab data from your Fitbit and analyze it
- Use the USGS API to see flooding around the United States in real-time
APIs are like a set of pre-made LEGO bricks. If you wanted to build a miniature house, LEGO makes plastic pieces that intuitively fit together and allow you to build your house using their modular pieces.
In the same vein, APIs provide little code pieces that allow you to build programs. They have functions like:
getLikeCount()
createGoogleSheet()
sendEmail()
If you used the Gmail API to build a script to read your email, you could use the “login” piece of the API to log in to your account, then the “get all emails” piece to read all the emails, then the “delete emails” piece to get rid of ones you don’t want.
How do we know what these “code pieces” are?
Every API comes with documentation that lists all the ways you can use it, kind of like a LEGO catalog. It shows you all the things you can do with an API, which for the Reddit API might be “post a comment” or “save a post.” Documentation is often found online on a company’s website, usually on some hidden developer page. The “things you can do” are referred to as “API methods.”
Some documentation is better than others, but documentation is ultimately a list of pieces of pre-made code and functions that you can use in your own code.
The Easiest API: How to Get Your IP Address
Let’s look at an API that only has one piece: the IPify API, which tells you what your IP address is.
An IP address is like a phone number or mailing address for your computer. It tells the internet where to find you. Websites have their own IP addresses as well.
We’re going to send one command to IPify.org and ask it what our IP address is. This works on both Mac OS and Windows.
Open either Terminal (Mac) or Command Prompt (Windows) and type the following:
curl 'https://api.ipify.org?format=json'
Press Enter. You will get back your IP address:
{"ip": "68.142.10.250"}
IPify.org sent back what they saw your IP address as in a format called JSON.
To verify that this is your real IP, visit WhatIsMyIPAddress.com.
What is JSON?
JSON is one of the most popular data formats for modern web APIs. It puts everything in {“key”: value}
format so that all of the data has a label. Another popular data format is XML.
For example, when we ask the Open Notify API about the location of the International Space Station, the JSON looks like this:
{
"timestamp": 1681525645,
"message": "success",
"iss_position": {
"latitude": "7.6707",
"longitude": "-144.8210"
}
}
This is the gist of a lot of web APIs. We ask a website for some data and it responds back with that data. APIs used commonly in scientific research, like the NASA, USGS, and USFDA APIs, work this way. We can get data about space satellites, the elevation of mountains in the US, and the list of FDA-approved drugs if we just ask for it.
What About Automating User Activity?
What if we don’t want to just grab data from a website? What about making bots or automating our work day?
This is where more complicated APIs come in. They still involve singular commands where we talk to a website or app and say, “I want to get this data (or send this data).” And the app responds accordingly. But now we’re putting multiple commands together to accomplish more sophisticated activities. These will be in upcoming tutorials. Stay tuned.
Examples to Get Started
We have a few short tutorials to get familiar with using APIs from Python, R, and Bash code. Some require no code at all.
- How to Find Where the International Space Station is Right Now with Code
- How to Get Real-Time Flood Data from the USGS API
- How to Get Every FDA-Approved Drug with One Line of Code
- How to Get Polar Ice Data from NASA’s ICESat-2 Satellite
Or you can check out these APIs around the Internet that you can use to automate your social media, check the weather, and more.
Questions and Feedback
If you have questions or comments, email us at protobioengineering@gmail.com or message us on Instagram (@protobioengineering).
If you liked this article, consider supporting us by donating a coffee.