REST API Demo
This demo showcases how to build RESTful APIs using StoneJS Framework's dhandler system. All endpoints below are fully functional and return JSON responses.
What is a Dhandler?
A dhandler is a special file that handles all requests for paths that don't have a specific file. It's perfect for building dynamic APIs with route parameters.
API Overview
The API is implemented in /pages/demo/api/dhandler and handles all requests to /demo/api/*.
Available Endpoints
GET
/demo/api
Description: API information and available endpoints
Response:
Try it:
Open in new tab →Users API
GET
/demo/api/users
Description: List all users with pagination support
Query Parameters:
| Parameter | Type | Description |
|---|---|---|
page |
integer | Page number (default: 1) |
limit |
integer | Items per page (default: 10) |
Response:
Try it:
/demo/api/users → /demo/api/users?page=1&limit=2 →
GET
/demo/api/users/:id
Description: Get a specific user by ID
Path Parameters:
id- User ID (integer)
Response (Success):
Response (Not Found):
Try it:
/demo/api/users/1 → /demo/api/users/2 → /demo/api/users/999 (404) →
POST
/demo/api/users
Description: Create a new user (demo only - not persisted)
Request Body:
Response:
Try it with curl:
Products API
GET
/demo/api/products
Description: List all products with optional price filtering
Query Parameters:
| Parameter | Type | Description |
|---|---|---|
minPrice |
float | Minimum price filter |
maxPrice |
float | Maximum price filter |
Response:
Try it:
/demo/api/products → /demo/api/products?minPrice=50&maxPrice=200 →
GET
/demo/api/products/:id
Description: Get a specific product by ID
Path Parameters:
id- Product ID (integer)
Response:
Try it:
/demo/api/products/1 → /demo/api/products/3 →Session API
GET
/demo/api/session
Description: Get current session information
Response:
Try it:
/demo/api/session →Cache Demo API
GET
/demo/api/cache-demo
Description: Demonstrates caching with a 30-second TTL
Response:
Try it:
/demo/api/cache-demo →Refresh the endpoint multiple times within 30 seconds - the timestamp and random number will stay the same!
Error Handling
Error Responses
404 Not Found:
405 Method Not Allowed:
500 Internal Server Error:
Try it:
/demo/api/nonexistent (404) →Implementation Guide
Creating the API Dhandler
The API is implemented in /pages/demo/api/dhandler:
Key Features
- Dynamic Routing: Parse URL paths to extract route parameters
- Method Handling: Support GET, POST, PUT, DELETE
- Query Parameters: Access via
$req.query - Request Body: Access via
$req.body - Status Codes: Use
$res.status(code) - JSON Responses: Set Content-Type and stringify objects
- Error Handling: Try-catch with proper error responses
Accessing Request Data
Setting Response Headers
Working with Sessions and Cache
Testing the API
Using curl
Using JavaScript fetch()
Best Practices
API Development Tips:
- Always set appropriate
Content-Typeheaders - Use proper HTTP status codes (200, 201, 404, 500, etc.)
- Include consistent error response format
- Validate input data before processing
- Use caching for expensive operations
- Document all endpoints and parameters
- Handle edge cases (missing params, invalid IDs, etc.)
- Implement rate limiting for production
- Use authentication/authorization when needed
Try It Yourself
- Click the API links above to see live JSON responses
- Open your browser's Developer Tools (Network tab) to see requests
- Try the curl commands in your terminal
- Modify the dhandler to add your own endpoints
- Experiment with different query parameters