PHPackages                             jeromejhipolito/laravel-api-versioning - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. jeromejhipolito/laravel-api-versioning

ActiveLibrary[API Development](/categories/api)

jeromejhipolito/laravel-api-versioning
======================================

Header-based API versioning with version flags for Laravel. Supports semantic versioning and feature flag-like version control.

v1.1.0(3mo ago)2120↓50%MITPHPPHP ^8.2

Since Jan 20Pushed 3mo agoCompare

[ Source](https://github.com/jeromejhipolito/laravel-api-versioning)[ Packagist](https://packagist.org/packages/jeromejhipolito/laravel-api-versioning)[ RSS](/packages/jeromejhipolito-laravel-api-versioning/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (2)Dependencies (6)Versions (3)Used By (0)

Laravel API Versioning
======================

[](#laravel-api-versioning)

[![Latest Version on Packagist](https://camo.githubusercontent.com/1d01226ba29be75e5325a474a9a997be9aadf0aa518424f33c35355d6150e3ed/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6a65726f6d656a6869706f6c69746f2f6c61726176656c2d6170692d76657273696f6e696e672e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/jeromejhipolito/laravel-api-versioning)[![License](https://camo.githubusercontent.com/7d7465fbc6906dcacad93c0049b901838dc49d30d66db9067b3d6076e4d5bc78/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f6a65726f6d656a6869706f6c69746f2f6c61726176656c2d6170692d76657273696f6e696e672e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/jeromejhipolito/laravel-api-versioning)

Header-based API versioning with version flags for Laravel. Supports semantic versioning and feature flag-like version control.

Features
--------

[](#features)

- 🏷️ **Header-based versioning** - Uses `X-API-Version` header (industry standard like Stripe, GitHub)
- 📦 **Semantic versioning** - Full support for major.minor.patch format (1.0.0, 1.1.0, 2.0.0)
- 🚩 **Version flags** - Enable/disable versions independently (perfect for app store review periods)
- 🔒 **Minimum version middleware** - Require minimum version per route: `min.version:1.1.0`
- 🔄 **Controller inheritance** - Override only changed methods in versioned controllers
- 📱 **Mobile-friendly** - Disabled versions return `version_enabled: false` so apps can hide features
- 🌍 **Translations** - Built-in support for English, Japanese, and Korean

Installation
------------

[](#installation)

```
composer require jeromejhipolito/laravel-api-versioning
```

Publish Configuration (Optional)
--------------------------------

[](#publish-configuration-optional)

```
php artisan vendor:publish --tag=api-versioning-config
php artisan vendor:publish --tag=api-versioning-lang
```

Configuration
-------------

[](#configuration)

Add to your `.env`:

```
# Optional: Comma-separated list of enabled versions (null = all enabled)
API_ENABLED_VERSIONS=1.0.0,1.1.0

# Optional: Default version when header is missing
API_DEFAULT_VERSION=1.0.0
```

Edit `config/api-versioning.php`:

```
return [
    'supported_versions' => ['1.0.0', '1.1.0', '2.0.0'],
    'enabled_versions' => env('API_ENABLED_VERSIONS')
        ? array_map('trim', explode(',', env('API_ENABLED_VERSIONS')))
        : null, // null = all supported versions enabled
    'default_version' => env('API_DEFAULT_VERSION', '1.0.0'),
];
```

Register Middleware
-------------------

[](#register-middleware)

In `bootstrap/app.php`:

```
use JeromeJHipolito\ApiVersioning\Middleware\ApiVersionMiddleware;
use JeromeJHipolito\ApiVersioning\Middleware\ResolveVersionedController;

->withMiddleware(function (Middleware $middleware) {
    $middleware->api(append: [
        ApiVersionMiddleware::class,
        ResolveVersionedController::class,
    ]);
})
```

Usage
-----

[](#usage)

### Making Requests

[](#making-requests)

```
# With version header
curl -H "X-API-Version: 1.0.0" https://api.example.com/users

# Without header (uses default version)
curl https://api.example.com/users
```

### Response Headers

[](#response-headers)

Every response includes:

- `X-API-Version: 1.0.0`
- `X-API-Version-Enabled: true`
- `X-API-Supported-Versions: 1.0.0, 1.1.0, 2.0.0`
- `X-API-Enabled-Versions: 1.0.0, 1.1.0`

### Check Version Status

[](#check-version-status)

```
GET /api/version/status
```

```
{
  "status": "success",
  "data": {
    "current_version": "1.0.0",
    "version_enabled": true,
    "default_version": "1.0.0",
    "supported_versions": ["1.0.0", "1.1.0", "2.0.0"],
    "enabled_versions": ["1.0.0", "1.1.0"],
    "version_flags": {
      "1.0.0": true,
      "1.1.0": true,
      "2.0.0": false
    }
  }
}
```

### Using in Controllers

[](#using-in-controllers)

```
use JeromeJHipolito\ApiVersioning\Traits\VersionAwareTrait;

class UserController extends Controller
{
    use VersionAwareTrait;

    public function show($id)
    {
        $user = User::find($id);

        // Check version
        if ($this->isVersionAtLeast('2.0.0')) {
            return new V2\UserResource($user);
        }

        return new UserResource($user);
    }
}
```

### Using in Resources

[](#using-in-resources)

```
use JeromeJHipolito\ApiVersioning\Traits\VersionAwareResourceTrait;

class UserResource extends JsonResource
{
    use VersionAwareResourceTrait;

    public function toArray($request): array
    {
        return [
            'id' => $this->id,
            'name' => $this->name,

            // Only in 1.1.0+
            ...$this->mergeWhenVersion('1.1.0', [
                'profile_score' => $this->profile_score,
            ]),

            // Only below 2.0.0 (deprecated)
            ...$this->mergeWhenVersionBelow('2.0.0', [
                'legacy_field' => $this->old_data,
            ]),
        ];
    }
}
```

### Disabled Version Response

[](#disabled-version-response)

When a version is supported but not enabled:

```
{
  "status": "success",
  "version_enabled": false,
  "message": "This API version is currently disabled...",
  "current_version": "2.0.0",
  "data": null
}
```

This allows mobile apps to check `version_enabled` and hide features accordingly.

Version Flags Workflow
----------------------

[](#version-flags-workflow)

1. **Add new version as supported but disabled**

    ```
    'supported_versions' => ['1.0.0', '2.0.0'],
    ```

    ```
    API_ENABLED_VERSIONS=1.0.0
    ```
2. **Deploy to production** - Old apps continue working
3. **Submit new app version** - App checks `version_flags` and hides 2.0.0 features
4. **After app store approval** - Enable the version

    ```
    API_ENABLED_VERSIONS=1.0.0,2.0.0
    ```

Minimum Version Middleware
--------------------------

[](#minimum-version-middleware)

Require a minimum API version for specific routes or groups:

### Register the Middleware Alias

[](#register-the-middleware-alias)

In `bootstrap/app.php`:

```
use JeromeJHipolito\ApiVersioning\Middleware\MinimumVersionMiddleware;

->withMiddleware(function (Middleware $middleware) {
    $middleware->alias([
        'min.version' => MinimumVersionMiddleware::class,
    ]);
})
```

### Usage

[](#usage-1)

```
// Single route
Route::post('new-feature', [FeatureController::class, 'store'])
    ->middleware('min.version:1.1.0');

// Route group
Route::group(['middleware' => ['min.version:2.0.0']], function () {
    Route::post('advanced', [AdvancedController::class, 'store']);
    Route::delete('advanced/{id}', [AdvancedController::class, 'destroy']);
});
```

### Response When Version Is Too Low

[](#response-when-version-is-too-low)

```
{
    "message": "This endpoint requires API version 1.1.0 or higher",
    "current_version": "1.0.0",
    "minimum_version": "1.1.0"
}
```

HTTP Status: `400 Bad Request`

Available Methods
-----------------

[](#available-methods)

### VersionAwareTrait (Controllers)

[](#versionawaretrait-controllers)

MethodDescription`getApiVersion()`Get current version string`getApiMajorVersion()`Get major version number`getApiMinorVersion()`Get minor version number`getApiPatchVersion()`Get patch version number`isVersionAtLeast($v)`Check if &gt;= version`isVersionBelow($v)`Check if &lt; version`isVersionExactly($v)`Check if exact version`isVersionBetween($min, $max)`Check if in range### VersionAwareResourceTrait (Resources)

[](#versionawareresourcetrait-resources)

MethodDescription`mergeWhenVersion($v, $array)`Merge array if &gt;= version`mergeWhenVersionBelow($v, $array)`Merge array if &lt; version`mergeWhenVersionExactly($v, $array)`Merge array if exact version`mergeWhenVersionBetween($min, $max, $array)`Merge if in range`whenVersion($v, $value, $default)`Return value if &gt;= version`whenVersionBelow($v, $value, $default)`Return value if &lt; versionLicense
-------

[](#license)

MIT License. See [LICENSE](LICENSE) for details.

###  Health Score

40

—

FairBetter than 88% of packages

Maintenance79

Regular maintenance activity

Popularity15

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity48

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~1 days

Total

2

Last Release

110d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/0306a31e9423235a83c0093c7caaa9725e76e8796396ed3bc51225dd0cdaa4f1?d=identicon)[jeromejhipolito](/maintainers/jeromejhipolito)

---

Top Contributors

[![jeromejhipolito](https://avatars.githubusercontent.com/u/180266815?v=4)](https://github.com/jeromejhipolito "jeromejhipolito (6 commits)")

---

Tags

apilaravelheadersemverversioningversionfeature-flags

###  Code Quality

TestsPest

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/jeromejhipolito-laravel-api-versioning/health.svg)

```
[![Health](https://phpackages.com/badges/jeromejhipolito-laravel-api-versioning/health.svg)](https://phpackages.com/packages/jeromejhipolito-laravel-api-versioning)
```

###  Alternatives

[andreaselia/laravel-api-to-postman

Generate a Postman collection automatically from your Laravel API

1.0k586.2k3](/packages/andreaselia-laravel-api-to-postman)[api-ecosystem-for-laravel/dingo-api

A RESTful API package for the Laravel and Lumen frameworks.

3121.5M10](/packages/api-ecosystem-for-laravel-dingo-api)[essa/api-tool-kit

set of tools to build an api with laravel

52680.5k](/packages/essa-api-tool-kit)[resend/resend-laravel

Resend for Laravel

1191.4M6](/packages/resend-resend-laravel)[grazulex/laravel-apiroute

Complete API versioning lifecycle management for Laravel

1036.0k](/packages/grazulex-laravel-apiroute)[shahghasiadil/laravel-api-versioning

Elegant attribute-based API versioning solution for Laravel applications with built-in deprecation management and version inheritance

2913.6k](/packages/shahghasiadil-laravel-api-versioning)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
