Skip to main content

Command-line interface for the ksef2 Python SDK

Project description

ksef2-cli logo

Command-line interface for the ksef2 Python SDK.

The CLI avoids hidden state: authenticated commands accept credentials from global options or environment variables, and resumable workflow files are written only when you pass options such as --receipt, --receipt-dir, or --state-file.

Install / Run

pip install ksef2-cli
ksef2 --help

For an isolated CLI install:

uv tool install ksef2-cli
# or
pipx install ksef2-cli

The package exposes scriptable CLI commands:

ksef2
ksef2-cli

Documentation

Published documentation is assembled into https://docs.ksef2.dev/cli/. Source documentation lives in docs/.

Authentication

For repeated local work, create a profile once and let commands inherit its environment, NIP, and authentication settings:

uv run ksef2 profile create demo-client \
  --env test \
  --nip 6880313213 \
  --cert /path/accountant-auth-cert.pem \
  --key /path/accountant-auth-key.pem

uv run ksef2 invoices metadata --role buyer --date-from 2026-01-01T00:00:00Z

Global auth options are still available for CI and one-off commands. They must be placed before the command group:

uv run ksef2 --env test --nip 5261040828 --test-cert auth login --json
uv run ksef2 --nip 5261040828 --token "$KSEF_TOKEN" invoices metadata --date-from 2026-01-01T00:00:00Z

Supported direct auth methods:

  • --token / KSEF2_TOKEN
  • --test-cert for the TEST environment
  • --cert and --key for PEM XAdES credentials
  • --p12 for PKCS#12/PFX XAdES credentials

Common environment variables:

export KSEF2_PROFILE=demo-client
export KSEF2_NIP=5261040828
export KSEF2_TOKEN=...

Precedence is:

  1. CLI options such as --profile, --nip, and --token
  2. Environment variables such as KSEF2_PROFILE, KSEF2_NIP, and KSEF2_TOKEN
  3. The active profile in the local config file

Local Config

For local development, create profiles under your home directory:

uv run ksef2 profile create demo-client --env test --nip 5261040828 --test-cert
uv run ksef2 profile current
uv run ksef2 profile list

By default the file is:

~/.config/ksef2-cli/config.toml

You can override it with --config path/to/config.toml or KSEF2_CONFIG. Use --no-config to ignore the file for one invocation.

Example config:

active_profile = "demo-client"

[profiles.demo-client]
environment = "test"
nip = "5261040828"

[profiles.demo-client.auth]
type = "test_certificate"

Profiles store token and password environment variable names rather than secret values.

Examples

Query invoice metadata:

uv run ksef2 --nip "$KSEF2_NIP" --token "$KSEF2_TOKEN" \
  invoices metadata --role seller --date-from 2026-01-01T00:00:00Z --all

Download one processed invoice XML:

uv run ksef2 --nip "$KSEF2_NIP" --token "$KSEF2_TOKEN" \
  invoices download --ksef-number "$KSEF_NUMBER" --out invoice.xml

Schedule and fetch an export:

uv run ksef2 --nip "$KSEF2_NIP" --token "$KSEF2_TOKEN" \
  invoices export --date-from 2026-01-01T00:00:00Z --handle-file export.json

uv run ksef2 --nip "$KSEF2_NIP" --token "$KSEF2_TOKEN" \
  invoices export-fetch --handle-file export.json --wait --out-dir downloads

Send one invoice and save its UPO:

uv run ksef2 --env test --nip "$KSEF2_NIP" --test-cert \
  invoices send invoice.xml --wait --upo-dir upos

Send every XML file in a directory and save receipts for later status/UPO checks:

uv run ksef2 --env test --nip "$KSEF2_NIP" --test-cert \
  invoices send invoices/ --receipt-dir receipts

Fetch a UPO later from a receipt:

uv run ksef2 --env test --nip "$KSEF2_NIP" --test-cert \
  invoices upo --receipt receipts/invoice-receipt.json --out invoice-upo.xml

Submit invoices as one batch:

uv run ksef2 --nip "$KSEF2_NIP" --token "$KSEF2_TOKEN" \
  invoices send invoice-1.xml invoice-2.xml --mode batch --wait --upo-dir upos

The lower-level online and batch command groups remain available when you need explicit session control.

Permission queries and TEST limit updates use JSON payloads shaped like the SDK models:

uv run ksef2 --nip "$KSEF2_NIP" --token "$KSEF2_TOKEN" \
  permissions query persons --payload person-query.json

uv run ksef2 --env test --nip "$KSEF2_NIP" --token "$KSEF2_TOKEN" \
  limits set api --payload api-rate-limits.json

Use --json for script-friendly output.

Maintainability

The CLI is split by command domain under src/ksef2_cli/commands/, with shared runtime, auth, rendering, parsing, JSON I/O, and invoice workflow modules. See docs/contributing/architecture.md for the module map and the rules for adding new commands without growing large files again.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ksef2_cli-0.0.1.tar.gz (1.5 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ksef2_cli-0.0.1-py3-none-any.whl (59.6 kB view details)

Uploaded Python 3

File details

Details for the file ksef2_cli-0.0.1.tar.gz.

File metadata

  • Download URL: ksef2_cli-0.0.1.tar.gz
  • Upload date:
  • Size: 1.5 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for ksef2_cli-0.0.1.tar.gz
Algorithm Hash digest
SHA256 a438accbde2e524d07287028e59efd04a83cb46e24ce9c2d6304ad59e21e7a37
MD5 32b39f0d5e476df2e9e8fb6493ef79d6
BLAKE2b-256 0e6a5f894caf70ad3a4e4a57aa88df907275a8a9598d5f36384e85ed546d094a

See more details on using hashes here.

Provenance

The following attestation bundles were made for ksef2_cli-0.0.1.tar.gz:

Publisher: publish.yml on stacking-hq/ksef2-cli

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file ksef2_cli-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: ksef2_cli-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 59.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for ksef2_cli-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 685a38c979f5c4c244c7116468797183dfa7206c06226482beb133c79d71c016
MD5 dcaef30eaf8c09ce7064f55cf585f42e
BLAKE2b-256 9b36ad1e475d56203086218db0a8400e6565e608c0a8222e6f5bd5a22d57c1fb

See more details on using hashes here.

Provenance

The following attestation bundles were made for ksef2_cli-0.0.1-py3-none-any.whl:

Publisher: publish.yml on stacking-hq/ksef2-cli

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page