Creating a Response
Every response class must extendAbstractApivalkResponse and implement:
getStatusCode(): Returns the HTTP status code (e.g., 200, 201, 404).getDocumentation(): Defines the schema for OpenAPI generation.toArray(): Returns the data to be rendered (usually as JSON).
Example
Built-in Error Responses
Apivalk provides several pre-defined error responses for common scenarios:BadRequestApivalkResponse(400)UnauthorizedApivalkResponse(401)ForbiddenApivalkResponse(403)NotFoundApivalkResponse(404)MethodNotAllowedApivalkResponse(405)TooManyRequestsApivalkResponse(429)InternalServerErrorApivalkResponse(500)BadValidationApivalkResponse(422) - Used automatically by theRequestValidationMiddleware.
Headers
You can add custom headers to any response object:Automatic Rate Limit Headers
When the Rate Limit Middleware is active, Apivalk automatically appends rate limit information to the response headers. This ensures that clients are always aware of their current quota and remaining requests. The following headers are automatically added:X-RateLimit-Limit: The maximum number of requests allowed in the current window.X-RateLimit-Remaining: The number of requests left for the current window.X-RateLimit-Reset: The time at which the current rate limit window resets (UTC timestamp).
Retry-After header is also included.
Rendering
TheJsonRenderer is the default renderer. It takes the array from toArray() and converts it to a JSON string, handling headers and status codes automatically.