When you are browsing a web page, have you ever wondered: where does this page get all of this information from? The short answer is: a server. In this article we are going to explore what an API is, more specifically a RESTful API, and dive a bit deeper to see how exactly a client communicates with a server to get information.
Knowing the following terms will help you understand
exactly what REST is and what it means to make an API RESTful.
A client is the browser that makes the request for data.
This is typically done through a HTTP request:
If you've never seen a HTTP request here is an example
The server is, as Wikipedia puts it:
a piece of computer hardware or software (computer program) that provides functionality for other programs or devices, called "clients"
In short: servers provide the information that the client requests so as long as the information is something the server can provide.
A real world example of how the client - server relationship works is a night out at a restaurant. You, as a paying customer, are the client. You read the menu, and request the food you may like. Your waiter or waitress is the server. They listen to your request and check to see if it is something they can fulfill. If you ask for pizza at a burger place, you've made a bad request, and it won't get fulfilled.
The acronym API stands for Application Programming Interfaces. Now what exactly does that mean? And why are APIs useful?
According to MDN an API is
a set of commands, functions, protocols, and objects that programmers can use to create software or interact with an external system.
Have you ever used a weather app? More than likely they are using a weather API to update the weather conditions on their app dynamically.
Have you used tinder? This is another great example of API usage as they use a Facebook API to show shared friends and shared interests among potential matches.
If you've ever used
JQuery, you guessed it, you've used an API.
JQuery gives us access to a lot of functions and objects that helps us create software. If you look back at MDN's definition of what an API is, this fits perfectly!
REST stands for REpresentational State Transfer
REST is essentially just an architectural style for designing APIs. Roy fielding began REST as apart of his PHD dissertation where he developed a set of rules that web developers could follow when building their APIs. He brought about the idea that all websites should use the same structure for building their APIs. This made it much easier for clients and servers to work together and use different APIs to work quickly, efficiently, and easily.
There are a few rules that an API has to follow in order to be considered RESTful. The two main rules are:
- Must use HTTP request verbs. Those verbs include:
- GET - gets the request from the server
- POST - posts the request to the server
- PUT & PATCH - updates information on the database
- DELETE - deletes a specific piece of information in the database
- Must use a specific pattern of routes/endpoint URLS
After reading this article see if you can build your own RESTful API.