Skip to main content
API endpoints allow you to create custom RESTful routes for your EverShop store. They’re perfect for handling form submissions, webhooks, integrations, and custom business logic.

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 the api 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

Execution order:
  1. authenticate.ts
  2. validateInput.ts
  3. createProduct.ts

Route Configuration

HTTP Methods

Specify which HTTP methods are allowed:

Custom Paths

Override the default path:

Dynamic Parameters

Use URL parameters:
Access in handler:

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

  1. Create a new request
  2. Set the method (GET, POST, PUT, DELETE)
  3. Enter the URL: http://localhost:3000/foos
  4. Add headers: Content-Type: application/json
  5. Add body (for POST/PUT): {"name":"Foo","description":"Description"}
  6. Send the request

Best Practices

Use TypeScript: Always define types for request and response data to catch errors early.
  • 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)

  1. Verify route.json exists and is valid
  2. Check the path matches your request URL
  3. Run npm run build to rebuild
  4. Check extension is enabled in config/default.json

Middleware Not Executing

  1. Verify middleware filename uses bracket notation
  2. Check middleware exports a function
  3. Ensure middleware calls next() when done

Body Parser Not Working

  1. Verify body parser middleware is first in chain
  2. Check Content-Type header is application/json
  3. Ensure body parser is imported correctly

Next Steps

Event Subscribers

React to API events with subscribers

GraphQL Types

Learn about GraphQL API