Published using Google Docs
Tech blog: REST API Best practices
Updated automatically every 5 minutes

Declaration: This is not an original piece of work but a set of notes derived from the original sources as mentioned in the link.

What Does RESTful API Mean?

"RESTful" implies a few features:

So, the RESTful API is a service that follows these rules (hopefully) and uses HTTP methods to manipulate the set of resources.

Characteristics of a good API

HTTP methods and it’s meaning

Best practices for REST APIs

Abstract vs Concrete APIs

Let's look at two API versions. Is it better to have an API that has one /entities or an API that has /owners, /blogs and, /blogpostsseparately?

Which one seems more descriptive to you as a developer? Which API would you rather use?

URI Formatting (Nouns, not Verbs): Good URL vs Bad URL Examples

Bad endpoint examples

/getAllBlogPosts

/updateBlogPost/12

/deleteBlogPost/12

/getAuthorById/3

/deleteAuthor/3

/updateAuthor/3

Good endpoints examples

GET /blogposts - gets all the blog posts.

GET /blogposts/12 - gets the blog post with the id 12.

POST /blogposts - adds a new blog post and returns the details.

DELETE /blogposts/12 - removes the blog post with the id 12.

GET /authors/3/blogposts - gets all the blog posts of the author with id 3.

Error Handling

This is another important aspect of  API building. There are a few good ways to handle errors.

Status Codes

Should you have a strict status code for every situation?There are over 70 status codes out there. Do you know them by heart? Will the potential API user know them all?

Most developers are familiar with the most common status codes:

By starting with these three, you can cover most of the functionalities of your REST API.

Other commonly seen codes include:

It’s recommended to help the user by using a limited number of codes and descriptive messages

REST API Versioning

This approach is a great way to introduce new changes while letting some users continue with old versions. Before you start making your API, you can version your API by prefixing the endpoints by the API version:

https://api.example.com/v1/authors/2/blogposts/13

This way you can always increment your API version number (eg. v2, v3...) whenever there are breaking changes in your API. This also signals to the users that something drastic has changed and they should be careful when using the new version.

Use Proper HTTP Headers for Serialization Formats

Both client and server need to know which format is used for communication. The format has to be specified in the HTTP-Header.

Content-Type defines the request format.

Accept defines a list of acceptable response formats.

Get Method and Query Parameters Should Not Alter the State

We must use PUT, POST and DELETE methods instead of the GET method to alter the state. Do not use GET for state changes:

GET /users/711?activate or GET /users/711/activate etc are wrong

Use Sub-Resources for Relations

If a relation can only exist within another resource, RESTful principles provide useful guidance. If a resource is related to another resource using subresources.

GET /cars/711/drivers/ Returns a list of drivers for car 711

GET /cars/711/drivers/4 Returns driver #4 for car 711

Other examples:

Searching, Sorting, Filtering, and Pagination

All of these actions are simply the query on one dataset. There will be no new set of APIs to handle these actions. We need to append the query params with the GET method API. Let’s understand with a few examples of how to implement these actions.

GET /employees?state=internal&title=senior

GET /employees?id=1,2

The JSON:API way of filtering is:

GET /employees?filter[state]=internal&filter[title]=senior

GET /employees?filter[id]=1,2

/employees?offset=30&limit=15 # returns the employees 30 to 45.

If the client omits the parameter you should use defaults (like offset=0 and limit=100). Never return all resources. If the retrieval is more expensive you should decrease the limit.

/employees                    # returns the employees 0 to 100

Use Consistently Plural Nouns

Prefer

/employees

/employees/21

over

/employee

/employee/21

Format to use Rest API URL

http(s)://{Domain name (:Port number)}/{A value indicating REST API}/{API version}/{path for identifying a resource}

http(s)://{Domain name indicating REST API(:Port number)}/{API version}/{path for identifying a resource}

Good API Reference

https://developer.paypal.com/docs/api/orders/v2/

Documentation

We should always have proper documentation for our APIs. For example:

Swagger is another tool that can be explored

References:

https://dzone.com/articles/top-rest-api-best-practices

https://phauer.com/2015/restful-api-design-best-practices/