What is REST API? #
REST stands for representational state transfer, and is a type of application programming interface (API) that enables applications to talk together.
In order for APIs to work they need to conform to a particular protocol – the rules that guide what the API can share and how it achieves it. One of the most widely used protocols for accessing applications and web services is REST API.
REST, also known as RESTful, was developed after SOAP API with the aim of making it simpler to access web services.
Unlike SOAP, REST is more of an architectural style rather than a protocol as it isn’t constrained by the formats it can use.
Whilst it also uses HTTP/HTTPS to send requests between systems, it can also use the CSV (Command Separated Value), JSON (JavaScript Object Notation) and RSS (Really Simple Syndication) data formats as well as XML.
In a REST API, resources are identified by URIs (Uniform Resource Identifiers) and can be accessed using HTTP methods such as GET, POST, PUT, DELETE, etc. The API then uses the HTTP protocol to transfer data in the JSON or XML formats.
REST API example #
The following is an example of using REST API.
Let’s say we have a web application that manages a library of books. We want to build a REST API that allows users to perform create, read, update and delete operations on the books in the library.
Retrieving a list of all books
GET /books
This endpoint would return a list of all the books in the library.
Retrieving a specific book
GET /books/{id}
This endpoint would return the book with the specified ID.
Creating a new book
POST /books
The client would send a JSON object containing the details of the new book in the request body. The server would then create a new book with a unique ID and return the ID to the client.
Updating an existing book
PUT /books/{id}
The client would send a JSON object containing the updated details of the book in the request body. The server would then update the book with the specified ID and return a success message to the client.
Deleting a book
DELETE /books/{id}
This endpoint would delete the book with the specified ID and return a success message to the client.
What are the benefits of REST API? #
REST is popular with developers as it offers a powerful and flexible way to build modern web applications, enabling them to build scalable, maintainable, and performant systems. It also provides a much simpler way of integrating business applications and systems.
The majority of developers are also familiar with its frameworks and features, such as Secure Sockets Layer (SSL) encryption and Transport Layer Security (TLS). It is also less bandwidth-intensive than SOAP due to the structuring of its messages.
There are numerous benefits of using a REST API, including:
- Scalability: REST APIs are highly scalable, making it easy to add new resources or update existing ones without requiring changes to the underlying architecture.
- Flexibility: REST APIs are platform-independent, meaning they can be used with any programming language or framework, making them highly flexible.
- Simplicity: REST APIs are easy to understand and use, as they use standard HTTP methods and data formats, such as JSON or XML.
- Performance: REST APIs are designed to be lightweight, making them highly performant, with the ability to easily handle a large volume of requests.
- Separation of concerns: REST APIs separate the client-side and server-side concerns, allowing developers to focus on specific aspects of the application, which leads to cleaner code and better maintainability.
- Cacheability: REST APIs can be cached by clients, which can improve performance and reduce the number of requests to the server.
- Security: REST APIs can be secured using standard HTTP authentication mechanisms, such as OAuth, making it easy to secure API resources.
Related REST API terms #
What is HTTP? #
HTTP (Hypertext Transfer Protocol) is an application-layer protocol for transmitting text, images, sound and video files between web browsers and web servers. The process can then return the resources in a format that the receiving application can interpret, which is achieved by using the XML data format.
What is XML? #
XML (Extensible Markup Language) is a text format containing codes or tags that describe the digital document being sent over the internet, including how it should be structured, stored and transported. It is similar to HTML which is used by web pages.