iTranslated by AI
API Design and URL Naming Conventions
Update 2024/05
I believe Google's API Design Guide will be a useful reference.
Book
API Design Patterns (Compass Books series)
However, whether the custom method :method can be applied likely depends on the routing of the framework you are using.
Conclusion
API design is easier when you effectively incorporate commands into resource-based naming, rather than relying on strictly resource-based or strictly command-based naming. Furthermore, when the number of commands grows too large, check if there are hidden resources there and perform API refactoring.
The Popularity of REST and Resource-based Naming Conventions
With the influence of Rails, RESTful became popular, leading to the widespread practice of representing URLs through resource hierarchies and their corresponding HTTP methods.
For example, use cases such as:
- List cats
- Create a new cat
- Update cat information
- Delete cat information
would look like this:
- #GET /cats
- #POST /cats
- #PUT /cats/:id
- #DELETE /cats/:id
However, while it seems easy to understand when you learn it, things aren't so simple once you start actual implementation.
For example, how should you represent an API that wants to set a cat's status to an ACTIVE state?
If we force ourselves to think in terms of resources, it might look like this:
Considering it a status change:
- #PUT /cats/:id
Or, defining a resource for `ACTIVE` cats:
- #POST /active-cats
Simply resolving it with a PUT request is an oversimplification, and the intent is lost. Also, defining a resource like active-cats just for this purpose seems like it would unnecessarily increase the number of controllers and be a burden.
Command-based Naming Conventions
Command-based here refers to a method where the POST method is used for everything, and what you want to do (the command) is expressed in the URL.
- #POST /get-cats
- #POST /create-cats
- #POST /update-cats
- #POST /delete-cats
It is not as well-organized as resource-based naming.
Merging Resource-based and Commands
Experienced API designers might do this unconsciously. The problem with resource-based design lies in the difficulty of handling updates (UPDATE, PUT). Therefore, while maintaining the basic resource-based approach, we treat specific derived update use cases as commands.
General use cases:
- List cats
- Create a new cat
- Update cat information
- Delete cat information
- #GET /cats
- #POST /cats
- #PUT /cats/:id
- #DELETE /cats/:id
A specific use case such as "an API to set a cat's status to ACTIVE."
- #PUT /cats/:id/activate
This also aligns well with object-oriented thinking (there is an object first, and then several methods are associated with it).
Points to Note when Merging Resource-based Design and Commands
There are times when it is better to use a resource instead of a command.
For example, when there are multiple APIs for cats in an ACTIVE state, the following is not well-organized.
- #PUT /cats/:id/do1-for-active
- #PUT /cats/:id/do2-for-active
- #PUT /cats/:id/do3-for-active
Instead, in such cases, it becomes easier to build APIs by identifying a new resource.
- #PUT /active-cats/:id/do1
- #PUT /active-cats/:id/do2
- #PUT /active-cats/:id/do3
Discussion