API Tutorial and Introduction to RESTful Development

0
2185

I hope that you are having a good start to the week, on Mondays I like to cover a computer science or development topic and give a dead simple explanation of how it works and today I’m going to give a basic API tutorial.

API stands for Application Programming Interface and if you aren’t familiar with how APIs work, they’re essentially tools that let outside applications communicate with whatever application they are connected to. There are a number of API tools that are used in the development community, however the most popular is the REST based API, and that’s what I’ll be discussing today.

Let’s start with a real world case study, imagine that you got hired to build a real estate web application and you’re asked to dynamically show all of the Tweets posted in the area. You could try create a web scraper that pulls in all of the relevant posts from various Twitter pages, but that would be a very messy way of developing the feature. Instead you could connect to the Twitter API and pull in all of the relevant Tweets for that area.

So how would you connect to the API? If you’ve never done this type of development before it can seem a little mysterious, but it’s actually quite straightforward. In your application you would send a request to a URL that Twitter created and pass in the location parameters. An example would look something like this:

http://twitter.com/search?q=place%3A247f43d441defc03

To break this down a bit, the URL http://twitter.com/search is called the API’s URI, which stands for Uniform Resource Identifier, which is a fancy way of saying that it’s the API’s address. The ?q=place%3a is the API endpoint that lets Twitter know that we want to look for a Place, as opposed to a specific Tweet or say a User’s friend list. Lastly the 247f43d441defc03 is the Place ID. After our application sends a request to this Twitter API link Twitter will first check to see if the application is allowed to communicate with it, then it will take the parameters from the request and query its database. Lastly it will send back an API response that contains the data we asked for.

api flow

Typically APIs return either JSON or XML data, for example, if we requested a list of Twitter Mentions, the returned response would look something like this:

json api

APIs return standardized formats such as JSON and XML so that developers can easily parse through the returned data and use it however our application requires. When your application gets the data back from the API you can iterate over it and create database records with it or you can simply render it to users if the information is temporary in nature, such as with Tweets.

Other examples of applications that you could leverage APIs for would be:

  • Mapping applications with Google Maps
  • Rendering local business reviews from Yelp
  • Showing relevant videos on your website from YouTube
  • And the list goes on

In fact, APIs have become so prevalent that there are many times where I’m able to skip entire features of applications and simply integrate outside APIs to provide the functionality. A great example is one that we’re working on with devCamp right now where we needed to integrate a scheduling component into the application so that students could easily schedule meetings with instructors. That type of feature would have taken quite a bit of time to build into the app from scratch, especially when you consider all of the potential edge cases. However by leveraging an outside scheduling API the feature was fully built within a few days.

Another reason I created this API tutorial is because in addition to working with outside applications, it’s also very important to have a strong working knowledge of APIs since there are many times where you will need to build APIs for your own applications to communicate with each other. For example, service oriented architecture, also known as the microservice design pattern, focuses on building one application per feature, and having all of the apps connected via APIs. So whether you need to connect to an outside service, or if you’re looking to work with modern development practices, working with APIs is very important.

I hope that this API tutorial has been a helpful introduction to what APIs are, how they work, and why they’re important to understand. In future episodes I’ll be continuing this discussion and I’ll cover other topics related to APIs, including JSON parsing and how to send RESTful requests from an application. In the meantime I’ve included some links in the show notes where you can explore APIs further.

API Tutorial Resources

api tutorial

LEAVE A REPLY

Please enter your comment!
Please enter your name here