APIs and Postman Deliver!
Prior to attending the Flatiron School, I worked for a sports pick ’em app called Daily Bracket. If you want to learn more about the app, feel free to check out “How to Play” on https://www.dailybracket.com/. I was in charge of day-to-day operations for the app and had to insert the sporting events each day as well as some of the players in the game. Daily Bracket provided live stats on each of the sporting events we displayed on the app and we did so by interacting the with Sportsradar API and they used Postman to do this. There were plenty of terms I heard where I had no idea what they were talking about and “API” and “Postman” were two of those terms. Now I am still far from an expert, but I thought I would shed some light on what both of these scary words are for those new to programming.
So let’s get started with what an API is. The basic definition for an API (or application program interface) is code that allows two software programs to communicate with each other (http://searchmicroservices.techtarget.com/definition/application-program-interface-API).
However, there are multiple types of APIs and the ones we deal with in this class are usually web APIs. A web API is normally defined as a set of HTTP request messages and response messages and it usually comes back in XML or JSON format (for more on those formats). This may all seem confusing so I will show you an example of a lab we did in class. One of the often-used public APIs is the Github API. Say someone wanted to see all of the repositories that I have stored on Github. They could type in https://api.github.com/users/dkush22/repos and they would see a page that looks roughly like this:
Mine may look a bit different because I downloaded the JSON Viewer Chrome Extension. On this page you can clearly see different data related to my repositories. You can decide to pull any of this data and use it in your own application using a get request. In this one particular lab, we searched by username and interacted with the Github API via a “GET” request to get all of the repositories of that particular user. The end result looked something like this:
If you can see in the search box, I searched for my Github username and the app provided me with all of my repositories. I could then also get all of the commits and branches via another “GET” request.
Another public API is the Marvel API and I used it to create a mini project where I could search Marvel characters by each letter of alphabet and data would be provided for each character when I scrolled over their names. Here is a snippet of the Marvel API for one character:
I used this information to seed my Marvel character database and the end result was this:
Now the Marvel API is a little harder to interact with than the Github API because it requires you to create an account to get a unique key and then you must pass that key in as a parameter. I will get into this a little bit later, but the way I manipulated the Marvel API was through an app called Postman!
Postman is a Google Chrome app for interacting with HTTP APIs. It provides you with easily viewable responses, usually in JSON format. In fact, the Marvel API example from above was taken directly from Postman. The simplest action in Postman is the “GET” request and this provides you with the JSON file we saw above and this is pretty similar to just putting this URI in the address bar. However, Postman has the ability to send many more requests. They have the basic CRUD requests of GET, PUT, POST, and DELETE, as well as a few more.
I found Postman particularly helpful for the Marvel API because of the way it helped me edit the correct URI. If you just put http://gateway.marvel.com/v1/public/characters into Postman (or your address bar) you will see the following message:
After further research from the Marvel API website, I found out that the URI must have a valid API key, a timestamp, and a hash for the data to show. These must be passed in as parameters, which are essentially just additions to your URI. For example: http://gateway.marvel.com/v1/public/characters?apikey=1e29284190f7081c5n9d834d8f6ae65&ts=1502840200286&hash=e029482ds065957f4b60b71ca89b8835f& (I didn’t use my real apikey).
In that URI you can see that after “/characters” we start to pass in our parameters so that our data is displayed. Now that would be hard to pass in on your own, but it’s as easy as this in Postman:
You just put in the parameters and it changes the URI for you!