Introduction to REST APIs

Learn about REST APIs and their importance in building software solutions.

Welcome to the course!

We will understand REST by starting with an example. Suppose that you work for an ice cream shop and are trying to build a web application to show the flavors of ice cream that are in stock that day. You want to allow the workers to make updates to those flavors. How can you do this?

Well, with a REST API. You have a web app or web page communicating with a cloud-based server via a REST API. Let’s jump into what exactly a REST API is.

What does REST stand for?

The term REST stands for Representational State Transfer which is essentially a standardized software architecture style. It is a specific type of API that is widely known and used by the industry.

The first thing that you should really know about REST APIs is that they are all about communication.

You may have heard the term restful. So, a restful web service is a service that uses REST APIs to communicate.

What are the benefits of REST APIs?

  • REST APIs offer simple and standardized approach to communication. You don’t have to worry about how to format your data or how to format your request each time — it’s all standardized and industry used.

  • REST APIs are scalable and stateless. As your service grows in complexity, you can easily make modifications. Additionally, because of them being stateless, you don’t have to worry about what data is in which state or keep track of that across client and server. It’s truly stateless.

  • REST APIs have high performance in large part due to the fact that they support caching. So even when your service gets more complex, the performance stays very high.

Some APIs, such as SOAP or XML-RPC, impose a strict framework on developers, but REST APIs can be developed using virtually any programming language and support a variety of data formats. The only requirement is that they should align to the following six REST design principles, also known as architectural constraints:

  1. Client-Server: In REST API design, client and server applications must be completely independent of each other. This creates a separation of concerns, letting each application grow and scale independently of the other and allowing your organization to grow quickly and efficiently.

  2. Stateless: REST APIs are stateless, meaning that each request needs to include all the information necessary for processing it. A REST API should not rely on data being stored on the server or sessions to determine what to do with a call, but should rather solely rely on the data that is provided in that call itself. It means that no data is stored on the server related to the client request.

  3. Cache: When possible, resources should be cacheable on the client or server-side. The goal is to improve performance on the client-side while increasing scalability on the server-side.

  4. Uniform interface: The uniform interface lets the client talk to the server in a single language, independent of the architectural back-end of either.

  5. Layered system: Don’t assume that the client and server applications connect directly to each other. There may be a number of different intermediaries in the communication loop. REST APIs need to be designed so that neither the client nor the server can tell whether it communicates with the end application or an intermediary.

  6. Code on demand (optional): REST APIs usually send static resources, but in certain cases, responses can also contain executable code (such as Java applets). In these cases, the code should only run on-demand.

Let’s go back to our example. What would the REST API look like for the ice cream shop?

You have an endpoint that might look something like this:

icecream.com/api/flavors 

Let’s break that apart a bit. The api in the above endpoint signifies that this is the API portion of the endpoint. Pretty straightforward there.

flavors the actual resource. This signifies that we are working with the flavors resource in this REST API. In our example, the main building blocks, or parts, of the REST API are as follows:

  • The request that is sent from the client to the server
  • The response that is received back from the server