Endpoints
Endpoints are the heart of Azu applications. They define how your application responds to HTTP requests with type safety, validation, and clear separation of concerns.
What are Endpoints?
An endpoint is a type-safe, testable object that handles a specific HTTP route. Each endpoint defines:
HTTP Methods: Which HTTP methods it accepts (GET, POST, PUT, DELETE, etc.)
Route Pattern: The URL pattern it handles
Request Contract: What data it expects to receive
Response Object: What data it returns
Business Logic: How it processes the request
Basic Endpoint Structure
struct UserEndpoint
include Azu::Endpoint(UserRequest, UserResponse)
get "/users/:id"
def call : UserResponse
# Your business logic here
UserResponse.new(find_user(params["id"]))
end
endKey Components
Module Include:
include Azu::Endpoint(RequestType, ResponseType)Route Declaration:
get "/users/:id"Call Method:
def call : ResponseType- the main logic
HTTP Methods
Azu supports all standard HTTP methods:
Multiple Routes
You can handle multiple routes in a single endpoint:
Request Contracts
Request contracts define and validate the data your endpoint expects:
Accessing Request Data
In your endpoint, access validated request data:
Validation
Request contracts automatically validate incoming data:
Response Objects
Response objects structure your endpoint's output:
Response Types
Azu provides several built-in response types:
Route Parameters
Access URL parameters in your endpoints:
Parameter Types
Parameters are automatically converted to the appropriate type:
Query Parameters
Access query string parameters:
Request Context
Access the full HTTP request context:
Error Handling
Handle errors gracefully in your endpoints:
Common Error Responses
Status Codes
Set appropriate HTTP status codes:
Common Status Codes
200 OK: Successful GET, PUT, PATCH
201 Created: Successful POST
204 No Content: Successful DELETE
400 Bad Request: Invalid request
401 Unauthorized: Authentication required
403 Forbidden: Access denied
404 Not Found: Resource not found
422 Unprocessable Entity: Validation errors
500 Internal Server Error: Server error
Content Types
Handle different content types:
Testing Endpoints
Test your endpoints with Crystal's built-in testing framework:
Best Practices
1. Single Responsibility
Each endpoint should have a single, clear responsibility:
2. Type Safety
Always use typed request and response objects:
3. Error Handling
Handle errors gracefully and provide meaningful messages:
4. Validation
Always validate input data:
5. Status Codes
Use appropriate HTTP status codes:
Advanced Patterns
Resource Endpoints
Create RESTful resource endpoints:
Nested Resources
Handle nested resources:
Next Steps
Now that you understand endpoints:
Request Contracts - Master request validation and type safety
Response Objects - Structure your API responses
Routing - Organize your application routes
Middleware - Customize request processing
Testing - Write comprehensive tests for your endpoints
WebSocket Channels - Build real-time features
Endpoints are the foundation of Azu applications. With type safety, validation, and clear separation of concerns, they make your code robust, testable, and maintainable.
Last updated
Was this helpful?
