Skip to main content

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 a route.json file that defines its routing configuration:
route.json

Configuration Fields

string[]
required
Array of allowed HTTP methods. Supported values: GET, POST, PUT, PATCH, DELETE
string
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 authentication
  • private - 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:
  1. [auth]endpoint.ts - Authentication
  2. [bodyParser]endpoint.ts - Body parsing
  3. [validation]endpoint.ts - Validation
  4. endpoint.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

Always use TypeScript for type safety and better IDE support:
Always validate request data before processing:
Return the correct HTTP status code for each scenario:
  • 201 for resource creation
  • 400 for validation errors
  • 404 for not found
Use a consistent response format across all endpoints:
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