A modern Python SDK for the Fakturownia.pl invoicing API. Provides synchronous and asynchronous clients, typed invoice models, and sanitized error handling.
- Python 3.12+
- Built on
httpxand Pydantic v2 - Sync and async clients with shared behavior
- Typed invoice create/update/list/email models with tolerant parsing
- Resource-oriented API:
client.invoices.get(123)
# with uv
uv add fakturownia-sdk
# or with pip
pip install fakturownia-sdkEach Fakturownia account has its own subdomain (https://<account>.fakturownia.pl)
and an API token. Sign in to your account, open
Ustawienia → Integracje → API, and copy the token. The SDK never reads
environment variables implicitly — pass the token explicitly from your
application configuration:
import os
api_token = os.environ["FAKTUROWNIA_API_TOKEN"]
account = os.environ["FAKTUROWNIA_ACCOUNT"]Never commit real API tokens to source control.
from fakturownia_sdk import FakturowniaClient
with FakturowniaClient(
account="my-company",
api_token="...",
) as client:
invoice = client.invoices.get(123)
print(invoice.number, invoice.price_gross)import asyncio
from fakturownia_sdk import AsyncFakturowniaClient
async def main() -> None:
async with AsyncFakturowniaClient(
account="my-company",
api_token="...",
) as client:
invoice = await client.invoices.get(123)
print(invoice.number, invoice.price_gross)
asyncio.run(main())Both clients accept either a bare account prefix ("my-company") or a full
URL ("https://my-company.fakturownia.pl"). The base URL is normalized to
https://<account>.fakturownia.pl with no trailing slash.
If you do not pass an http_client, the SDK constructs and owns an httpx
client and closes it when you close the SDK client. If you inject your own
httpx.Client / httpx.AsyncClient, the SDK never closes it — you retain
ownership. Both clients support with / async with and explicit .close()
/ .aclose().
import httpx
transport = httpx.HTTPTransport(retries=0)
http_client = httpx.Client(transport=transport, timeout=30.0)
with FakturowniaClient(account="my-company", api_token="...", http_client=http_client) as client:
...
# http_client is still open and owned by the caller.from decimal import Decimal
from fakturownia_sdk import FakturowniaClient
from fakturownia_sdk.models import InvoiceCreate, InvoicePosition
invoice_data = InvoiceCreate(
kind="vat",
client_id=123,
positions=[
InvoicePosition(
name="Consulting",
quantity=Decimal("1"),
tax=Decimal("23"),
total_price_gross=Decimal("123.00"),
)
],
)
with FakturowniaClient(account="my-company", api_token="...") as client:
invoice = client.invoices.create(invoice_data)
print(invoice.id, invoice.number)InvoiceCreate accepts extra fields not modeled by the SDK, so newly
introduced API fields can be passed without waiting for a release. Documented
top-level options such as buyer_id or product_id are passed as keyword
arguments:
client.invoices.create(invoice_data, buyer_id=999, product_id=42)from fakturownia_sdk.models import InvoiceListParams
# Single page (one HTTP request)
invoices = client.invoices.list(
InvoiceListParams(
period="last_month",
kinds=["vat", "margin"],
include_positions=True,
per_page=50,
)
)
# Iterate every matching invoice across pages
for invoice in client.invoices.iter_all(InvoiceListParams(period="last_year")):
print(invoice.id, invoice.number)iter_all() requests sequential pages of per_page=100 by default and stops
when a page is empty or returns fewer records than the page size. The async
client mirrors this with an async iterator:
async for invoice in async_client.invoices.iter_all():
...pdf_bytes = client.invoices.download_pdf(123)
with open("invoice-123.pdf", "wb") as fh:
fh.write(pdf_bytes)Pass a print_option for a specific PDF layout:
from fakturownia_sdk.models import InvoicePrintOption
pdf_bytes = client.invoices.download_pdf(123, print_option=InvoicePrintOption.COPY)File writing is intentionally the caller's responsibility.
# Update — only explicitly supplied fields are sent
client.invoices.update(123, InvoiceUpdate(description="updated description"))
# Delete
client.invoices.delete(123)
# Cancel with optional reason
client.invoices.cancel(123, reason="customer cancelled")
# Send by email
from fakturownia_sdk.models import InvoiceEmailOptions
client.invoices.send_by_email(
123,
InvoiceEmailOptions(
email_to=["buyer@example.com", "finance@example.com"],
email_cc=["boss@example.com"],
email_pdf=True,
),
)
# Change status
client.invoices.change_status(123, "paid")The async equivalents simply require await:
await async_client.invoices.update(123, InvoiceUpdate(description="x"))
await async_client.invoices.delete(123)
await async_client.invoices.cancel(123)
await async_client.invoices.send_by_email(123)
await async_client.invoices.change_status(123, "paid")
pdf_bytes = await async_client.invoices.download_pdf(123)All SDK errors derive from FakturowniaError:
FakturowniaError
├── FakturowniaTransportError # httpx connection/timeout failures
└── FakturowniaAPIError # non-success HTTP responses
├── FakturowniaAuthenticationError # 401, 403
├── FakturowniaNotFoundError # 404
├── FakturowniaValidationError # 422
└── FakturowniaRateLimitError # 429
from fakturownia_sdk import (
FakturowniaAuthenticationError,
FakturowniaError,
FakturowniaNotFoundError,
FakturowniaRateLimitError,
FakturowniaValidationError,
)
try:
invoice = client.invoices.get(123)
except FakturowniaNotFoundError:
print("invoice does not exist")
except FakturowniaAuthenticationError:
print("check your API token")
except FakturowniaRateLimitError as exc:
print(f"slow down; retry after {exc.headers.get('retry-after')}s")
except FakturowniaValidationError as exc:
print(f"server rejected the request: {exc.error_data}")
except FakturowniaError as exc:
print(f"other SDK error: {exc}")FakturowniaAPIError exposes status_code, method, a sanitized url
(api_token always removed), error_data (parsed JSON body if available),
response_text (raw body otherwise), and a small set of safe response
headers. API tokens never appear in exception messages, exception URLs, or
client repr() output.
This repository uses uv for dependency
management and Hatchling as the build backend.
# Install dev dependencies (creates uv.lock)
uv sync --all-extras
# Lint and format
uv run ruff check .
uv run ruff format --check .
# Type-check
uv run mypy src
# Run tests with coverage
uv run pytest --cov=fakturownia_sdk
# Build the wheel and source distribution
uv buildAll tests are deterministic and mocked with respx; no network access or
real credentials are required.
Upstream API reference: https://github.com/fakturownia/API#faktury
MIT