Română

A Brief Introduction to REST APIs: Principles and Best Practices

A Brief Introduction to REST APIs: Principles and Best Practices
09.03.2022
Note: This article is over a year old. Some information may be outdated. We recommend reading the latest documentation or talking to one of our experts.
A REST API (known also as RESTful API) is an application programming interface (API) which uses HTTP requests to access the data. Usualy it uses 4 HTTP methods: GET, PUT, POST, DELETE to read, update, create or delete a resource.
An API is a collection of methods that allows two software applications to communicate and exchange data.
Rules of REST APIs:
1) REST is based on the resource name or noun. A URI of a REST API should always end with a noun.
2) HTTP methods are used to identify an action - GET, POST, UPDATE, DELETE
3) A web application must be organized into resources and HTTP verbs to modify those resources.
4) Always use plurals in URL for consitency
5) Send a correct HTTP code to indicate success or failure

Example of a simple REST API:
api/books GET Get all books
api/books/new GET Show form for adding new book
api/books POST Add a new book
api/books/1 PUT Update a book with id = 1
api/books/1/edit GET Show edit form for book with id = 1
api/books/1 DELETE Delete a book with id = 1
api/books/1 GET Get a book with id = 1

In building an RESTful API, 6 REST arhitectural constraints must be taken into consideration:
1) Uniform Interface. All the resources must be uniquely identified through an URL, and only by using methods such as DELETE, PUT, GET, POST someone can access a resource. It should be a unique way of interacting with a server, independent of the device type or application.
2) Stateless. All operations must be stateless and all state management must take place on the client, not the server.
3) Cacheable. All resources should allow caching
4) Client-Server. A clear distinction between client and server must exist. For example, UI is a concept in the client's domain and data access and security in the server's domain.
5) Layered System. REST allows for an architecture composed of multiple layers of servers.
6) Code on demand. Usualy, the server will send to the client resources in the form of JSON but when necessary, servers can send back executable code to the client.

OPTI Systems makes the process of creating an API straightforward by following these steps:
1) Determine the client’s requirements. We analyze functional and nonfunctional requirements and determine what exactly the API needs to do, the expected performance and response time and concerns with regard to data protection.
3) Developing the API. OPTI has highly skilled developers and will implement the API in the shortest time and in a cost effective manner.
4) Testing the API. The goal of testing the API is to make sure it works under different conditions and the security meets the highest possible standards
5) Publish the API.
6) Monitor the API. After testing and launching we will monitor your API to understand how it is being used and how it performs.

Quick Questions

What does it mean for a REST API to be 'stateless'?

It means the server does not store any information about the client's session. Each request sent from the client to the server must contain all the information necessary to be understood and processed, independent of previous requests.

What is the difference between the HTTP methods POST and PUT?

Generally, POST is used to create a new resource (e.g., adding a new user). PUT is used to update an existing resource in its entirety. If the resource does not exist, PUT can also create it.

Is JSON the only data format that can be used in a REST API?

No, but it is the most popular due to its simplicity and readability. REST APIs can also use other formats, such as XML, but JSON has become the de facto standard for most modern web applications.

What is the TLDR (conclusion)?

Understanding these principles is crucial for any software development project that involves communication between different systems. A well-designed REST API acts as a stable and predictable contract between a client and a server, enabling seamless data integration.

What technologies and methodologies are involved?

Technologies: REST API, HTTP, JSON, API Design
Methodologies: REST API, Software architecture, Data integration, Web development

Marian Călborean

Article written by

Marian Călborean

Manager, Software Architect, PhD. in Logic, Fulbright Visiting Scholar (CUNY GC, 2023)

See on LinkedIn →
Interesat?

Interested?

Schedule a meeting

Get a Free Audit

News and Guides

More News