# TaxVega Python SDK (`tv_client.py`)

Thin stdlib-only client for the TaxVega engine (`urllib`, no pip installs).
TaxVega computes and proves (regime, rate, `tv1_*` digest of the rules snapshot);
**it never touches money** — payments go through your own gateway.

Files in `/sdk/`:
- `tv-widget.js` — checkout widget (browser, no dependencies)
- `tv_client.py` — this SDK
- `sdk-docs.html` — full docs on the site (en/ru)

## Install

Copy `tv_client.py` into your project. Python 3.8+, standard library only.

```bash
curl -O https://your-site/sdk/tv_client.py
```

## Quick start

```python
from tv_client import TaxVegaClient

tv = TaxVegaClient("https://taxvega.makomemory.com", key="tv_your_key")

# 1. Resolve the tax moment for a sale
res = tv.resolve("DE", buyer="b2c", vat_valid=False)
print(res["regime"], res["rate"], res["rule_digest"])
# eu_vat_b2c 19.00 tv1_bbadb83a17140ff66c1b1f56

# B2B with a verified VAT ID -> Reverse Charge
res = tv.resolve("DE", buyer="b2b", vat_valid=True)   # rate 0, regime eu_vat_reverse

# 2. Public rules table (sources + effective dates + digest)
rules = tv.public_rules("RU")
print(rules["digest"], len(rules["rules"]))

# 3. Multi-party chain (requires a cabinet key)
res = tv.chain(
    participants=[
        {"id": "p0", "kind": "company", "country": "RU", "role": "seller"},
        {"id": "p1", "kind": "company", "country": "KZ", "role": "agent"},
        {"id": "p2", "kind": "company", "country": "CN", "role": "supplier"},
    ],
    links=[{"from": "p0", "to": "p2", "type": "goods", "amount": 100000}],
)
```

Save the digest into your order record — it is the proof of the calculation forever.

## Errors — fail-closed

Any failure raises `TaxVegaError` with a `.code` (HTTP status int, or
`'network'` / `'parse'`) and `.detail` (engine message). You never get tax
numbers that did not come from the engine.

| Exception | Code | Meaning |
|---|---|---|
| `TaxVegaAuthError` | 401 | endpoint requires a key / key missing |
| `TaxVegaPaymentRequiredError` | 402 | key unknown or rejected (demo quota over) |
| `TaxVegaRateLimitError` | 429 | quota exceeded — retried automatically |
| `TaxVegaServerError` | 5xx | engine failure — retried automatically |
| `TaxVegaUnavailableError` | network | engine unreachable / unparsable answer |

```python
from tv_client import TaxVegaClient, TaxVegaError, TaxVegaPaymentRequiredError

try:
    res = tv.resolve("DE")
except TaxVegaPaymentRequiredError as e:
    print("bad key:", e.code, e.detail)      # checkout still possible — tax manually
except TaxVegaError as e:
    print("no calculation:", e.code, e.detail)
```

Transient failures (network errors, 429, 5xx) are retried 3 times with
exponential backoff (0.5 s, 1 s). 4xx errors never retry.

## CLI smoke tool

```bash
TAXVEGA_ENGINE=https://taxvega.makomemory.com python3 tv_client.py resolve DE b2c
TAXVEGA_ENGINE=https://taxvega.makomemory.com python3 tv_client.py rules RU
TAXVEGA_KEY=tv_... python3 tv_client.py chain demo
```

Exit code 1 + `TaxVegaError: code=...` on stderr on failure.

## Notes

- The key goes in the `Authorization: Bearer tv_...` header, never in the URL
  (query strings land in logs).
- Free plan: 100 calculations/month; without a key — 3 demo calculations per IP.
- The widget (`tv-widget.js`) is the browser side of the same contract; it is
  fail-closed too: engine down -> "calculation unavailable", no numbers invented.
