RESTful API is a way for a client (website, mobile app, etc.) to communicate to a web service regarding a resource (data, picture, web page, etc.). An example of this is when you use Google search. You will type in a parameter instructing Google to retrieve and display data for you to view. This article will explain what RESTful API is and how it is commonly used. You can read a brief guide to creating an API with Node and Express in the article A Simple Guide to Node/Express.
Application Programming Interface (API) is the instructions for asking a web service how to request a resource from it. To put this in human terms, humans learn a language to talk to one another. We go to a restaurant, read the menu to determine the exact type of food we can order, create an order, and the restaurant knows what food to give you. The menu is the API.
REST or RESTful stands for Representational State Transfer.This is an architectural system to make an API request to a web service to get or update data/resources. Here is the list of guidelines that I found the most useful to know:
- The web service is recommended to be stateless. This means nothing has to be remembered by the web service for the next transaction.
- Each transaction is independent of past transactions by a client. The web service itself does not remember each client. This allows the web service to be scalable when the volume of requests changes.
- The web service should focus on a client server relationship. A web service should be developed around the one to many concept instead of a unique connection to each client that asks for a resource. This allows a web service the ability to scale up with demand. An example is Twitter. Its web service is able to scale up the number of tweets issued during important events because the one web service can be use by untold millions of users (clients).
- The web service should have all resources available upon demand. An example is a restaurant web page that sends out menus as PDFs. There is not a finite number of digital PDFs. If 1000 people ask for it, 1000 PDFs get sent.
REST APIs architectural format is to use a url (the HTTP verb isn’t seen but it is implied) and if neccessary a parameter with data. Another way to describe this format is to state the location of the resource, what you want to do with it, and additional specific instructions you want sent to the web service. Below is a description of each part of this format.
An example of the REST API format is https:/fastfoodmenu.com/api/burgers. This API call from a client will send a GET request to a web service at https:/fastfoodmenu.com/ to get all burgers that the instructions say will be sent if any random client types this specific command.
REST APIs use an Uniform Resource Locator (URL) to allow a client to access an endpoint or pointer to the web service. A familiar example is www.google.com. This url asks for a web service for the Google homepage.
REST APIs use HTTP requests verbs/methods to tell the web service what type of request the client is making. GET, POST, PUT, DELETE are the most commonly used methods.
- GET: This is used to retrieve data/resources from the web service. An example is when a user logs onto Twitter with their mobile app. The app makes a REST API call to Twitter to get the most recent tweets.
- POST: This is used to submit data/sources to a web service. An example is creating a username and password when creating a new profile.
- PUT: This is used to submit data/resources to a web service to update something that is already there. An example is when you edit a facebook post.
- DELETE: This is used to delete a specific data/resource from a web service.
The data retrieved from a RESTful API call can be in a variety of formats. The most familiar is a JSON format. This uses a series of key value pairs to organize data. Other formats include PDF, images, XML, etc.
You can follow my journey doing #100DaysOfCode on Twitter at JCSmiley4 or connect with me on LinkedIn at JC Smiley Jr. As always, let’s have fun and do lots of victory dancing.