Installation
composer require jane-php/open-api
(Note: Due to archival, verify compatibility with your PHP/Laravel version—last release was 2018.)
Generate Client
Place your OpenAPI 3.x spec (e.g., api.yaml) in your project. Run:
vendor/bin/jane --spec=api.yaml --output=src/Generated/Client
(Outputs a PSR7/PSR18-compatible client in src/Generated/Client.)
First Use Case
Register the generated client in Laravel’s AppServiceProvider:
public function register()
{
$this->app->singleton('api.client', function () {
return new \Generated\Client\Client(
new \GuzzleHttp\Client(),
'https://api.example.com'
);
});
}
Inject and use it in a controller:
use Generated\Client\Api\UsersApi;
public function fetchUsers(UsersApi $usersApi)
{
$response = $usersApi->getUsers();
return response()->json($response->getBody());
}
Request/Response Handling
Use the generated client’s methods (e.g., getUsers(), createUser()) to interact with the API. Responses are PSR7-compatible (Psr\Http\Message\ResponseInterface):
$response = $usersApi->getUsers(['limit' => 10]);
$data = json_decode($response->getBody(), true);
Authentication Pass credentials via middleware or headers:
$client = new \Generated\Client\Client(
new \GuzzleHttp\Client([
'headers' => ['Authorization' => 'Bearer ' . $token]
]),
'https://api.example.com'
);
Error Handling
Wrap API calls in try-catch blocks to handle exceptions (e.g., Generated\Client\Exception\ApiException):
try {
$usersApi->deleteUser(123);
} catch (\Generated\Client\Exception\ApiException $e) {
report($e);
return back()->withError($e->getMessage());
}
Laravel Integration
public function handle($request, Closure $next)
{
$request->headers->set('X-Custom-Header', 'value');
return $next($request);
}
Testing Mock the generated client in tests:
$mockClient = Mockery::mock(\Generated\Client\Client::class);
$this->app->instance(\Generated\Client\Client::class, $mockClient);
Archived Package
Generated Code Overrides
vendor/bin/jane) overwrites the output directory. Use version control for the Generated/ folder or exclude it from regeneration.OpenAPI 3.x Quirks
openapi-to-swagger tools).$ref with dynamic paths) may require manual adjustments in the generated code.PSR18 Compatibility
use Psr\Http\Client\ClientInterface;
use Nyholm\Psr7\Client;
$psr18Client = new Client(new \GuzzleHttp\Client());
Caching Responses
$cacheKey = 'users_' . $limit;
return Cache::remember($cacheKey, now()->addHours(1), function () use ($usersApi, $limit) {
return $usersApi->getUsers(['limit' => $limit]);
});
Enable Guzzle Debugging Add a debug handler to Guzzle’s middleware stack:
$client = new \GuzzleHttp\Client([
'handler' => \GuzzleHttp\HandlerStack::create(
new \GuzzleHttp\Middleware::tap(function ($request) {
\Log::debug('Request:', [
'url' => (string) $request->getUri(),
'method' => $request->getMethod(),
'headers' => $request->getHeaders(),
'body' => (string) $request->getBody()
]);
})
)
]);
Validate OpenAPI Spec
Use tools like Swagger Editor or spectral to validate your spec before generation.
Generated Client Logs
Enable debug mode in the generated client by setting the DEBUG constant:
define('DEBUG', true);
Custom Middleware Extend the client’s middleware stack to add logging, retries, or auth:
$client = new \Generated\Client\Client(
new \GuzzleHttp\Client([
'middleware' => [
new \GuzzleHttp\Middleware(),
new class {
public function __invoke(
callable $handler,
\Psr\Http\Message\RequestInterface $request
) {
// Custom logic (e.g., add timestamp header)
$request = $request->withHeader('X-Request-Timestamp', now()->toIso8601String());
return $handler($request);
}
}
]
]),
'https://api.example.com'
);
Modify Generated Code
Use template overrides to customize the generated client. Create a templates/ directory with modified .mustache files and pass the --template-dir flag to jane.
Laravel Facades Create a facade for cleaner syntax:
// ApiFacade.php
class ApiFacade extends \Illuminate\Support\Facades\Facade
{
protected static function getFacadeAccessor() { return 'api.client'; }
}
Usage:
$users = Api::users()->getUsers();
How can I help you explore Laravel packages today?