Overview
EverShop uses a file-based routing system where:- Directory name = endpoint path
- route.json = route configuration (methods, access)
- Middleware files = request handlers with execution order
Directory Structure
API endpoints are created in theapi folder of your extension:
Creating Your First API Endpoint
Middleware Execution Order
Middleware execution is controlled by filename prefixes in square brackets:Example Middleware Chain
authenticate.tsvalidateInput.tscreateProduct.ts
Route Configuration
HTTP Methods
Specify which HTTP methods are allowed:Custom Paths
Override the default path:Dynamic Parameters
Use URL parameters:Real-World Examples
GET Endpoint
Fetch data from the database:src/api/getFoos/getFoos.ts
src/api/getFoos/route.json
POST with Validation
Create a resource with input validation:src/api/createFoo/validateInput.ts
src/api/createFoo/[bodyParser][validateInput]createFoo.ts
PUT/PATCH Endpoint
Update a resource:src/api/updateFoo/[bodyParser]updateFoo.ts
src/api/updateFoo/route.json
DELETE Endpoint
Delete a resource:src/api/deleteFoo/deleteFoo.ts
src/api/deleteFoo/route.json
Authentication & Authorization
Private Endpoints
Endpoints with"access": "private" require authentication:
Custom Authentication
Create an authentication middleware:src/api/protected/authenticate.ts
Error Handling
Standard Error Response
Validation Errors
Testing API Endpoints
Using cURL
Using Postman
- Create a new request
- Set the method (GET, POST, PUT, DELETE)
- Enter the URL:
http://localhost:3000/foos - Add headers:
Content-Type: application/json - Add body (for POST/PUT):
{"name":"Foo","description":"Description"} - Send the request
Best Practices
- Validate Input - Always validate and sanitize user input
- Error Handling - Use try-catch blocks and return meaningful error messages
- HTTP Status Codes - Use appropriate status codes (200, 201, 400, 401, 404, 500)
- Consistent Response Format - Return consistent JSON structure
- Security - Never expose sensitive data or internal errors
- Logging - Log errors for debugging but don’t expose details to clients
Common Response Formats
Success Response
Error Response
Validation Error Response
Troubleshooting
Endpoint Not Found (404)
- Verify
route.jsonexists and is valid - Check the
pathmatches your request URL - Run
npm run buildto rebuild - Check extension is enabled in
config/default.json
Middleware Not Executing
- Verify middleware filename uses bracket notation
- Check middleware exports a function
- Ensure middleware calls
next()when done
Body Parser Not Working
- Verify body parser middleware is first in chain
- Check Content-Type header is
application/json - Ensure body parser is imported correctly
Next Steps
Event Subscribers
React to API events with subscribers
GraphQL Types
Learn about GraphQL API