Search by

silarhi / cerfahub-php

guillaume-sainthillier

PHP client of the CerfaHub API: fill the official French CERFA forms in PDF from JSON

v1.0.1 2026-12-02 13:22 UTC

This package is auto-updated.

Last update: 2026-12-03 00:55:04 UTC


README

Latest Stable Version Total Downloads License PHP Version CI Status Coverage

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-client and nyholm/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.