Why Using APIs is Like Playing with LEGO

Proto Bioengineering
5 min readMar 6, 2023

--

APIs are a way to use other people’s code and data as building blocks for your own code.

Photo by Xavi Cabrera on Unsplash

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:

How we might use an API for a forum like Reddit. Original photo by Xavi Cabrera on Unsplash

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.

LEGO’s Pick a Brick site and catalog.

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.”

The Slack API has slick documentation.

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'
What the command looks like in Terminal on a MacBook.

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.

If this car had an API, it would have a “tire” attribute and “drive()” method. Photo by Eric & Niklas on Unsplash

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.

Or you can check out these APIs around the Internet that you can use to automate your social media, check the weather, and more.

This kid knows how to read the API documentation. Photo by Kelly Sikkema on Unsplash

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.

Related Links

Photo by Phil Hearing on Unsplash

--

--

Proto Bioengineering

Learn to code for science. “Everything simple is false. Everything complex is unusable.” — Paul Valery