- Home
- API Design
- How to Design URL for REST API ...
While designing the rest API, deciding URL is a very important part as it’s the entry point to your API.
The key principles of REST involve separating your API into logical resources.
URL Design Rules
- Keep it simple
- This is not the method or class name
- Forward slash separator (/) must be used to indicate a hierarchical relationship
- Lowercase letters should be preferred in URI paths
- Don’t use verbs in the base URLs
- If you follow method driven approach while designing a URL will end up in verbs in the URL
- Resource Names don’t mix up singular and plural nouns, use only plural nouns for all resources
Example of the Orders API
GET /orders # Retrieves a list of orders GET /orders/1 # Retrieves a specific order 1 POST /orders # Creates a new order PUT /orders/1 # Updates order 1 PATCH /orders/1 # Partially updates order 1 DELETE /orders/1 # Deletes order 1
Resource Relations
One of the principles of the RESTful architecture style is that these relationships are expressed by hyperlinks to the representation of a resource.
Example of the OrderItems in Orders API
GET /orders/1/orderitems # Retrieves a list of orderitems for order 1 GET /orders/1/orderitems/1 # Retrieves 1 orderitem for order 1 POST /orders/1/orderitems # Creates a new order PUT /orders/1/orderitems/1 # Updates 1 orderitem for order 1 PATCH /orders/1/orderitems/1 # Partially updates 1 orderitem for order 1 DELETE /orders/1/orderitems/1 # Deletes 1 orderitem for order 1