Install the Bundle
composer require aldaflux/sirene-api-bundle
Enable the bundle in config/bundles.php:
return [
// ...
AldaFlux\SireneApiBundle\AldaFluxSireneApiBundle::class => ['all' => true],
];
Configure Credentials
Add your INPI Sirene API credentials in config/packages/aldaflux_sirene_api.yaml:
aldaflux_sirene_api:
credentials:
sirene_key: '%env(SIRENE_API_KEY)%'
sirene_secret: '%env(SIRENE_API_SECRET)%'
Store secrets in .env:
SIRENE_API_KEY=your_api_key_here
SIRENE_API_SECRET=your_api_secret_here
First Use Case: Fetch a SIREN/SIRET
Inject the SireneApiClient service and call a method (e.g., getCompanyBySiren):
use AldaFlux\SireneApiBundle\Service\SireneApiClient;
public function __construct(private SireneApiClient $sireneApi) {}
public function getCompanyData(string $siren): array
{
return $this->sireneApi->getCompanyBySiren($siren);
}
Company Data Retrieval Use the bundle’s client methods for structured responses:
// Get company by SIREN
$company = $this->sireneApi->getCompanyBySiren('123456789');
// Get company by SIRET
$company = $this->sireneApi->getCompanyBySiret('12345678901234');
Pagination & Advanced Queries
Leverage the searchCompanies method for bulk operations:
$results = $this->sireneApi->searchCompanies(
criteria: ['siren' => '123456789'],
limit: 10,
offset: 0
);
Integration with Symfony Forms Validate SIREN/SIRET inputs using constraints:
use AldaFlux\SireneApiBundle\Validator\Constraints\ValidSiren;
#[Assert\All({
new ValidSiren(),
})]
public array $companyIdentifiers;
Caching Responses Decorate the client to cache API responses (e.g., using Symfony Cache):
$cache = $this->container->get('cache.app');
$decoratedClient = new CachedSireneApiClient(
$this->sireneApi,
$cache,
3600 // TTL in seconds
);
Rate Limiting The Sirene API enforces strict rate limits (e.g., 100 requests/hour). Implement exponential backoff in your client:
try {
$response = $this->sireneApi->getCompanyBySiren($siren);
} catch (RateLimitExceededException $e) {
sleep($e->getRetryAfter());
retry();
}
Deprecated Endpoints The bundle may not cover all Sirene API endpoints. Check the official API docs and extend the client:
// Extend the client to add custom endpoints
class ExtendedSireneApiClient extends SireneApiClient
{
public function getCompanyHistory(string $siren): array
{
return $this->request('GET', "/companies/{$siren}/history");
}
}
Error Handling
The bundle throws SireneApiException for HTTP errors. Catch and log details:
try {
$data = $this->sireneApi->searchCompanies(...);
} catch (SireneApiException $e) {
$this->logger->error('Sirene API Error', [
'status' => $e->getCode(),
'message' => $e->getMessage(),
'siren' => $siren,
]);
}
Environment-Specific Config Use Symfony’s parameter bags to switch credentials per environment:
# config/packages/dev/aldaflux_sirene_api.yaml
aldaflux_sirene_api:
credentials:
sirene_key: '%env(resolve:SIRENE_API_KEY_DEV)%'
Testing
Mock the client in tests using createMock:
$mockClient = $this->createMock(SireneApiClient::class);
$mockClient->method('getCompanyBySiren')
->willReturn(['siren' => '123456789', 'name' => 'Test Company']);
$this->container->set(SireneApiClient::class, $mockClient);
Logging API Calls Decorate the client to log requests/responses:
class LoggingSireneApiClient implements SireneApiClientInterface
{
public function __construct(
private SireneApiClient $decorated,
private LoggerInterface $logger
) {}
public function getCompanyBySiren(string $siren): array
{
$this->logger->info('Fetching SIREN data', ['siren' => $siren]);
$response = $this->decorated->getCompanyBySiren($siren);
$this->logger->debug('Response', $response);
return $response;
}
}
Webhook Integration Use the bundle to validate SIREN/SIRET data in webhook payloads:
public function handleWebhook(array $payload): void
{
$siren = $payload['siren'];
$this->sireneApi->validateSiren($siren); // Throws if invalid
// Process payload...
}
How can I help you explore Laravel packages today?