Integration guide

KSeF API PHP integration guide

Send a KSeF FA(3) invoice from PHP with the Notaly API using cURL: POST /invoices, idempotency, error handling and status tracking.

Updated

Overview

PHP remains one of the most common languages for invoicing backends, and Notaly lets you plug KSeF into an existing PHP application in a few lines. You send invoice data as a JSON array to https://api.getnotaly.com/invoices; Notaly produces the FA(3) XML, validates it against the official XSD and submits it to KSeF. The example below uses PHP's built-in cURL extension, so it works without Composer or any third-party package.

Prerequisites

Sign up for a free sandbox account and copy the API key. The sandbox only accepts the seller NIP 2745013933 (the platform's test NIP); a different NIP triggers 422 seller_nip_not_authorized without using one of your three free test invoices. Store the key in an environment variable read with getenv(). Send an Idempotency-Key header derived from your own invoice identifier: repeating a request with the same key returns the invoice already stored instead of creating a new one.

Submit an invoice

<?php
$key = getenv('NOTALY_API_KEY');

$invoice = [
    'number' => 'FV/2026/10/001',
    'issueDate' => '2026-10-12',
    'currency' => 'PLN',
    'seller' => ['nip' => '2745013933', 'name' => 'Example Sp. z o.o. (FICTIONAL)', 'address' => ['line1' => 'ul. Testowa 1, 00-001 Warszawa', 'country' => 'PL']],
    'buyer' => ['nip' => '1234563218', 'name' => 'Test Customer S.A. (FICTIONAL)', 'address' => ['line1' => 'ul. Probna 2, 30-001 Kraków', 'country' => 'PL']],
    'items' => [
        ['name' => 'SaaS subscription - October', 'unit' => 'pcs.', 'quantity' => 1, 'unitNetPrice' => 1000, 'vatRate' => 23],
        ['name' => 'Support', 'unit' => 'hrs.', 'quantity' => 2, 'unitNetPrice' => 150.5, 'vatRate' => 8],
    ],
];

$ch = curl_init('https://api.getnotaly.com/invoices');
curl_setopt_array($ch, [
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POST => true,
    CURLOPT_HTTPHEADER => [
        'Authorization: Bearer ' . $key,
        'Content-Type: application/json',
        // stable per invoice: reuse the same key when you retry this invoice
        'Idempotency-Key: invoice-' . $invoice['number'],
    ],
    CURLOPT_POSTFIELDS => json_encode($invoice),
]);

$body = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
curl_close($ch);

$created = json_decode($body, true);
if ($status >= 400) {
    fwrite(STDERR, $status . ': ' . ($created['error'] ?? 'unknown') . PHP_EOL);
    exit(1);
}
echo 'Invoice ' . $created['id'] . ' is ' . $created['status'] . PHP_EOL;

curl_setopt_array configures the request in one place: POST method, headers and the encoded payload. After the call, curl_getinfo($ch, CURLINFO_RESPONSE_CODE) gives you the HTTP status so you can separate success from failure before decoding the body.

Interpret the reply

On success you get HTTP 201 and a JSON object. status is submitted when KSeF received the invoice and returned a reference number. If the response is draft with error: "ksef_submitting", KSeF has not confirmed the submission yet. Do not send a new POST and do not issue the invoice again under a new number: Notaly resends the same XML automatically. Poll GET /invoices/{id} until the status is submitted. Such a draft already counts towards the three sandbox invoices. If the status is submitted with error: "ksef_duplicate_review", KSeF already has this invoice from an earlier attempt. Do not reissue it with another number; we link it to the KSeF record on our side. If you need it sooner, write to hello@getnotaly.com with the invoice id. If the invoice is draft with error: "ksef_retry_exhausted", Notaly has stopped resending it automatically (after 24 hours, or earlier if KSeF returned a definitive error). Do not create a new invoice: write to hello@getnotaly.com with the invoice id, and we check in KSeF whether it arrived before anything is resent. If an invoice is still draft with error: null or ksef_unavailable after about a minute, Notaly does not resend it: do not create a new invoice; write to hello@getnotaly.com with the invoice id.

The id field is what you use later to poll status; accepted invoices also expose ksefNumber, pdfUrl and, when KSeF issues it, upo. Notaly validates and submits the invoice; VAT rates and tax classification remain your responsibility.

Deal with errors

Read the structured error field instead of matching human-readable text. validation_failed (422) includes a details array with the offending fields. xsd_invalid (422) means the generated XML failed the official FA(3) schema; nothing was sent or counted. currency_not_supported (422) means the currency is not PLN, the only one supported today. quota_exceeded (402) means the sandbox allowance of three invoices is used. 401 is a missing or invalid key, 404 a valid key for the wrong resource, and a 503 (ksef_unavailable or validator_unavailable) is transient with nothing stored — retry with backoff and the same key.

Follow the invoice to completion

Poll GET /invoices/{id} with the same bearer token, or set up webhooks. The lifecycle is draft → submitted → accepted or rejected; a rejected invoice carries an error code such as ksef_rejected:<code>.

Sources

Source: getnotaly.com

Frequently asked questions

Which PHP version and extensions do I need?

PHP 7.4+ with the cURL extension enabled, which is standard in most environments.

Can I use Guzzle instead of cURL?

Yes. Any HTTP client works as long as it sends JSON with the Authorization: Bearer header and, for safe retries, a stable Idempotency-Key.

Issue your first KSeF test invoice today

Create a sandbox account: API key in seconds, 3 free test invoices on KSeF TEST, no credit card.