RESTful API Design Principles
RESTful API Design Principles
A REST API (also called a RESTful API or RESTful web API) is an application
programming interface (API) that conforms to the design principles of the representational
state transfer (REST) architectural style. REST APIs provide a flexible, lightweight way to
integrate applications and to connect components in microservices architectures.
Uniform interface
API requests for the same resource should look the same, no matter where the
request comes from. The REST API should ensure that the same piece of data, such as the
name or email address of a user, belongs to only one uniform resource identifier (URI).
Resources shouldn’t be too large but should contain every piece of information that the
client might need.
Client-server decoupling
In REST API design, client and server applications must be completely independent
of each other. The only information that the client application should know is the URI of the
requested resource; it can't interact with the server application in any other ways.
Similarly, a server application shouldn't modify the client application other than passing it
to the requested data via HTTP.
Statelessness
REST APIs are stateless, meaning that each request needs to include all the
information necessary for processing it. In other words, REST APIs do not require any
server-side sessions. Server applications aren’t allowed to store any data related to a client
request.
Cacheability
When possible, resources should be cacheable on the client or server side. Server
responses also need to contain information about whether caching is allowed for the
delivered resource. The goal is to improve performance on the client side, while increasing
scalability on the server side.
For example, a REST API would use a GET request to retrieve a record. A POST request
creates a new record. A PUT request updates a record, and a DELETE request deletes one. All
HTTP methods can be used in API calls. A well-designed REST API is similar to a website
running in a web browser with built-in HTTP functionality.
Request headers and parameters are also important in REST API calls because they
include important identifier information such as metadata, authorizations, uniform resource
identifiers (URIs), caching, cookies and more. Request headers and response headers, along
with conventional HTTP status codes, are used within well-designed REST APIs.
REST API best practices
Although flexibility is a big advantage of REST API design, that same flexibility makes
it easy to design an API that’s broken or performs poorly. For this reason, professional
developers share best practices in REST API specifications.
Securing a REST API also starts with industry best practices. Use hashing algorithms
for password security and HTTPS for secure data transmission. An authorization framework
like OAuth 2.0 can help limit the privileges of third-party applications.
Using a timestamp in the HTTP header, an API can also reject any request that arrives
after a certain time period. Parameter validation and JSON Web Tokens are other ways to
ensure that only authorized clients can access the API.