Python SDK

The official NahuPay Python library supports Python 3.9+ and includes sync and async clients.

Installation

pip install nahupay

Initialisation

import nahupayimport osclient = nahupay.NahuPay(api_key=os.environ["NAHUPAY_SECRET_KEY"],)

Payments

Create

payment = client.payments.create(amount=50000,currency="ETB",methods=["telebirr", "bank_transfer"],success_url="https://myshop.com/success",cancel_url="https://myshop.com/cart",metadata={"order_id": "ORD-99"},)print(payment.checkout_url)

Retrieve

payment = client.payments.retrieve("pay_01hx9m2k")print(payment.status)  # "completed"

List (auto-paginate)

for payment in client.payments.list(status="completed").auto_paginate():print(payment.id)

Webhooks

Verify and parse a webhook in Django or FastAPI:

# FastAPI examplefrom fastapi import Request, HTTPExceptionfrom nahupay import WebhookSignatureErrorasync def webhook(request: Request):payload = await request.body()      # raw bytessig     = request.headers["NahuPay-Signature"]try:event = client.webhooks.construct_event(payload, sig,os.environ["NAHUPAY_WEBHOOK_SECRET"])except WebhookSignatureError:raise HTTPException(status_code=400, detail="Invalid signature")if event.type == "payment.completed":await fulfill_order(event.data.object)return {"received": True}

Async support

Use AsyncNahuPay for async contexts:

from nahupay import AsyncNahuPayclient = AsyncNahuPay(api_key=...)async def run():payment = await client.payments.create(amount=10000, currency="ETB",methods=["telebirr"], success_url="https://...", cancel_url="https://...")print(payment.checkout_url)