-
-
Notifications
You must be signed in to change notification settings - Fork 4
HTTP Headers
Learn about the RFC-compliant HTTP headers automatically added to API responses.
Laravel ApiRoute automatically adds version-related headers to all API responses. These headers help clients understand the version they're using and any lifecycle information.
HTTP/1.1 200 OK
X-API-Version: v1
X-API-Version-Status: deprecated
Deprecation: Sun, 01 Jun 2025 00:00:00 GMT
Sunset: Mon, 01 Dec 2025 00:00:00 GMT
Link: </api/v2/users>; rel="successor-version"Enable or disable individual headers:
// config/apiroute.php
'headers' => [
'enabled' => true,
'include' => [
'version' => true, // X-API-Version
'status' => true, // X-API-Version-Status
'deprecation' => true, // Deprecation (RFC 8594)
'sunset' => true, // Sunset (RFC 7231)
'successor_link' => true, // Link rel="successor-version"
],
],Shows the current API version being used.
X-API-Version: v1Always present when headers are enabled.
Shows the lifecycle status of the version.
X-API-Version-Status: activePossible values:
-
active- Current stable version -
beta- Beta/preview version -
deprecated- Deprecated version -
sunset- End-of-life version
Added when a fallback to a different version occurred.
X-API-Version: v2
X-API-Version-Fallback: v1This means the request was for v2, but the route was served from v1.
The Deprecation header indicates when an API version was deprecated.
Deprecation: Sun, 01 Jun 2025 00:00:00 GMTFormat: RFC 7231 HTTP-date format
Reference: RFC 8594 - The Sunset HTTP Header Field
When added: Only for versions marked as deprecated
ApiRoute::version('v1', function () {
// ...
})->deprecated('2025-06-01'); // Adds Deprecation headerThe Sunset header indicates when an API version will be removed.
Sunset: Mon, 01 Dec 2025 00:00:00 GMTFormat: RFC 7231 HTTP-date format
Reference: RFC 7231 - Hypertext Transfer Protocol
When added: Only for versions with a sunset date
ApiRoute::version('v1', function () {
// ...
})->sunset('2025-12-01'); // Adds Sunset headerThe Link header points to the successor version.
Link: </api/v2/users>; rel="successor-version"Reference: RFC 8288 - Web Linking
When added: Only when a successor version is defined
ApiRoute::version('v1', function () {
// ...
})->setSuccessor('v2'); // Adds Link headerFor a deprecated version with all information:
ApiRoute::version('v1', function () {
Route::get('users', [UserController::class, 'index']);
})
->deprecated('2025-06-01')
->sunset('2025-12-01')
->setSuccessor('v2');Response headers:
HTTP/1.1 200 OK
Content-Type: application/json
X-API-Version: v1
X-API-Version-Status: deprecated
Deprecation: Sun, 01 Jun 2025 00:00:00 GMT
Sunset: Mon, 01 Dec 2025 00:00:00 GMT
Link: </api/v2/users>; rel="successor-version"| Status | X-API-Version | X-API-Version-Status | Deprecation | Sunset | Link |
|---|---|---|---|---|---|
| Active | Yes | active |
No | No | No |
| Beta | Yes | beta |
No | No | No |
| Deprecated | Yes | deprecated |
Yes (if date set) | Yes (if date set) | Yes (if successor set) |
| Sunset | Yes | sunset |
Yes | Yes | Yes |
async function fetchAPI(endpoint) {
const response = await fetch(`/api/v1/${endpoint}`);
// Check for deprecation warning
const deprecation = response.headers.get('Deprecation');
if (deprecation) {
console.warn(`API version is deprecated since ${deprecation}`);
}
// Check for sunset date
const sunset = response.headers.get('Sunset');
if (sunset) {
console.warn(`API version will be removed on ${sunset}`);
}
// Check for successor
const link = response.headers.get('Link');
if (link && link.includes('successor-version')) {
console.info(`Upgrade available: ${link}`);
}
return response.json();
}$response = Http::get('https://api.example.com/v1/users');
if ($response->header('Deprecation')) {
Log::warning('Using deprecated API version', [
'deprecation' => $response->header('Deprecation'),
'sunset' => $response->header('Sunset'),
]);
}The VersionHeaders class handles adding headers to responses:
use Grazulex\ApiRoute\Http\Headers\VersionHeaders;
class VersionHeaders
{
public function addToResponse(Response $response, VersionDefinition $version): Response
{
// Adds all configured headers
}
}Headers are added automatically by the ResolveApiVersion middleware.
// config/apiroute.php
'headers' => [
'enabled' => false,
],// config/apiroute.php
'headers' => [
'enabled' => true,
'include' => [
'version' => true,
'status' => true,
'deprecation' => false, // Disable deprecation header
'sunset' => false, // Disable sunset header
'successor_link' => false,
],
],If you need custom header logic, you can extend the middleware:
use Grazulex\ApiRoute\Middleware\ResolveApiVersion;
class CustomApiVersionMiddleware extends ResolveApiVersion
{
public function handle(Request $request, Closure $next): Response
{
$response = parent::handle($request, $next);
// Add custom headers
$response->headers->set('X-Custom-Header', 'value');
return $response;
}
}- Version Lifecycle - Managing deprecation and sunset
- Middleware - How headers are added
- Configuration - All configuration options
Laravel ApiRoute - Complete API versioning lifecycle management for Laravel
Home | Getting Started | Examples | Configuration
Made with ❤️ for the Laravel community