Custom Routes

Extend your entities with custom route handlers for specific business logic

Custom Routes

While Frey automatically generates CRUD endpoints for your entities, you can extend them with custom routes to handle specific business logic.

Adding Custom Routes

Use the customRoutes property to add custom endpoints to your entity:

This creates a new endpoint: GET /users/stats

Custom Route Structure

Route Handler Context

Custom route handlers receive the same context as CRUD handlers:

Common Use Cases

Search Endpoints

Bulk Operations

File Upload

Authentication & Authorization

Advanced Patterns

Nested Resources

Middleware Integration

Response Transformation

Error Handling

Always handle errors gracefully in custom routes:

Route Ordering

Custom routes are registered in the order they appear in the array. Be careful with route conflicts:

Best Practices

  1. Use descriptive paths: Choose clear, RESTful paths for your custom routes
  2. Handle errors: Always include proper error handling
  3. Validate input: Validate request parameters and body data
  4. Use appropriate HTTP methods: Follow REST conventions
  5. Keep handlers focused: Each custom route should have a single responsibility
  6. Document your routes: Consider adding OpenAPI documentation for custom routes

Next Steps