Learn how to organize and structure multiple endpoints to create a complete API. Build a realistic user management system with proper HTTP methods and error handling.
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
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:
# Get all posts for a user
- endpoint:
method: GET
path: /users/:id/posts
response:
status: 200
headers:
Content-Type: application/json
body: >
{
"user_id": {{path-params.id}},
"posts": [
{
"id": 1,
"title": "First post by user {{path-params.id}}",
"content": "This is the content of the first post",
"created_at": "2024-01-10T09:00:00Z",
"updated_at": "2024-01-10T09:00:00Z",
"status": "published"
},
{
"id": 2,
"title": "Second post by user {{path-params.id}}",
"content": "This is the content of the second post",
"created_at": "2024-01-12T14:30:00Z",
"updated_at": "2024-01-12T14:30:00Z",
"status": "published"
}
],
"total": 2,
"page": 1,
"per_page": 10
}
# Create a new post for a user
- endpoint:
method: POST
path: /users/:id/posts
response:
status: 201
headers:
Content-Type: application/json
Location: /users/{{path-params.id}}/posts/{{json-params.post_id}}
body: >
{
"id": 54321,
"user_id": {{path-params.id}},
"title": "{{json-params.title}}",
"content": "{{json-params.content}}",
"status": "{{json-params.status}}",
"created_at": "2024-01-15T10:30:00Z",
"updated_at": "2024-01-15T10:30:00Z",
"message": "Post created successfully for user {{path-params.id}}"
}
# Get a specific post
- endpoint:
method: GET
path: /users/:id/posts/:postId
response:
status: 200
headers:
Content-Type: application/json
body: >
{
"id": {{path-params.postId}},
"user_id": {{path-params.id}},
"title": "Post {{path-params.postId}} by user {{path-params.id}}",
"content": "This is the detailed content of post {{path-params.postId}}",
"status": "published",
"created_at": "2024-01-10T09:00:00Z",
"updated_at": "2024-01-10T09:00:00Z",
"author": {
"id": {{path-params.id}},
"name": "User {{path-params.id}}",
"email": "user{{path-params.id}}@example.com"
},
"tags": ["tutorial", "example", "moclojer"]
}
Step 4: Add proper error handling
Real APIs need proper error responses. Add these error endpoints:
# User not found
- endpoint:
method: GET
path: /users/999
response:
status: 404
headers:
Content-Type: application/json
body: >
{
"error": "Not Found",
"message": "User with ID 999 not found",
"code": "USER_NOT_FOUND",
"timestamp": "2024-01-15T10:30:00Z"
}
# Bad request for invalid user creation
- endpoint:
method: POST
path: /users/invalid
response:
status: 400
headers:
Content-Type: application/json
body: >
{
"error": "Bad Request",
"message": "Invalid request format",
"code": "INVALID_REQUEST",
"timestamp": "2024-01-15T10:30:00Z"
}
# Validation error
- endpoint:
method: POST
path: /users/validation-error
response:
status: 422
headers:
Content-Type: application/json
body: >
{
"error": "Validation Failed",
"message": "The request data is invalid",
"code": "VALIDATION_ERROR",
"errors": [
{
"field": "email",
"message": "Email is required"
},
{
"field": "name",
"message": "Name must be at least 2 characters"
}
],
"timestamp": "2024-01-15T10:30:00Z"
}
Step 5: Add filtering and pagination
Enhance your list endpoints with query parameters: