Introduction
EverShop’s REST API allows you to create custom endpoints for your e-commerce application. The API follows a modular architecture where endpoints are defined within extensions using a declarative routing system.API Architecture
REST endpoints in EverShop are organized within extensions and follow a specific directory structure:Key Components
Route Configuration
Declarative JSON file defining HTTP methods, path, and access control
Middleware Chain
Optional middleware functions that run before the main endpoint handler
Endpoint Handler
Main function that processes the request and returns a response
TypeScript Support
Full TypeScript support with EverShop request/response types
Route Configuration
Every endpoint requires aroute.json file that defines its routing configuration:
route.json
Configuration Fields
string[]
required
Array of allowed HTTP methods. Supported values:
GET, POST, PUT, PATCH, DELETEstring
required
The URL path where the endpoint will be accessible. Can include path parameters.
string
required
Access control level for the endpoint.
public- Accessible without authenticationprivate- Requires authentication
Request/Response Types
EverShop provides TypeScript types for handling HTTP requests and responses:EvershopRequest
Extends Express.js Request with additional properties:object
Parsed request body (requires bodyParser middleware)
object
URL path parameters
object
URL query string parameters
object
HTTP request headers
EvershopResponse
Extends Express.js Response with standard methods:function
Sets the HTTP status code
function
Sends a JSON response
function
Sends a generic response
Middleware Pattern
Middleware functions run before the main endpoint handler. They are named using the pattern[middlewareName]endpoint.ts:
bodyParser.ts
[bodyParser]createFoo.ts
Middleware Execution Order
Middleware files are executed in alphabetical order based on the middleware name in square brackets:[auth]endpoint.ts- Authentication[bodyParser]endpoint.ts- Body parsing[validation]endpoint.ts- Validationendpoint.ts- Main handler
Response Conventions
Success Response
For successful operations, return a structured JSON response:Error Response
For errors, include descriptive error messages:HTTP Status Codes
Follow standard HTTP status code conventions:Best Practices
Use TypeScript
Use TypeScript
Always use TypeScript for type safety and better IDE support:
Validate Input
Validate Input
Always validate request data before processing:
Use Appropriate Status Codes
Use Appropriate Status Codes
Return the correct HTTP status code for each scenario:
- 201 for resource creation
- 400 for validation errors
- 404 for not found
Structure Responses Consistently
Structure Responses Consistently
Use a consistent response format across all endpoints:
Separate Middleware Logic
Separate Middleware Logic
Keep concerns separated by using dedicated middleware files:
- Authentication in
[auth]endpoint.ts - Validation in
[validation]endpoint.ts - Body parsing in
[bodyParser]endpoint.ts
Example: Complete Endpoint
Here’s a complete example of creating a REST endpoint:1
Create Directory Structure
2
Create route.json
route.json
3
Create Middleware (optional)
bodyParser.ts
4
Create Endpoint Handler
[bodyParser]createFoo.ts
5
Test the Endpoint
Next Steps
View Endpoints
Browse available REST endpoints in this application
GraphQL API
Learn about the GraphQL API as an alternative
Creating Extensions
Learn how to create custom extensions
API Endpoints Guide
Step-by-step guide to creating API endpoints