Skip to content

NakoPayHQ/sdk-python

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

nakopay

PyPI Python License

Official NakoPay SDK for Python. Accept Bitcoin, Lightning, Litecoin, Monero and more with a developer-first REST API.

Install

pip install nakopay

Requires Python 3.8+.

Quickstart (sync)

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

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)

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

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

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

Links

License

MIT

About

Official NakoPay Python SDK

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages