SDKs
Python SDK
Official Python SDK for lomi. payments API.
Official Python SDK for the lomi. payments API. Works with Django, Flask, FastAPI, and any Python 3.9+ application.
Installation
pip install lomi-sdkOr with Poetry:
poetry add lomi-sdkQuick Start
import os
from lomi import LomiClient
client = LomiClient(
api_key=os.environ["LOMI_API_KEY"],
environment="test" # 'test' for sandbox, 'live' for production
)Environments:
'test'→https://sandbox.api.lomi.africa'live'→https://api.lomi.africa
Payment Examples
Create a Checkout Session
session = client.checkout_sessions.create({
"amount": 10000,
"currency_code": "XOF",
"title": "Premium Subscription",
"description": "Monthly access to premium features",
"customer_email": "customer@example.com",
"success_url": "https://yoursite.com/success",
"cancel_url": "https://yoursite.com/cancel",
"metadata": {"order_id": "ORD-123"}
})
print(f"Redirect to: {session['checkout_url']}")Create a Payment Link
link = client.payment_links.create({
"link_type": "product",
"title": "Pro Plan",
"currency_code": "XOF",
"product_id": "prod_abc123...",
"allow_coupon_code": True
})
print(f"Share this link: {link['url']}")List Transactions with Filters
transactions = client.transactions.list(
status="completed",
provider="WAVE",
startDate="2024-01-01T00:00:00Z",
pageSize=50
)
for tx in transactions:
print(f"{tx['id']}: {tx['gross_amount']} {tx['currency_code']}")Customer Management
Create a Customer
customer = client.customers.create({
"name": "Fatou Diop",
"email": "fatou@example.com",
"phone_number": "+221771234567",
"country": "Senegal",
"city": "Dakar",
"metadata": {"source": "website"}
})
print(f"Customer ID: {customer['id']}")Get Customer Transactions
transactions = client.customers.get_transactions("cus_abc123...")Products & Subscriptions
Create a Product
product = client.products.create({
"name": "Premium Plan",
"description": "Full access to all features",
"product_type": "recurring",
"prices": [
{
"amount": 15000,
"currency_code": "XOF",
"billing_interval": "month",
"is_default": True
}
],
"trial_enabled": True,
"trial_period_days": 7
})Add a New Price
price = client.products.add_price("prod_abc123...", {
"amount": 150000,
"currency_code": "XOF",
"billing_interval": "year"
})Cancel a Subscription
cancelled = client.subscriptions.cancel("sub_abc123...", {
"cancel_at_period_end": True,
"reason": "Customer request"
})Payouts
Initiate a Withdrawal
payout = client.payouts.create({
"amount": 100000,
"currency_code": "XOF",
"payout_method_id": "pm_abc123...",
"description": "Monthly withdrawal"
})Pay a Beneficiary
beneficiary_payout = client.beneficiary_payouts.create({
"amount": 50000,
"currency_code": "XOF",
"beneficiary_phone": "+221771234567",
"beneficiary_name": "Contractor Name",
"provider_code": "WAVE"
})Account & Organization
Get Account Balance
balances = client.accounts.get_balance()
xof_balance = client.accounts.get_balance(currency="XOF")
print(f"Available: {xof_balance['available']}")Get Organization Metrics
metrics = client.organizations.get_metrics()
print(f"MRR: {metrics['mrr']}")
print(f"Total Customers: {metrics['total_customers']}")Error Handling
from lomi import LomiClient, LomiError, LomiNotFoundError
try:
customer = client.customers.get("invalid_id")
except LomiNotFoundError:
print("Customer not found")
except LomiError as e:
print(f"API Error [{e.status_code}]: {e.message}")Django Integration
# settings.py
LOMI_API_KEY = os.environ.get("LOMI_API_KEY")
LOMI_WEBHOOK_SECRET = os.environ.get("LOMI_WEBHOOK_SECRET")
# views.py
import hmac
import hashlib
import json
from django.http import JsonResponse
from django.views.decorators.csrf import csrf_exempt
from django.conf import settings
@csrf_exempt
def lomi_webhook(request):
signature = request.headers.get("x-lomi-signature")
expected = hmac.new(
settings.LOMI_WEBHOOK_SECRET.encode(),
request.body,
hashlib.sha256
).hexdigest()
if not hmac.compare_digest(signature, expected):
return JsonResponse({"error": "Invalid signature"}, status=400)
event = json.loads(request.body)
if event["type"] == "PAYMENT_SUCCEEDED":
# Handle successful payment
transaction = event["data"]
print(f"Payment received: {transaction['gross_amount']}")
return JsonResponse({"received": True})Flask Integration
import hmac
import hashlib
import os
from flask import Flask, request, jsonify
app = Flask(__name__)
@app.route("/webhook", methods=["POST"])
def webhook():
signature = request.headers.get("x-lomi-signature")
secret = os.environ["LOMI_WEBHOOK_SECRET"].encode()
expected = hmac.new(secret, request.data, hashlib.sha256).hexdigest()
if not hmac.compare_digest(signature, expected):
return jsonify({"error": "Invalid signature"}), 400
event = request.json
if event["type"] == "PAYMENT_SUCCEEDED":
# Handle payment
pass
return jsonify({"received": True})Available Services
| Service | Methods |
|---|---|
accounts | list, get, get_balance, get_balance_breakdown, check_balance |
beneficiary_payouts | list, get, create |
checkout_sessions | list, get, create |
customers | list, get, create, update, delete, get_transactions |
discount_coupons | list, get, create, get_performance |
organizations | list, get, get_metrics |
payment_links | list, get, create |
payment_requests | list, get, create |
payouts | list, get, create |
products | list, get, create, add_price, set_default_price |
refunds | list, get, create |
subscriptions | list, get, get_by_customer, cancel |
transactions | list, get |
webhook_delivery_logs | list, get |
webhooks | list, get, create, update, delete |