Define API contracts in your Laravel application and generate TypeScript types, API clients, SDKs, OpenAPI specs, Postman collections, and more.
yeab/laravel-api-contract is a Laravel package for define api contracts in your laravel application and generate typescript types, api clients, sdks, openapi specs, postman collections, and more..
It currently has 0 GitHub stars and 3 downloads on Packagist (latest version v1.1.0).
Install it with composer require yeab/laravel-api-contract.
Discover more Laravel packages by yeab
or browse all Laravel packages to compare alternatives.
Last updated
The ultimate "Write once. Generate everything." toolkit for Laravel APIs.
Laravel API Contract is an advanced, fully automated architecture utility for Laravel applications. It statically analyzes your existing Laravel routes, controllers, Form Requests, and API Resources to automatically generate a comprehensive API Contract.
From this single source of truth, it instantly generates Swagger documentation, TypeScript interfaces, typed API clients, Postman collections, Markdown docs, PHPUnit feature tests, and backwards-compatibility reports.
"Write once. Generate everything."
You shouldn't have to write your validation rules in a Form Request, duplicate them in OpenAPI annotations, and then write them again as TypeScript interfaces for your frontend. Laravel API Contract eliminates this friction. Write clean, standard Laravel code, and let the package do the rest.
Building APIs usually means maintaining parallel systems. When you update an endpoint in your controller, you also have to update your Postman collection, notify the frontend team to update their types, fix the Swagger annotations, and write the tests.
This causes friction, drift, and broken integrations. We built this package to ensure that your Laravel code is the only source of truth.
rules() to determine expected input payloads and validation states.toArray() methods to determine the exact shape of your JSON responses.ApiContract JSON representation.ApiContract to emit various artifacts.Once your application is analyzed, you can generate:
Require the package via Composer. It is recommended to install it as a dev dependency unless you are generating contracts in production:
composer require yab/laravel-api-contract --dev
Optionally, publish the configuration file:
php artisan vendor:publish --provider="Yab\LaravelApiContract\Providers\LaravelApiContractServiceProvider" --tag="config"
To instantly analyze your API and build the core contract file (usually written to storage/api-contract.json):
php artisan api-contract:build
Now, you can generate everything else from it!
# Generate Swagger Docs
php artisan api-contract:swagger --path=public/swagger.json
# Generate TypeScript Interfaces
php artisan api-contract:typescript --output=resources/ts/types/api.ts
# Generate an API Client
php artisan api-contract:client --output=resources/ts/services/
# Scaffold PHPUnit Tests
php artisan api-contract:tests --output=tests/Feature/Api/
The package works automatically by inspecting standard Laravel conventions.
// routes/api.php
Route::post('/users', [UserController::class, 'store'])->name('users.store');
// app/Http/Controllers/UserController.php
public function store(StoreUserRequest $request)
{
$user = User::create($request->validated());
return new UserResource($user);
}
// app/Http/Requests/StoreUserRequest.php
public function rules()
{
return [
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'email', 'unique:users'],
];
}
// app/Http/Resources/UserResource.php
public function toArray($request)
{
return [
'id' => $this->id,
'name' => $this->name,
'email' => $this->email,
];
}
Run php artisan api-contract:build, and the package will figure out exactly what /users accepts and what it returns!
| Command | Description |
|---|---|
| api-contract:build | Analyze the application and build the central api-contract.json file. |
| api-contract:swagger | Generate an OpenAPI/Swagger v3.0 JSON specification. |
| api-contract:typescript | Generate TypeScript interface definition files. |
| api-contract:client | Generate a fully-typed TypeScript API client service. |
| api-contract:postman | Generate a Postman v2.1 Collection JSON file. |
| api-contract:docs | Generate human-readable Markdown documentation. |
| api-contract:tests | Generate boilerplate PHPUnit feature tests. |
| api-contract:compare | Compare two contract files and output a breaking-change report. |
For deep dives into configuration and advanced generator options, please refer to the documentation:
We welcome contributions! Please see CONTRIBUTING.md for details.
If you discover any security-related issues, please email [email protected] instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.