Multiple Endpoints
Build complete REST APIs with multiple endpoints. Learn CRUD operations (GET, POST, PUT, DELETE), nested resources, error handling, and API design best practices with a full user management example.
In the previous tutorials, you learned to create basic endpoints and make them dynamic. Now you'll learn how to organize multiple endpoints into a cohesive API structure. You'll build a complete user management system with proper HTTP methods, error handling, and realistic responses.
What you'll learn
How to organize multiple related endpoints
Using different HTTP methods properly (GET, POST, PUT, DELETE)
Creating consistent API responses
Implementing proper error handling
Structuring a complete CRUD (Create, Read, Update, Delete) API
Best practices for API design
What you'll build
A complete user management API with these endpoints:
GET /users- List all usersGET /users/:id- Get a specific userPOST /users- Create a new userPUT /users/:id- Update a userDELETE /users/:id- Delete a userGET /users/:id/posts- Get user's postsPOST /users/:id/posts- Create a post for a userError responses for various scenarios
Prerequisites
Completed Dynamic Responses tutorial
Understanding of HTTP methods and status codes
Basic knowledge of REST API principles
Step 1: Planning your API structure
Before writing configuration, let's plan our API structure:
Users Resource:
List users:
GET /usersGet user:
GET /users/:idCreate user:
POST /usersUpdate user:
PUT /users/:idDelete user:
DELETE /users/:id
Posts Resource (nested under users):
Get user posts:
GET /users/:id/postsCreate user post:
POST /users/:id/postsGet specific post:
GET /users/:id/posts/:postId
Error Handling:
404 for not found resources
400 for bad requests
422 for validation errors
Step 2: Create the basic CRUD endpoints
Create a new moclojer.yml file:
Step 3: Add nested resources (user posts)
Add post-related endpoints under users:
Step 4: Add proper error handling
Real APIs need proper error responses. Add these error endpoints:
Step 5: Add filtering and pagination
Enhance your list endpoints with query parameters:
Step 6: Add API metadata endpoints
Include helpful metadata endpoints:
Step 7: Test your complete API
Start your server and test all the endpoints:
Best practices demonstrated
Your API now follows several important best practices:
✅ Consistent Response Format
All responses have similar structure with consistent field names.
✅ Proper HTTP Status Codes
200 for successful GET, PUT, DELETE
201 for successful POST (creation)
404 for not found
400 for bad requests
422 for validation errors
✅ RESTful URL Structure
/usersfor the collection/users/:idfor individual resources/users/:id/postsfor nested resources
✅ Meaningful Error Messages
Error responses include helpful information for debugging.
✅ Resource Relationships
Posts are properly nested under users, showing the relationship.
✅ Pagination Support
List endpoints include pagination metadata.
Your complete API configuration
Here's your complete moclojer.yml with all endpoints organized:
What you've accomplished
✅ Complete CRUD API - Full Create, Read, Update, Delete operations ✅ Nested Resources - Posts under users showing relationships ✅ Proper HTTP Methods - GET, POST, PUT, DELETE used correctly ✅ Error Handling - 404, 400, 422 responses with meaningful messages ✅ Consistent Structure - All responses follow similar patterns ✅ Query Parameters - Filtering and pagination support ✅ API Metadata - Health checks and documentation endpoints
Next steps
You now have a well-structured API! In the final tutorial, you'll put everything together in a real-world example that showcases advanced features.
👉 Real-World Example - Build a complete e-commerce API with advanced features
Need help?
Want practical examples? See CRUD Operations Guide
Need pagination? Check Pagination Guide
Have questions? Join the community discussions
See Also
CRUD Operations - Complete Create, Read, Update, Delete guide
Pagination - Implement pagination strategies
HTTP Methods - Detailed guide on all HTTP methods
Query Parameters - Filters, search, and sorting
Troubleshooting Guide - Solutions for common issues
Last updated
Was this helpful?