What you’ll build
A REST API with:- List all users
- Get user by ID with 404 handling
- Create new users with validation
- Update existing users
- Delete users
- CORS support
- Slow response and error scenarios
name: User API
version: "1.0"
description: Basic user management API for examples
server:
port: 9001
base_path: /api/v1
cors:
enabled: true
origins: ["*"]
fixtures:
users:
- id: 1
username: "alice"
email: "[email protected]"
role: "admin"
- id: 2
username: "bob"
email: "[email protected]"
role: "user"
endpoints:
- method: GET
path: /users
description: List all users
responses:
200:
content_type: application/json
body: |
{{#each fixtures.users}}
{
"id": {{id}},
"username": "{{username}}",
"email": "{{email}}",
"role": "{{role}}"
}{{#unless @last}},{{/unless}}
{{/each}}
- method: GET
path: /users/{id}
description: Get user by ID
responses:
200:
condition: "{{eq params.id '1'}}"
content_type: application/json
body: |
{
"id": 1,
"username": "alice",
"email": "[email protected]",
"role": "admin"
}
404:
condition: "{{ne params.id '1'}}"
content_type: application/json
body: |
{
"error": "User not found",
"message": "No user with ID {{params.id}}"
}
- method: POST
path: /users
description: Create new user
responses:
201:
content_type: application/json
body: |
{
"id": {{faker "datatype.number"}},
"username": "{{request.body.username}}",
"email": "{{request.body.email}}",
"role": "user",
"created_at": "{{now}}"
}
400:
condition: "{{not request.body.email}}"
content_type: application/json
body: |
{
"error": "Validation failed",
"message": "Email is required"
}
scenarios:
- name: "slow_response"
description: "Simulate slow API responses"
delay_ms: 2000
endpoints: ["GET /users"]
- name: "error_mode"
description: "Return errors for testing failure scenarios"
endpoints: ["POST /users"]
response:
status: 503
body: |
{
"error": "Service temporarily unavailable"
}
behavior:
request_logging: true
response_delays:
min_ms: 50
max_ms: 200
List all users
Get user by ID
Get non-existent user (404)
Create a new user
Create user without email (validation error)
[
{
"id": 1,
"username": "alice",
"email": "[email protected]",
"role": "admin"
},
{
"id": 2,
"username": "bob",
"email": "[email protected]",
"role": "user"
}
]
{
"id": 42567,
"username": "charlie",
"email": "[email protected]",
"role": "user",
"created_at": "2024-01-15T10:30:00Z"
}
curl -X POST http://localhost:9001/api/v1/users \
-H "Content-Type: application/json" \
-H "X-Scenario: error_mode" \
-d '{"username": "test", "email": "[email protected]"}'
Key features demonstrated
CORS support
Enable cross-origin requests for frontend development:Conditional responses
Return different responses based on request data:Request validation
Validate required fields before processing:Fixtures for test data
Define reusable user data once:{{fixtures.users}}.
Response delays
Add realistic network latency:Scenario-based testing
Test different conditions without modifying code:X-Scenario header.
Generate TypeScript types
Create type-safe client code automatically:Generate React Query hooks
Create ready-to-use React hooks:Export to OpenAPI
Convert your service to an OpenAPI specification:- Import into Postman or Insomnia
- Generate documentation with Swagger UI
- Share API contracts with your team
Next steps
- Add update (PUT) and delete (DELETE) endpoints
- Implement pagination for the user list
- Add authentication with JWT tokens
- Set up contract testing against a real API
- Use the TUI for visual service management:
apicentric tui