REST API

Get an overview of REST APIs and their constraints and endpoints.

Overview

REST is a software architecture implemented to create web services. It follows certain constraints to ensure consistent and reliable communication between the client and server.

Constraints

The REST architecture has a few key principles that guide its design and behavior. These include:

  • Client-Server: The architecture is separated into two main components, the client and the server. The client is responsible for the user interface and user experience, while the server is responsible for managing the resources and business logic.

  • Stateless: The server doesn’t maintain any information about the client’s state; each request from the client contains all the information necessary for the server to understand and respond to the request.

  • Cacheable: Responses from the server can be cached by the client, allowing for faster response times and reducing the load on the server.

  • Layered system: The architecture is divided into layers, each having a specific responsibility. The client communicates with the server through the interface provided by the top layer, while the bottom layer interacts with the database or other resources.

Endpoints and methods

A RESTful API comprises a set of endpoints, each representing a specific resource and a set of methods, or HTTP verbs, that can be used to interact with the resource.

  • GET: Retrieve a resource or a list of resources.
  • POST: Create a new resource.
  • PUT: Update an existing resource.
  • DELETE: Delete a resource.

Example

An example of a RESTful API for a blog website could have the following endpoints:

  • GET /posts: Retrieve a list of all the blog posts.
  • GET /posts/:id: Retrieve a specific blog post by ID.
  • POST /posts: Create a new blog post.
  • PUT /posts/:id: Update an existing blog post.
  • DELETE /posts/:id: Delete a specific blog post.

A client could request the GET /posts endpoint to retrieve a list of all the blog posts, and then request the GET /posts/:id endpoint to retrieve a specific post, passing the ID as a parameter.

Get hands-on with 1200+ tech skills courses.