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 users

  • GET /users/:id - Get a specific user

  • POST /users - Create a new user

  • PUT /users/:id - Update a user

  • DELETE /users/:id - Delete a user

  • GET /users/:id/posts - Get user's posts

  • POST /users/:id/posts - Create a post for a user

  • Error 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 /users

  • Get user: GET /users/:id

  • Create user: POST /users

  • Update user: PUT /users/:id

  • Delete user: DELETE /users/:id

Posts Resource (nested under users):

  • Get user posts: GET /users/:id/posts

  • Create user post: POST /users/:id/posts

  • Get 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

  • /users for the collection

  • /users/:id for individual resources

  • /users/:id/posts for 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?

See Also

Last updated

Was this helpful?