PyPI: kkhayv1.0.4Zero Dependencies
Python SDK
The official Python SDK for K Khay Gateway. Built strictly on Python's standard library (urllib.request, hmac, hashlib, json, dataclasses). Compatible with Python 3.9+, FastAPI, Django, Flask, and Celery workers.
1. Installation
Python Package Manager
$pip install kkhay
2. Client Initialization
Initialize KkhayClient with your secret API key from the gateway dashboard.
main.py
import osfrom kkhay import KkhayClient # Initialize client with your API keykkhay = KkhayClient( api_key=os.environ.get("KKHAY_API_KEY", "kkhay_live_..."), timeout=10, # Request timeout in seconds (default: 10))3. Creating Invoices
create_invoice.py
# Create an invoice for $50.00 USDinvoice = kkhay.create_invoice( amount=50.00, currency="USD", pay_network="bsc", # Optional pre-selection (bsc, base, sol, btc...) pay_token="USDT", # Optional token (USDT, USDC, BTC, ETH...) order_id="ORDER-5011", # Your internal order reference title="Annual Pro Membership", customer_email="developer@example.com", redirect_url="https://yourshop.com/checkout/success", cancel_url="https://yourshop.com/cart",) print(f"Invoice ID: {invoice.id}")print(f"Checkout URL: {invoice.hosted_url}")# Redirect customer to: invoice.hosted_url4. Webhook Verification (FastAPI)
Verify x-kkhay-signature using timing-safe HMAC-SHA256 via hmac.compare_digest:
webhook_handler.py
from fastapi import FastAPI, Request, HTTPException, Headerimport osfrom kkhay import parse_webhook_event, KkhaySignatureVerificationError app = FastAPI()WEBHOOK_SECRET = os.environ["KKHAY_WEBHOOK_SECRET"] @app.post("/api/webhooks/kkhay")async def handle_kkhay_webhook( request: Request, x_kkhay_signature: str = Header(None)): raw_body = await request.body() try: event = parse_webhook_event( payload=raw_body.decode("utf-8"), signature=x_kkhay_signature, secret=WEBHOOK_SECRET, ) except KkhaySignatureVerificationError: raise HTTPException(status_code=400, detail="Invalid signature") if event.event in ("payment.finished", "payment.paid"): print(f"Order {event.order_id} fulfilled on-chain: {event.tx_hash}") # Fulfill customer order in database return {"status": "ok"}