# ksef.generate

Generate FA(3) XML for a domestic Polish PLN B2B VAT invoice with up to 500 lines and check it locally against the official XSD.

- REST: `POST https://api.kanagu.dev/v1/ksef/generate`
- MCP: `call_operation` with `operation: "ksef.generate"` at https://api.kanagu.dev/mcp
- Price: $0.04 USD per invoice, on successful calls only.
- Auth: `Authorization: Bearer <key>` for REST and `call_operation`. Retries: reuse `Idempotency-Key` (MCP: `idempotencyKey`).

## Compliance coverage

- Domain: Tax & e-invoicing
- Jurisdictions: PL
- Standard: FA(3) XML schema (CRWDE 13775) 1-0E (2025-06-25) — https://crd.gov.pl/wzor/2025/06/25/13775/schemat.xsd
- Standard: Ministry of Finance StrukturyDanych XSD 10-0E (2022-01-05) — https://crd.gov.pl/xml/schematy/dziedzinowe/mf/2022/01/05/eD/DefinicjeTypy/StrukturyDanych_v10-0E.xsd
- Standard: Ministry of Finance ElementarneTypyDanych XSD 10-0E (2022-01-05) — https://crd.gov.pl/xml/schematy/dziedzinowe/mf/2022/01/05/eD/DefinicjeTypy/ElementarneTypyDanych_v10-0E.xsd
- Standard: Ministry of Finance KodyKrajow XSD 10-0E (2022-01-05) — https://crd.gov.pl/xml/schematy/dziedzinowe/mf/2022/01/05/eD/DefinicjeTypy/KodyKrajow_v10-0E.xsd
- Standard: Ministry of Finance FA(3) information guide August 2026 (2026-08-26) — https://ksef.podatki.gov.pl/media/nuclnu0w/broszura-informacyjna-dotyczaca-struktury-logicznej-fa-3.pdf
- Standard: Polish VAT Act, Article 106e Dz.U. 2026 poz. 1263 (2026-09-28) — https://eli.gov.pl/eli/DU/2026/1263/ogl
- Standard: KSeF invoice verification 2026-04-09 (2026-04-09) — https://github.com/CIRFMF/ksef-api/blob/c67c589c4bc695bfde9541133b35ec6b8e794562/faktury/weryfikacja-faktury.md
- Standard: Extensible Markup Language (XML) 1.0, fifth edition (2008-11-26) — https://www.w3.org/TR/2008/REC-xml-20081126/
- Verification: ksef.generate checks generated XML and ksef.validate checks submitted XML against the Ministry's FA(3) XSD and supported invoice-level and arithmetic rules.
- Last reviewed: 2026-11-26

## Request body

- `invoiceNumber` (string (max 256 chars), required)
- `issueDate` (string (date), required)
- `createdAt` (string (date-time), required): Actual XML creation timestamp, in UTC (YYYY-MM-DDTHH:mm:ssZ).
- `supplyDate` (string (date)): Common delivery or service date, when known and different from the issue date.
- `dueDate` (string (date))
- `seller` (object, required)
- `seller.nip` (string, required): Polish NIP, ten digits without the PL prefix.
- `seller.name` (string (max 512 chars), required)
- `seller.addressLine1` (string (max 512 chars), required)
- `seller.addressLine2` (string (max 512 chars))
- `buyer` (object, required)
- `buyer.nip` (string, required): Polish NIP, ten digits without the PL prefix.
- `buyer.name` (string (max 512 chars), required)
- `buyer.addressLine1` (string (max 512 chars), required)
- `buyer.addressLine2` (string (max 512 chars))
- `cashAccounting` (boolean, required): Whether the invoice requires the cash accounting annotation (metoda kasowa).
- `lines` (array of object, required)
- `lines[].description` (string (max 512 chars), required)
- `lines[].unit` (string (max 256 chars), required)
- `lines[].quantity` (string, required)
- `lines[].unitNetPrice` (string, required)
- `lines[].vatRate` ("5" | "8" | "23" | "0 KR", required): FA(3) P_12 code supplied by the issuer. 0 KR is domestic zero-rated sales.
- `lines[].annex15` (boolean, required): Whether the goods or services fall under Annex 15 of the VAT Act.

## Response

- `status` ("valid" | "invalid" | "needs_review", required): Outcome of the local checks.
- `xsd` ("passed" | "failed" | "not_run", required)
- `specification` (object, required)
- `specification.version` ("1-0E", required)
- `specification.date` ("2025-06-25", required)
- `specification.url` ("https://crd.gov.pl/wzor/2025/06/25/13775/schemat.xsd", required)
- `issues` (array of object, required)
- `issues[].severity` ("error" | "needs_review", required)
- `issues[].rule` (string, required)
- `issues[].path` (string, required)
- `issues[].line` (integer (≤ 9007199254740991))
- `issues[].expected` (string, required)
- `issues[].actual` (string, required)
- `issues[].source` (string (uri), required)
- `xml` (string): FA(3) XML when the local checks pass.

## Example

```sh
curl https://api.kanagu.dev/v1/ksef/generate \
  -H "Authorization: Bearer $KANAGU_KEY" \
  -H "Content-Type: application/json" \
  -d '{"invoiceNumber":"FV/2026/03/104","issueDate":"2026-03-02","createdAt":"2026-03-02T10:05:00Z","supplyDate":"2026-02-27","dueDate":"2026-03-16","seller":{"nip":"5260250274","name":"Przykładowy Dostawca sp. z o.o.","addressLine1":"ul. Wolska 14, 01-201 Warszawa"},"buyer":{"nip":"8567346215","name":"Przykładowy Nabywca sp. z o.o.","addressLine1":"ul. Długa 4, 31-146 Kraków"},"cashAccounting":false,"lines":[{"description":"Konserwacja urządzeń","unit":"godz.","quantity":"2.5","unitNetPrice":"120.00","vatRate":"23","annex15":false}]}'
```

```ts
const res = await fetch("https://api.kanagu.dev/v1/ksef/generate", {
  method: "POST",
  headers: {
    Authorization: `Bearer ${process.env.KANAGU_KEY}`,
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    "invoiceNumber": "FV/2026/03/104",
    "issueDate": "2026-03-02",
    "createdAt": "2026-03-02T10:05:00Z",
    "supplyDate": "2026-02-27",
    "dueDate": "2026-03-16",
    "seller": {
      "nip": "5260250274",
      "name": "Przykładowy Dostawca sp. z o.o.",
      "addressLine1": "ul. Wolska 14, 01-201 Warszawa"
    },
    "buyer": {
      "nip": "8567346215",
      "name": "Przykładowy Nabywca sp. z o.o.",
      "addressLine1": "ul. Długa 4, 31-146 Kraków"
    },
    "cashAccounting": false,
    "lines": [
      {
        "description": "Konserwacja urządzeń",
        "unit": "godz.",
        "quantity": "2.5",
        "unitNetPrice": "120.00",
        "vatRate": "23",
        "annex15": false
      }
    ]
  }),
});
if (!res.ok) throw new Error((await res.json()).error.message);
const result = await res.json();
```

## Errors

JSON `{"error":{"code","message"}}`.

- 400 `invalid_input`: The body does not match the request schema. The message names each field.
- 400 `invalid_json`: The body is missing or is not valid JSON.
- 400 `invalid_idempotency_key`: Idempotency-Key must be 1–128 visible ASCII characters.
- 401 `invalid_api_key`: The Bearer key is missing, malformed, invalid or inactive.
- 402 `free_allowance_exhausted`: This month's free usage is used up and the account has no subscription. Subscribe from the dashboard, or wait for the next UTC month; Retry-After gives the seconds left.
- 404 `not_found`: No operation at this path.
- 409 `idempotency_conflict`: The Idempotency-Key was already used with a different body.
- 413 `payload_too_large`: JSON bodies may be at most 64 KiB.
- 500 `internal_error`: The call failed on our side. It is not billed; retry with the same Idempotency-Key.

## JSON Schema

Request:
```json
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "properties": {
    "invoiceNumber": {
      "type": "string",
      "minLength": 1,
      "maxLength": 256,
      "pattern": "\\S"
    },
    "issueDate": {
      "type": "string",
      "format": "date",
      "pattern": "^(?:(?:\\d\\d[2468][048]|\\d\\d[13579][26]|\\d\\d0[48]|[02468][048]00|[13579][26]00)-02-29|\\d{4}-(?:(?:0[13578]|1[02])-(?:0[1-9]|[12]\\d|3[01])|(?:0[469]|11)-(?:0[1-9]|[12]\\d|30)|(?:02)-(?:0[1-9]|1\\d|2[0-8])))$"
    },
    "createdAt": {
      "type": "string",
      "format": "date-time",
      "pattern": "^(?:(?:\\d\\d[2468][048]|\\d\\d[13579][26]|\\d\\d0[48]|[02468][048]00|[13579][26]00)-02-29|\\d{4}-(?:(?:0[13578]|1[02])-(?:0[1-9]|[12]\\d|3[01])|(?:0[469]|11)-(?:0[1-9]|[12]\\d|30)|(?:02)-(?:0[1-9]|1\\d|2[0-8])))T(?:(?:[01]\\d|2[0-3]):[0-5]\\d:[0-5]\\d(?:Z))$",
      "description": "Actual XML creation timestamp, in UTC (YYYY-MM-DDTHH:mm:ssZ)."
    },
    "supplyDate": {
      "description": "Common delivery or service date, when known and different from the issue date.",
      "type": "string",
      "format": "date",
      "pattern": "^(?:(?:\\d\\d[2468][048]|\\d\\d[13579][26]|\\d\\d0[48]|[02468][048]00|[13579][26]00)-02-29|\\d{4}-(?:(?:0[13578]|1[02])-(?:0[1-9]|[12]\\d|3[01])|(?:0[469]|11)-(?:0[1-9]|[12]\\d|30)|(?:02)-(?:0[1-9]|1\\d|2[0-8])))$"
    },
    "dueDate": {
      "type": "string",
      "format": "date",
      "pattern": "^(?:(?:\\d\\d[2468][048]|\\d\\d[13579][26]|\\d\\d0[48]|[02468][048]00|[13579][26]00)-02-29|\\d{4}-(?:(?:0[13578]|1[02])-(?:0[1-9]|[12]\\d|3[01])|(?:0[469]|11)-(?:0[1-9]|[12]\\d|30)|(?:02)-(?:0[1-9]|1\\d|2[0-8])))$"
    },
    "seller": {
      "type": "object",
      "properties": {
        "nip": {
          "type": "string",
          "pattern": "^[0-9]{10}$",
          "description": "Polish NIP, ten digits without the PL prefix."
        },
        "name": {
          "type": "string",
          "minLength": 1,
          "maxLength": 512,
          "pattern": "\\S"
        },
        "addressLine1": {
          "type": "string",
          "minLength": 1,
          "maxLength": 512,
          "pattern": "\\S"
        },
        "addressLine2": {
          "type": "string",
          "minLength": 1,
          "maxLength": 512,
          "pattern": "\\S"
        }
      },
      "required": [
        "nip",
        "name",
        "addressLine1"
      ],
      "additionalProperties": false
    },
    "buyer": {
      "type": "object",
      "properties": {
        "nip": {
          "type": "string",
          "pattern": "^[0-9]{10}$",
          "description": "Polish NIP, ten digits without the PL prefix."
        },
        "name": {
          "type": "string",
          "minLength": 1,
          "maxLength": 512,
          "pattern": "\\S"
        },
        "addressLine1": {
          "type": "string",
          "minLength": 1,
          "maxLength": 512,
          "pattern": "\\S"
        },
        "addressLine2": {
          "type": "string",
          "minLength": 1,
          "maxLength": 512,
          "pattern": "\\S"
        }
      },
      "required": [
        "nip",
        "name",
        "addressLine1"
      ],
      "additionalProperties": false
    },
    "cashAccounting": {
      "type": "boolean",
      "description": "Whether the invoice requires the cash accounting annotation (metoda kasowa)."
    },
    "lines": {
      "minItems": 1,
      "maxItems": 500,
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "description": {
            "type": "string",
            "minLength": 1,
            "maxLength": 512,
            "pattern": "\\S"
          },
          "unit": {
            "type": "string",
            "minLength": 1,
            "maxLength": 256,
            "pattern": "\\S"
          },
          "quantity": {
            "type": "string",
            "pattern": "^(?:[1-9][0-9]{0,15}|0)(?:\\.[0-9]{1,6})?$"
          },
          "unitNetPrice": {
            "type": "string",
            "pattern": "^(?:[1-9][0-9]{0,13}|0)(?:\\.[0-9]{1,8})?$"
          },
          "vatRate": {
            "type": "string",
            "enum": [
              "5",
              "8",
              "23",
              "0 KR"
            ],
            "description": "FA(3) P_12 code supplied by the issuer. 0 KR is domestic zero-rated sales."
          },
          "annex15": {
            "type": "boolean",
            "description": "Whether the goods or services fall under Annex 15 of the VAT Act."
          }
        },
        "required": [
          "description",
          "unit",
          "quantity",
          "unitNetPrice",
          "vatRate",
          "annex15"
        ],
        "additionalProperties": false
      }
    }
  },
  "required": [
    "invoiceNumber",
    "issueDate",
    "createdAt",
    "seller",
    "buyer",
    "cashAccounting",
    "lines"
  ],
  "additionalProperties": false,
  "examples": [
    {
      "invoiceNumber": "FV/2026/03/104",
      "issueDate": "2026-03-02",
      "createdAt": "2026-03-02T10:05:00Z",
      "supplyDate": "2026-02-27",
      "dueDate": "2026-03-16",
      "seller": {
        "nip": "5260250274",
        "name": "Przykładowy Dostawca sp. z o.o.",
        "addressLine1": "ul. Wolska 14, 01-201 Warszawa"
      },
      "buyer": {
        "nip": "8567346215",
        "name": "Przykładowy Nabywca sp. z o.o.",
        "addressLine1": "ul. Długa 4, 31-146 Kraków"
      },
      "cashAccounting": false,
      "lines": [
        {
          "description": "Konserwacja urządzeń",
          "unit": "godz.",
          "quantity": "2.5",
          "unitNetPrice": "120.00",
          "vatRate": "23",
          "annex15": false
        }
      ]
    }
  ]
}
```

Response:
```json
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "properties": {
    "status": {
      "type": "string",
      "enum": [
        "valid",
        "invalid",
        "needs_review"
      ],
      "description": "Outcome of the local checks."
    },
    "xsd": {
      "type": "string",
      "enum": [
        "passed",
        "failed",
        "not_run"
      ]
    },
    "specification": {
      "type": "object",
      "properties": {
        "version": {
          "type": "string",
          "const": "1-0E"
        },
        "date": {
          "type": "string",
          "const": "2025-06-25"
        },
        "url": {
          "type": "string",
          "const": "https://crd.gov.pl/wzor/2025/06/25/13775/schemat.xsd"
        }
      },
      "required": [
        "version",
        "date",
        "url"
      ],
      "additionalProperties": false
    },
    "issues": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "severity": {
            "type": "string",
            "enum": [
              "error",
              "needs_review"
            ]
          },
          "rule": {
            "type": "string"
          },
          "path": {
            "type": "string"
          },
          "line": {
            "type": "integer",
            "exclusiveMinimum": 0,
            "maximum": 9007199254740991
          },
          "expected": {
            "type": "string"
          },
          "actual": {
            "type": "string"
          },
          "source": {
            "type": "string",
            "format": "uri"
          }
        },
        "required": [
          "severity",
          "rule",
          "path",
          "expected",
          "actual",
          "source"
        ],
        "additionalProperties": false
      }
    },
    "xml": {
      "description": "FA(3) XML when the local checks pass.",
      "type": "string"
    }
  },
  "required": [
    "status",
    "xsd",
    "specification",
    "issues"
  ],
  "additionalProperties": false
}
```
