silarhi / cerfahub-php
PHP client of the CerfaHub API: fill the official French CERFA forms in PDF from JSON
Requires
- php: >=8.2
- php-http/discovery: ^1.20.0
- psr/http-client: ^1.0.3
- psr/http-client-implementation: ^1.0
- psr/http-factory: ^1.1
- psr/http-factory-implementation: ^1.0
- psr/http-message: ^1.1 || ^2.0
- psr/simple-cache: ^2.0 || ^3.0
Requires (Dev)
- ergebnis/composer-normalize: ^2.59.0
- friendsofphp/php-cs-fixer: ^3.95.32
- nyholm/psr7: ^1.8.3
- php-http/mock-client: ^1.6.1
- phpstan/extension-installer: ^1.4.3
- phpstan/phpstan: ^2.3.7
- phpstan/phpstan-strict-rules: ^2.1.2
- phpunit/phpunit: ^11.5.57 || ^12.5.38 || ^13.4.1
- rector/rector: ^2.9.2
- symfony/cache: ^6.4 || ^7.0 || ^8.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-12-03 00:55:04 UTC
README
CerfaHub PHP SDK
Fill the official French CERFA forms in PDF from JSON, with the CerfaHub API.
Send the data of a form, get back the official PDF, filled in.
đź“– API documentation
Features
- OAuth2 handled - the client requests the client credentials token, keeps it and renews it, in memory or in your PSR-16 cache.
- Every endpoint - list the forms, read a form, its versions and its JSON schema, validate data, generate the PDF.
- One exception per API error - a validation error gives each faulty field, a rate limit the seconds to wait.
- Your HTTP client - any PSR-18 client, discovered automatically (Symfony HttpClient, Guzzle...).
- Test keys - a
test_key generates SPÉCIMEN PDFs that do not count towards your plan, with no other change.
Requirements
- PHP 8.2+
- A PSR-18 HTTP client and a PSR-17 factory, for example
symfony/http-clientandnyholm/psr7
Installation
composer require silarhi/cerfahub-php
If your project has no PSR-18 client yet, add one:
composer require symfony/http-client nyholm/psr7
Quick start
Create an API key from your CerfaHub dashboard, then:
use Silarhi\CerfaHub\Client; $cerfahub = new Client('YOUR_CLIENT_ID', 'YOUR_CLIENT_SECRET'); $pdf = $cerfahub->generate('15776', '02', [ 'vehicule' => ['immatriculation' => 'FG-482-KL'], 'vendeur' => ['nom' => 'MARTIN Jean-Pierre'], 'acheteur' => ['nom' => 'DUBOIS Claire'], 'signatureVendeur' => ['lieu' => 'Toulouse', 'date' => '2026-10-28'], ]); $pdf->save('cession.pdf');
The fields of each form, their types and their constraints are in its schema (schema()) and in the API tab of its
page on cerfahub.fr. Field names are in French, as on the forms themselves.
The token in a web application
A token is valid for one hour. The client keeps it in memory, which is enough for a script or a worker. In a web
application, each PHP request starts from scratch: without a cache, each one would request a new token and end up
blocked by the rate limit of /oauth/token. Give the client a PSR-16 cache, your framework's:
use Symfony\Component\Cache\Adapter\FilesystemAdapter; use Symfony\Component\Cache\Psr16Cache; $cerfahub = new Client('YOUR_CLIENT_ID', 'YOUR_CLIENT_SECRET', cache: new Psr16Cache(new FilesystemAdapter()));
With Laravel, Cache::store() already is a PSR-16 cache. The token is stored under a key specific to the API key and
the API URL.
Methods
| Method | Returns |
|---|---|
cerfas() |
the list of available CERFA forms |
cerfa($numero, $version = 'latest') |
a CERFA form |
versions($numero) |
its versions (millésimes) |
schema($numero, $version = 'latest') |
its JSON schema, under the schema key |
validate($numero, $version, $data) |
['valid' => true, 'cerfa' => ..., 'version' => ...], with no PDF and no billing |
generate($numero, $version, $data, storeDocument: true) |
a GeneratedPdf |
$version is latest or a two-digit number (02). latest follows the newest version: in production, pin it, and
move to the next one once you have tested it.
GeneratedPdf has content (the bytes of the PDF), save($path), documentId (when the environment keeps its
documents in Mes documents), requestId (to give to support), and deprecation / sunset when a newer version
exists or this one is going to be retired. storeDocument: false does not keep this document in Mes documents.
Errors
Every exception implements Silarhi\CerfaHub\Exception\CerfaHubException. Those of the API extend ApiException,
which carries getStatus(), getTitle(), getDetail(), getRequestId() and getProblem().
| Exception | Status | When |
|---|---|---|
ValidationException |
422 | the data does not match the schema: violations() |
PaymentRequiredException |
402 | the generations of the month are used up, or an invoice is due |
RateLimitException |
429 | too many calls: wait getRetryAfter() seconds |
AuthenticationException |
401, 403 | key refused, or missing scope |
NotFoundException |
404 | unknown CERFA or version |
NotAvailableException |
409 | version not available for generation yet |
GoneException |
410 | retired version |
ApiException |
other | 400, 406, 413, 5xx |
TransportException |
the API could not be reached | |
UnexpectedResponseException |
an answer that is not the API's (proxy, wrong URL) |
A 422 error gives each faulty field, so that you can show it under the right field of your form:
use Silarhi\CerfaHub\Exception\ValidationException; try { $pdf = $cerfahub->generate('15776', '02', $data); } catch (ValidationException $exception) { foreach ($exception->violations() as $violation) { // $violation->propertyPath: "/vendeur/nom" (JSON Pointer), $violation->message, $violation->code } }
The client does not retry on its own after a 429: it is up to you to wait or to queue the call.
Contributing
See CONTRIBUTING.md.
License
MIT License. See LICENSE for details.
Built with care by SILARHI.
If the CerfaHub PHP SDK saves you time, consider giving it a star on GitHub.