RESTful API URL Design: Developer-Friendly Endpoint Guide
Best practices for RESTful API endpoint design. Create clean, consistent, and scalable API URL structures.
In RESTful API design, URL structure directly affects your API's usability and maintainability. In this guide, we learn how to design developer-friendly API endpoints.
REST Fundamentals
What is REST?
Representational State Transfer (REST) is an architectural style for web APIs. It represents resources through URLs and uses HTTP methods.
HTTP Methods
GET → Read resource
POST → Create new resource
PUT → Replace resource entirely
PATCH → Partially update resource
DELETE → Delete resourceURL Design Principles
1. Use Nouns, Not Verbs
# WRONG - Using verbs
GET /getUsers
POST /createUser
PUT /updateUser/123
DELETE /deleteUser/123
# CORRECT - Using nouns
GET /users → Get all users
GET /users/123 → Get single user
POST /users → Create new user
PUT /users/123 → Update user
DELETE /users/123 → Delete user2. Use Plural Nouns
# WRONG
/user
/product
/order
# CORRECT
/users
/products
/orders3. Hierarchical Structure
# User's orders
GET /users/123/orders
# Order's items
GET /orders/456/items
# User's specific order
GET /users/123/orders/4564. Use Lowercase and Hyphens
# WRONG
/userProfiles
/User_Profiles
/USER-PROFILES
# CORRECT
/user-profilesVersioning
Version in URL
# Most common method
https://api.example.com/v1/users
https://api.example.com/v2/usersVersion in Header
GET /users
Headers:
Accept: application/vnd.example.v1+json
Accept: application/vnd.example.v2+jsonFiltering, Sorting, and Pagination
Filtering
# Simple filtering
GET /products?category=electronics
GET /products?category=electronics&brand=apple
# Comparison operators
GET /products?price[gte]=100&price[lte]=500
GET /products?price_min=100&price_max=500
# Search
GET /products?search=iphone
GET /users?q=johnSorting
# Single field sorting
GET /products?sort=price
GET /products?sort=-price # Descending
# Multiple field sorting
GET /products?sort=category,-pricePagination
# Offset-based
GET /products?page=2&per_page=20
GET /products?offset=20&limit=20
# Cursor-based (more performant)
GET /products?cursor=abc123&limit=20Sub-Resource Relationships
Nested Resources
# User's blog posts
GET /users/123/posts
POST /users/123/posts
GET /users/123/posts/456
# Maximum 2-3 levels deep
GET /users/123/posts/456/comments # OK
GET /users/123/posts/456/comments/789/likes # Too deep!Flat vs Nested
# Nested - Relationship-focused
GET /users/123/orders
# Flat - More flexible
GET /orders?user_id=123
# Both valid, choose based on use caseSpecial Endpoints
Actions
# Special operations on resources
POST /users/123/activate
POST /users/123/deactivate
POST /orders/456/cancel
POST /payments/789/refund
# Alternative: Status update with PATCH
PATCH /users/123
{ "status": "active" }Batch Operations
# Bulk delete
DELETE /users?ids=1,2,3,4,5
# Bulk update
PATCH /users/batch
[
{ "id": 1, "status": "active" },
{ "id": 2, "status": "inactive" }
]HTTP Status Codes
# Success
200 OK → GET, PUT, PATCH successful
201 Created → POST successful, new resource created
204 No Content → DELETE successful
# Client Errors
400 Bad Request → Invalid request
401 Unauthorized → Authentication required
403 Forbidden → No permission
404 Not Found → Resource not found
409 Conflict → Conflict (duplicate, etc.)
422 Unprocessable→ Validation error
429 Too Many Req → Rate limit exceeded
# Server Errors
500 Internal → Server error
503 Unavailable → Service unavailableConclusion
Well-designed API URLs increase your API's usability and ease maintenance. Create developer-friendly APIs using consistent naming, logical hierarchy, and standard HTTP methods. Design your API endpoints with the same care you optimize web URLs with tools like SlugURL.