PHP code example of intermedia / ksef-api-v2

1. Go to this page and download the library: Download intermedia/ksef-api-v2 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/ */

    

intermedia / ksef-api-v2 example snippets


declare(strict_types=1);

2;

$sdk = Apiv2\Client::builder()
    ->setSecurity(
        '<YOUR_BEARER_TOKEN_HERE>'
    )
    ->build();



$response = $sdk->auth->getCurrentSessions(
    pageSize: 10
);

if ($response->authenticationListResponse !== null) {
    // handle response
}

declare(strict_types=1);

2\AuthTokenRequest;
use Intermedia\Ksef\Apiv2\Models\Components\{TContextIdentifier, TNip, SubjectIdentifierTypeEnum};

$req = new AuthTokenRequest(
    '20250625-CR-20F5EE4000-DA48AE4124-46',
    TContextIdentifier::fromNip(new TNip('5265877635')),
    SubjectIdentifierTypeEnum::CERTIFICATE_SUBJECT
);

// PEM Signature (private key and public certificate in separate files)
$signedXml = $req->signWithXadesToString('/path/to/private.pem', '/path/to/cert.pem');

// or PKCS#12 (private key and public certificate in one .p12 file)
$signedXml = $req->signWithXadesToString('/path/to/cert.p12', null, 'password_to_p12');

declare(strict_types=1);

2;

$sdk = Apiv2\Client::builder()
    ->setSecurity(
        '<YOUR_BEARER_TOKEN_HERE>'
    )
    ->build();



$response = $sdk->auth->getCurrentSessions(
    pageSize: 10
);

if ($response->authenticationListResponse !== null) {
    // handle response
}

declare(strict_types=1);

2;
use Intermedia\Ksef\Apiv2\Models\Errors;

$sdk = Apiv2\Client::builder()
    ->setSecurity(
        '<YOUR_BEARER_TOKEN_HERE>'
    )
    ->build();

try {
    $response = $sdk->auth->getCurrentSessions(
        pageSize: 10
    );

    if ($response->authenticationListResponse !== null) {
        // handle response
    }
} catch (Errors\ExceptionResponseThrowable $e) {
    // handle $e->$container data
    throw $e;
} catch (Errors\TooManyRequestsResponseExceptionThrowable $e) {
    // handle $e->$container data
    throw $e;
} catch (Errors\APIException $e) {
    // handle default exception
    throw $e;
}

declare(strict_types=1);

2;

$sdk = Apiv2\Client::builder()
    ->setServerIndex(0)
    ->setSecurity(
        '<YOUR_BEARER_TOKEN_HERE>'
    )
    ->build();



$response = $sdk->auth->getCurrentSessions(
    pageSize: 10
);

if ($response->authenticationListResponse !== null) {
    // handle response
}

declare(strict_types=1);

2;

$sdk = Apiv2\Client::builder()
    ->setServerURL('https://ksef-demo.mf.gov.pl/api/v2')
    ->setSecurity(
        '<YOUR_BEARER_TOKEN_HERE>'
    )
    ->build();



$response = $sdk->auth->getCurrentSessions(
    pageSize: 10
);

if ($response->authenticationListResponse !== null) {
    // handle response
}