Przejdź do głównej zawartości

Przeglad pakietu @ksefnik/http

Pakiet @ksefnik/http to klient HTTP do API KSeF w wersji 2.0. Implementuje pelny protokol autentykacji challenge-response z szyfrowaniem RSA-OAEP, zarzadzanie sesjami (access + refresh token), pobieranie metadanych faktur z paginacja i opcjonalnym wzbogacaniem XML, oraz retry z obsluga rate limitow.

ScenariuszPakiet
Produkcja — prawdziwe API KSeF@ksefnik/http
Testy jednostkowe i integracyjne@ksefnik/simulator
Wlasna integracja (proxy, gateway)Wlasna implementacja KsefClient
Okno terminala
pnpm add @ksefnik/http

@ksefnik/http wymaga @ksefnik/core i @ksefnik/shared jako peer dependencies.

import { createHttpAdapter } from '@ksefnik/http'
import { createKsefnik } from '@ksefnik/core'
const adapter = createHttpAdapter({
nip: '5213456784',
token: process.env.KSEF_TOKEN!,
environment: 'production',
})
const ksefnik = createKsefnik({
nip: '5213456784',
adapter,
})
await ksefnik.invoices.sync({
dateFrom: '2026-03-01',
dateTo: '2026-03-31',
})

Funkcja createHttpAdapter() tworzy gotowy adapter — inicjalizuje KsefHttpClient i opakowuje go w KsefAdapterImpl z @ksefnik/core.

┌─────────────┐ ┌──────────────────┐ ┌─────────────────┐ ┌──────────┐
│ Twoj kod │────>│ KsefAdapterImpl │────>│ KsefHttpClient │────>│ KSeF API │
│ (SDK/CLI) │ │ (@ksefnik/core) │ │ (@ksefnik/http) │ │ (MF v2) │
└─────────────┘ └──────────────────┘ └─────────────────┘ └──────────┘

KsefHttpClient implementuje interfejs KsefClient z @ksefnik/core. Adapter opakowuje go w stabilny interfejs KsefAdapter, dodajac zarzadzanie sesja i mapowanie typow.

Pakiet obsluguje trzy srodowiska KSeF:

SrodowiskoURL bazowyUzycie
productionhttps://api.ksef.mf.gov.pl/v2Produkcja — prawdziwe faktury
demohttps://api-demo.ksef.mf.gov.pl/v2Demo — testowanie z prawdziwym API
testhttps://api-test.ksef.mf.gov.pl/v2Testy — srodowisko testowe MF
import { ENDPOINTS } from '@ksefnik/http'
console.log(ENDPOINTS.production) // https://api.ksef.mf.gov.pl/v2
console.log(ENDPOINTS.demo) // https://api-demo.ksef.mf.gov.pl/v2
console.log(ENDPOINTS.test) // https://api-test.ksef.mf.gov.pl/v2

createHttpAdapter przyjmuje opcjonalne parametry klienta:

const adapter = createHttpAdapter({
nip: '5213456784',
token: process.env.KSEF_TOKEN!,
environment: 'demo',
publicKeyPem: customPublicKey, // opcjonalnie -- pin klucza MF
client: {
timeoutMs: 30_000, // timeout zapytan (domyslnie brak)
userAgent: 'my-app/1.0', // naglowek User-Agent
retry: { // konfiguracja retry
maxAttempts: 5,
baseDelayMs: 2000,
},
},
})
OpcjaTypDomyslnieOpis
nipstring—NIP podatnika (wymagany)
tokenstring—Token KSeF do autentykacji (wymagany)
environment'production' | 'demo' | 'test'—Srodowisko KSeF (wymagany)
publicKeyPemstringauto-fetchKlucz publiczny MF w formacie PEM/SPKI
client.timeoutMsnumber—Timeout zapytan HTTP w ms
client.userAgentstring—Naglowek User-Agent
client.retryHttpRetryOptionsdomyslneKonfiguracja mechanizmu retry
// Glowne
export { createHttpAdapter } from '@ksefnik/http'
export { KsefHttpClient } from '@ksefnik/http'
// Bledy
export { KsefApiError, KsefAuthError, KsefRateLimitError } from '@ksefnik/http'
// Retry
export { withHttpRetry } from '@ksefnik/http'
// Srodowiska
export { ENDPOINTS } from '@ksefnik/http'
// XML
export { parseInvoiceXml } from '@ksefnik/http'