..

API's Design - Not pieces of advice

Okay, okay, everything depends. But try to forget about it for a while, try to be more material. Doing this maybe you will be a better developer.

API hierarchy

The “/” should be separating the system hierarchy.

Like this:

1/users
2/users/{userId}
3/users/{userId}/orders
4/users/{userId}/orders/{orderId}
5/products
6/products/{productId}
7/categories
8/categories/{categoryId}

Queries

Queries can help you identify different endpoints. They’re our bread and butter overflow.

1/articles?author=john_doe&tags=technology,science
2/events?location=new_york&start_date=2024-09-01

These examples demonstrate how queries can be used to filter, sort, and specify parameters for API endpoints:

Queries allow for flexible and powerful data retrieval, BUT you can use this to differentiate your endpoints sometimes! (You need to be evil sometimes)

Response Status Codes I hate this part too much, but know this is crucial to have a good communication.

  1. 1xx (Informational): The request was received, continuing process.
  2. 2xx (Successful): The request was successfully received, understood, and accepted.
  3. 3xx (Redirection): Further action needs to be taken in order to complete the request.
  4. 4xx (Client Error): The request contains bad syntax or cannot be fulfilled.
  5. 5xx (Server Error): The server failed to fulfill an apparently valid request.

Common HTTP Response Status Codes

1xx: Informational

2xx: Successful

3xx: Redirection

4xx: Client Error

5xx: Server Error

Head and body (because I don’t wanna know about arms and legs)

Use headers to control cache, organize your authentication or to organize your responses. The term organize your responses is not good, sorry, so let me give you an example:

Content-Type

The Content-Type header is used to specify the media type of the resource being sent to the server or being returned to the client. This tells the server or client the data type contained in the body of the request or response.

Accept Header

The accept header tells the server what type(s) of content the client is prepared to accept in the response. The server then tries to provide the response in one of the acceptable formats. If it can’t, it may return a 406 Not Acceptable status.

Now the body, this is the rest, while the headers take care about the the healthy of the communication, the body is our envelope, our data container. So throw every sort of trash here, try to not leave a mess to other people deal with.

Take a look in:

WRML notation

REST API Design Rulebook: Designing Consistent RESTful Web Service Interfaces (Mark Masse)

Thanks, bye!