Guide
Python integration guide for creating and paying a YKNPG transaction.
Python integration guide for creating and paying a YKNPG transaction.
Prerequisites
- Python 3.10+, internet access to your YKNPG API.
- A bearer token stored as
YKNPG_API_TOKEN(use.env, never hard-code). requestsandpython-dotenvinstalled.
- Create project folder
mkdir -p demo_python && cd demo_pythonpython -m venv .venv && source .venv/bin/activate
- Install dependencies
pip install requests python-dotenv
- Add environment file
- File:
.env
YKNPG_API_TOKEN=replace-with-your-token
YKNPG_BASE_URL=https://yknpg.ngoul.com/api/v1
- Create transaction script
- File:
create_transaction.py
import os
import time
from dotenv import load_dotenv
import requests
load_dotenv()
API_TOKEN = os.environ["YKNPG_API_TOKEN"]
BASE_URL = os.environ.get("YKNPG_BASE_URL", "https://yknpg.ngoul.com/api/v1")
def create_transaction(amount: int, phone: str) -> dict:
payload = {
"amount": amount,
"provider": "ORANGE_MONEY",
"user_infos": {"number": phone},
"callback_url": "http://example.com",
"callback_method": "POST",
"your_message": "pay this",
"your_order_ref": "demo-1",
"provider_fees_on_customer": True,
}
resp = requests.post(
f"{BASE_URL}/transactions/",
headers={"Authorization": f"Bearer {API_TOKEN}"},
json=payload,
timeout=10,
)
resp.raise_for_status()
return resp.json()
def pay_transaction(uuid: str) -> dict:
resp = requests.post(
f"{BASE_URL}/transactions/{uuid}/pay/",
headers={"Authorization": f"Bearer {API_TOKEN}"},
timeout=10,
)
resp.raise_for_status()
return resp.json()
def poll_status(uuid: str) -> str:
status = "new"
while status in {"new", "paying"}:
time.sleep(5)
resp = requests.get(
f"{BASE_URL}/transactions/{uuid}/",
headers={"Authorization": f"Bearer {API_TOKEN}"},
timeout=10,
)
resp.raise_for_status()
status = resp.json()["status"]
print(f"Current status: {status}")
return status
if __name__ == "__main__":
phone = input("Enter phone number: ")
amount = int(input("Enter amount: "))
transaction = create_transaction(amount, phone)
print("Gateway instructions:", transaction.get("instructions"))
paid = pay_transaction(transaction["uuid"])
print("Post-pay instructions:", paid.get("instructions"))
final_status = poll_status(transaction["uuid"])
print("Final status:", final_status)
- Run
python create_transaction.py- Follow the console prompts; share instructions with the payer.
Notes
- The flow mirrors
create_transaction_vapi: create → display instructions → pay → poll. - Adjust
providerandcallback_urlper project needs; keep secrets in.env.