| name | ksef-cli |
| description | Use when an agent needs to interact with the Polish KSeF e-invoice system — list, send, download invoices or check auth session status via the ksef CLI tool |
ksef-cli
CLI tool for the Polish National e-Invoice System (KSeF). Agents use this tool to authenticate, list, send, and download invoices programmatically.
Prerequisites
ksef installed: pip install -e . in the project root
- Credentials configured:
KSEF_NIP + KSEF_TOKEN env vars, OR run ksef config set --nip NIP --token TOKEN
- For PDF/image extraction:
ANTHROPIC_API_KEY env var set
Agent Workflow
Always check auth before any invoice operation:
ksef auth status --json
If sessionActive is false, log in first:
ksef auth login --nip $KSEF_NIP --token $KSEF_TOKEN
Commands
Auth Status
ksef auth status --json
List Invoices
ksef invoice list --json
ksef invoice list --json --date-from 2024-01-01 --date-to 2024-01-31
ksef invoice list --json --received
Check Invoice Status
ksef invoice status REF_NUMBER --json
Send Invoice
ksef invoice send invoice.xml --json
ksef invoice send invoice.pdf --json
ksef invoice send invoice.xml --no-wait --json
Download Invoice
ksef invoice get KSeF_NUMBER --out invoice.xml
ksef invoice get KSeF_NUMBER --pdf --out inv.pdf
Generate XML (no send)
ksef invoice generate invoice.pdf --out invoice.xml
Error Handling
- Exit code
0 = success, exit code 1 = error
- With
--json: errors go to stderr as {"error": "message"}
- Read stderr with:
result = subprocess.run([...], capture_output=True); err = json.loads(result.stderr)
Environments
Default: PRD (production). Switch with ksef config set --environment TEST.