Official NakoPay SDK for Python. Accept Bitcoin, Lightning, Litecoin, Monero and more with a developer-first REST API.
pip install nakopayRequires Python 3.8+.
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)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())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 "", 200from 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)for inv in client.invoices.list(status="paid").auto_paging_iter():
print(inv.id, inv.amount)MIT