Metadata-Version: 2.4
Name: nakopay
Version: 1.0.0
Summary: Official NakoPay SDK for Python. Accept Bitcoin, Lightning, Litecoin, Monero and more with a Stripe-style API.
Project-URL: Homepage, https://nakopay.com
Project-URL: Documentation, https://nakopay.com/docs/sdk/python
Project-URL: Repository, https://github.com/NakoPayHQ/sdk-python
Project-URL: Issues, https://github.com/NakoPayHQ/sdk-python/issues
Author-email: NakoPay <support@nakopay.com>
License: MIT
Keywords: bitcoin,btcpay,crypto,invoices,lightning,monero,nakopay,payments
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: Office/Business :: Financial
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Classifier: Typing :: Typed
Requires-Python: >=3.8
Requires-Dist: httpx<1.0,>=0.24
Provides-Extra: dev
Requires-Dist: mypy>=1.5; extra == 'dev'
Requires-Dist: pytest-asyncio>=0.21; extra == 'dev'
Requires-Dist: pytest>=7.0; extra == 'dev'
Requires-Dist: respx>=0.20; extra == 'dev'
Requires-Dist: ruff>=0.1; extra == 'dev'
Description-Content-Type: text/markdown

# nakopay

[![PyPI](https://img.shields.io/pypi/v/nakopay.svg)](https://pypi.org/project/nakopay/)
[![Python](https://img.shields.io/pypi/pyversions/nakopay.svg)](https://pypi.org/project/nakopay/)
[![License](https://img.shields.io/pypi/l/nakopay.svg)](./LICENSE)

Official [NakoPay](https://nakopay.com) SDK for Python.
Accept Bitcoin, Lightning, Litecoin, Monero and more with a developer-first REST API.

## Install

```bash
pip install nakopay
```

Requires Python 3.8+.

## Quickstart (sync)

```python
import os
from nakopay import NakoPay

client = NakoPay(api_key=os.environ["NAKOPAY_SECRET_KEY"])

invoice = client.invoices.create(
    amount="19.99",
    currency="USD",
    coin="BTC",
    description="Pro plan",
    metadata={"order_id": "ORD-1042"},
    idempotency_key="ord_1042",
)

print(invoice.id, invoice.checkout_url)
```

## Async client

```python
import asyncio
from nakopay import AsyncNakoPay

async def main():
    async with AsyncNakoPay() as client:
        invoice = await client.invoices.create(
            amount="9.99",
            currency="USD",
            coin="BTC",
        )
        print(invoice.checkout_url)

        # Async pagination
        page = await client.invoices.list(status="paid")
        async for inv in page.auto_paging_iter():
            print(inv.id, inv.amount)

asyncio.run(main())
```

## Verifying webhooks (Flask)

```python
from flask import request
from nakopay import NakoPay, NakoPayError

client = NakoPay(api_key=os.environ["NAKOPAY_SECRET_KEY"])

@app.post("/webhook")
def webhook():
    try:
        event = client.webhooks.construct_event(
            payload=request.data,
            signature_header=request.headers["X-NakoPay-Signature"],
            secret=os.environ["NAKOPAY_WEBHOOK_SECRET"],
        )
    except NakoPayError:
        return "", 400

    if event.type == "invoice.paid":
        fulfill(event.data["object"])

    return "", 200
```

## Error handling

```python
from nakopay import NakoPayError, AuthenticationError, RateLimitError, IdempotencyError

try:
    client.invoices.create(amount="0", currency="USD", coin="BTC")
except AuthenticationError as e:
    print("Bad API key:", e.message)
except RateLimitError as e:
    print("Slow down:", e.message)
except IdempotencyError as e:
    print("Duplicate request with different params:", e.message)
except NakoPayError as e:
    print(e.code, e.message, e.param, e.request_id, e.http_status)
```

## Pagination

```python
for inv in client.invoices.list(status="paid").auto_paging_iter():
    print(inv.id, inv.amount)
```

## Links

- [NakoPay Website](https://nakopay.com)
- [Documentation](https://nakopay.com/docs)
- [Integration Guide](https://nakopay.com/docs/python)
- [API Reference](https://nakopay.com/docs/api-reference)

## License

MIT
