This is part two of a series based on the workshop I gave at CodeVA’s Full STEAM Ahead (FSA) Conference called “Make It Happen with APIs: The Great Data Journey and the Awesomeness of APIs.” FSA is an annual, local conference in the Greater Richmond Area for girls in middle school and rising 9th graders. FSA seeks to inspire girls through workshops focused on subjects within science, technology, engineering, arts, and math.
Missed part one? Click here to read now.
Awesome APIs
In my previous post, we learned how to query databases that we have direct access to. What if I wanted to get data from a database that I don't have direct access to? What if I wanted to get data from Instagram's databases? Or Facebook's databases?
These companies don't give outsiders access to their databases because that would be very unsafe. They can't let random people do whatever they want to their user data. So instead, they create an Application Programming Interface or API. An API is an application that acts as a messenger between a database and other applications. Using an API, they can clearly define what data they'll allow access to, how it will be queried, and what the data will look like when it gets returned to an end-user. There are many benefits to the end-user as well. The process of using an API to get data is usually language-agnostic because it utilizes the HTTP protocol. This means web developers can use nearly any programming language they want to obtain the data. Also, the data is usually returned in a format easily digested by web applications as either XML or JSON.
Example
Here's an example of a very simple API endpoint that returns some information about a user's Github account: https://api.github.com/users/alitinker. As you can see, it returns data as JSON. A web application could call this endpoint and use the data to display information on a webpage.
How do APIs work?
Let's break it down as simply as possible. When an application makes a request to the API for some data, the API fetches the data from the database and then returns it to those applications. This allows companies like Facebook and Instagram to control what data people can have access to and what they can do with that data.
API tutorial
Let's look at an example of an API. This is a Petstore API created by a company called Swagger. They created this API to show how easy it is to use APIs with an interface that they built. Let's try adding data using this API:.
- Go to: https://petstore.swagger.io/
- Click on the green section under "pet" that says "POST /pet Add a new pet to the store"
- Click on the button to the right that says 'Try it out"
- Now, in the white box, change the word "doggie" to another name... like "Toto" (keep it in quotes)
- Change the word "available" to "adopted"
- Click on the blue button that says "Execute"
- Scroll down and you will see a black box with the response from the API
- Scroll down
- Click on the blue section under "pet" that says GET /pet/{petID}
- Click on the button to the right that says "Try it out"
- Paste the ID that you copied into the form field
- Click the "Execute" button
- Click on the blue button that says "Execute"
- You should see the pet that you added to the store
Now, we'll use an app called Code Sandbox that lets us fetch data from different API endpoints and display the data on a page:
- Go to: https://codesandbox.io/s/confident-sound-2wk0w
- Click on the button in the top right of the page that says "Fork"
- Navigate to the files by clicking on the icon that looks like a document
- Click on the file that says "constants.js"
- Change the part of the TikTok URL after the text 'url=' to: https://www.tiktok.com/@dougthepug/video/6835646401972571397
- Watch the data change!
I hope you enjoyed this basic introduction to data, databases, data querying, and APIs. Please consider sharing it with someone young or inexperienced who may find it intriguing and informative. Let's continue to spark interest in STEAM!