PHP code example of apirelio / slim

1. Go to this page and download the library: Download apirelio/slim library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

apirelio / slim example snippets


use Apirelio\Core\Data\ApirelioApplication;
use Apirelio\Core\Data\ApirelioCustomer;
use Apirelio\Slim\ApirelioMiddleware;
use Apirelio\Slim\Config;

$apirelio = new ApirelioMiddleware(
    new Config(
        apiKey: $_ENV['APIRELIO_API_KEY'],
        endpoint: $_ENV['APIRELIO_ENDPOINT'] ?? 'https://apirelio.com',
        service: 'billing-api',
        environment: $_ENV['APP_ENV'] ?? 'production',
        release: $_ENV['APP_RELEASE'] ?? null,
        paths: ['/api/*'],
        bufferPath: __DIR__.'/../var/apirelio/events.ndjson',
    ),
    customerResolver: static fn ($request) => new ApirelioCustomer(
        id: (string) $request->getAttribute('account')->id,
        name: $request->getAttribute('account')->name,
    ),
    applicationResolver: static fn ($request) => new ApirelioApplication(
        id: (string) $request->getAttribute('api_client')->id,
    ),
);

// Slim middleware is LIFO. This order lets Apirelio see the resolved route
// while the error middleware remains the outermost exception boundary.
$app->add($apirelio);
$app->addRoutingMiddleware();
$app->addErrorMiddleware(false, true, true);

use Apirelio\Slim\RequestContext;

$app->post('/api/invoices', function ($request, $response) {
    /** @var RequestContext|null $context */
    $context = $request->getAttribute(RequestContext::ATTRIBUTE);
    $context?->addMetadata(['region' => 'eu-central']);
    $context?->setErrorCode('PAYMENT_REQUIRED');

    return $response;
});