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
- Use descriptive paths: Choose clear, RESTful paths for your custom routes
- Handle errors: Always include proper error handling
- Validate input: Validate request parameters and body data
- Use appropriate HTTP methods: Follow REST conventions
- Keep handlers focused: Each custom route should have a single responsibility
- Document your routes: Consider adding OpenAPI documentation for custom routes
Next Steps
- Learn about Parameter Handling for advanced querying
- Explore Type Safety for better development experience
- Check out Examples for more real-world use cases