intermedia / ksef-api-v2
Requires
- php: >=8.1
- ext-dom: *
- ext-openssl: *
- brick/date-time: >=0.7.0
- brick/math: >=0.12.1
- galbar/jsonpath: >=3.0
- guzzlehttp/guzzle: ^7.0
- phpdocumentor/type-resolver: >=1.8
- phpseclib/phpseclib: ^3.0
- robrichards/xmlseclibs: ^3.1
- speakeasy/serializer: ^4.0.3
Requires (Dev)
- laravel/pint: >=1.21.2
- phpstan/phpstan: >=2.1.0
- phpunit/phpunit: >=10
- roave/security-advisories: dev-latest
README
Developer-friendly & type-safe Php SDK specifically catered to leverage intermedia/ksef-api-v2 API.
Summary
KSeF API TE: Wersja API: 2.0.0 (build 2.0.0-rc4-te-20250827.1+d4adf52dbfb92d635d0069cba24a52e1e3d67e03)
Klucze publiczne Ministerstwa Finansów (dla danego środowiska): Pobierz klucze
Historia zmian: Changelog
Rozszerzona dokumentacja API: ksef-docs
Table of Contents
SDK Installation
The SDK relies on Composer to manage its dependencies.
To install the SDK and add it as a dependency to an existing composer.json
file:
composer require "intermedia/ksef-api-v2"
SDK Example Usage
Example
declare(strict_types=1); require 'vendor/autoload.php'; use Intermedia\Ksef\Apiv2; $sdk = Apiv2\Client::builder() ->setSecurity( '<YOUR_BEARER_TOKEN_HERE>' ) ->build(); $response = $sdk->auth->getCurrentSessions( pageSize: 10 ); if ($response->authenticationListResponse !== null) { // handle response }
AuthTokenRequest XML generation with XAdES signature example
declare(strict_types=1); require 'vendor/autoload.php'; use Intermedia\Ksef\Apiv2\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');
Authentication
Per-Client Security Schemes
This SDK supports the following security scheme globally:
Name | Type | Scheme |
---|---|---|
bearer |
http | HTTP Bearer |
To authenticate with the API the bearer
parameter must be set when initializing the SDK. For example:
declare(strict_types=1); require 'vendor/autoload.php'; use Intermedia\Ksef\Apiv2; $sdk = Apiv2\Client::builder() ->setSecurity( '<YOUR_BEARER_TOKEN_HERE>' ) ->build(); $response = $sdk->auth->getCurrentSessions( pageSize: 10 ); if ($response->authenticationListResponse !== null) { // handle response }
Available Resources and Operations
Available methods
auth
- getCurrentSessions - Pobranie listy aktywnych sesji
- revokeCurrentSession - Unieważnienie aktualnej sesji uwierzytelnienia
- revokeSession - Unieważnienie sesji uwierzytelnienia
- challenge - Inicjalizacja uwierzytelnienia
- withXades - Uwierzytelnienie z wykorzystaniem podpisu XAdES
- withKsefToken - Uwierzytelnienie z wykorzystaniem tokena KSeF
- getStatus - Pobranie statusu uwierzytelniania
- redeemToken - Pobranie tokenów dostępowych
- refreshToken - Odświeżenie tokena dostępowego
certificates
- getLimits - Pobranie danych o limitach certyfikatów
- getEnrollmentData - Pobranie danych do wniosku certyfikacyjnego
- processEnrollment - Wysyłka wniosku certyfikacyjnego
- getEnrollmentStatus - Pobranie statusu przetwarzania wniosku certyfikacyjnego
- retrieve - Pobranie certyfikatu lub listy certyfikatów
- revoke - Unieważnienie certyfikatu
- getList - Pobranie listy metadanych certyfikatów
getInvoices
- getByKsefNumber - Pobranie faktury po numerze KSeF
- getList - Pobranie listy metadanych faktur
- export - [mock] Eksport paczki faktur
- getExportStatus - [mock] Pobranie statusu eksportu paczki faktur
invoices
- getList - Pobranie faktur sesji
- getInvoiceStatus - Pobranie statusu faktury z sesji
- getFailed - Pobranie niepoprawnie przetworzonych faktur sesji
- getUpoByKsefNumber - Pobranie UPO faktury z sesji na podstawie numeru KSeF
- getUpo - Pobranie UPO faktury z sesji na podstawie numeru referencyjnego faktury
- sendOnline - Wysłanie faktury
permissions
- grantToPersons - Nadanie osobom fizycznym uprawnień do pracy w KSeF
- grantToEntities - Nadanie podmiotom uprawnień do obsługi faktur
- grantAuthorizations - Nadanie uprawnień podmiotowych
- grantIndirectly - Nadanie uprawnień w sposób pośredni
- grantToSubunits - Nadanie uprawnień administratora podmiotu podrzędnego
- grantToEuEntitiesAdminRights - Nadanie uprawnień administratora podmiotu unijnego
- grantToEuEntities - Nadanie uprawnień reprezentanta podmiotu unijnego
- revoke - Odebranie uprawnień
- revokeAuthorizations - Odebranie uprawnień podmiotowych
- getOperationStatus - Pobranie statusu operacji
- getPersonGrants - Pobranie listy uprawnień do pracy w KSeF nadanych osobom fizycznym lub podmiotom
- getSubunitsGrants - Pobranie listy uprawnień administratorów jednostek i podmiotów podrzędnych
- getEntityRoles - Pobranie listy ról podmiotu
- getSubordinateEntitiesRoles - Pobranie listy podmiotów podrzędnych
- getAuthorizationsGrants - Pobranie listy uprawnień podmiotowych do obsługi faktur
- getEuEntityGrants - Pobranie listy uprawnień administratorów lub reprezentantów podmiotów unijnych uprawnionych do samofakturowania
security
- getPublicKeyCertificates - Pobranie certyfikatów
sessions
- getList - Pobranie listy sesji
- getStatus - Pobranie statusu sesji
- getUpo - Pobranie UPO dla sesji
- openOnline - Otwarcie sesji interaktywnej
- closeOnline - Zamknięcie sesji interaktywnej
- openBatch - Otwarcie sesji wsadowej
- closeBatch - Zamknięcie sesji wsadowej
testData
- createSubject - Utworzenie podmiotu
- removeSubject - Usunięcie podmiotu
- createPerson - Utworzenie osoby fizycznej
- removePerson - Usunięcie osoby fizycznej
- assignPermissions - Nadanie uprawnień testowemu podmiotowi/osobie fizycznej
- revokePermissions - Odebranie uprawnień testowemu podmiotowi/osobie fizycznej
- addAttachment - Umożliwienie wysyłania faktur z załącznikiem
- revokeAttachment - Odebranie możliwości wysyłania faktur z załącznikiem
tokens
Error Handling
Handling errors in this SDK should largely match your expectations. All operations return a response object or throw an exception.
By default an API error will raise a Errors\APIException
exception, which has the following properties:
Property | Type | Description |
---|---|---|
$message |
string | The error message |
$statusCode |
int | The HTTP status code |
$rawResponse |
?\Psr\Http\Message\ResponseInterface | The raw HTTP response |
$body |
string | The response content |
When custom error responses are specified for an operation, the SDK may also throw their associated exception. You can refer to respective Errors tables in SDK docs for more details on possible exception types for each operation. For example, the getCurrentSessions
method throws the following exceptions:
Error Type | Status Code | Content Type |
---|---|---|
Errors\ExceptionResponse | 400 | application/json |
Errors\APIException | 4XX, 5XX | */* |
Example
declare(strict_types=1); require 'vendor/autoload.php'; use Intermedia\Ksef\Apiv2; 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\APIException $e) { // handle default exception throw $e; }
Server Selection
Override Server URL Per-Client
The default server can be overridden globally using the setServerUrl(string $serverUrl)
builder method when initializing the SDK client instance. For example:
declare(strict_types=1); require 'vendor/autoload.php'; use Intermedia\Ksef\Apiv2; $sdk = Apiv2\Client::builder() ->setServerURL('https://ksef-test.mf.gov.pl') ->setSecurity( '<YOUR_BEARER_TOKEN_HERE>' ) ->build(); $response = $sdk->auth->getCurrentSessions( pageSize: 10 ); if ($response->authenticationListResponse !== null) { // handle response }
Development
Maturity
This SDK is in beta, and there may be breaking changes between versions without a major version update. Therefore, we recommend pinning usage to a specific package version. This way, you can install the same version each time without breaking changes unless you are intentionally looking for the latest version.
Contributions
While we value open-source contributions to this SDK, this library is generated programmatically. Any manual changes added to internal files will be overwritten on the next generation. We look forward to hearing your feedback. Feel free to open a PR or an issue with a proof of concept and we'll do our best to include it in a future release.