Skip to main content

A Python wrapper for the Paystack API

Project description

paystackpay

A Python wrapper for the Paystack API. Supports both sync and async usage.

Installation

pip install paystackpay

Quickstart

from paystackpay import Paystack

client = Paystack(secret_key="sk_live_...")

# Initialize a transaction
response = client.transactions.initialize(
    email="customer@example.com",
    amount=100.0,  # GHS 100 — pass amounts in major units
    currency="GHS",
)
print(response["data"]["authorization_url"])

Async

import asyncio
from paystackpay import AsyncPaystack

async def main():
    client = AsyncPaystack(secret_key="sk_live_...")
    response = await client.transactions.initialize(
        email="customer@example.com",
        amount=100.0,
        currency="GHS",
    )
    print(response["data"]["authorization_url"])

asyncio.run(main())

Context managers

# Sync
with Paystack(secret_key="sk_live_...") as client:
    client.transactions.list()

# Async
async with AsyncPaystack(secret_key="sk_live_...") as client:
    await client.transactions.list()

Amounts

Pass amounts in the major currency unit (e.g. 100.0 = GHS 100 / NGN 100). The library converts to the subunit (pesewas/kobo) before sending to Paystack.

Resources

All resources are available as attributes on the client. Every sync method has an identical async counterpart on AsyncPaystack.

Transactions

client.transactions.initialize(email, amount, currency)
client.transactions.verify(reference)
client.transactions.list()
client.transactions.fetch(transaction_id)
client.transactions.charge_authorization(email, amount, authorization_code, currency)
client.transactions.timeline(id_or_reference)
client.transactions.totals()
client.transactions.export()
client.transactions.partial_debit(authorization_code, email, amount, currency)

Customers

client.customers.create(email, first_name, last_name, phone)
client.customers.list()
client.customers.fetch(email_or_code)
client.customers.update(customer_code, first_name=None, last_name=None, email=None, phone=None)
client.customers.whitelist(customer_code)
client.customers.blacklist(customer_code)
client.customers.deactivate_authorization(authorization_code)

Plans

client.plans.create(name, interval, amount, currency)
client.plans.list()
client.plans.fetch(id_or_code)
client.plans.update(id_or_code, name=None, interval=None, amount=None)

Transfers

client.transfers.initiate(amount, recipient, reason, currency)
client.transfers.finalize(transfer_code, otp)
client.transfers.fetch(id_or_code)
client.transfers.list()

Transfer Recipients

client.transfer_recipients.create(type, name, account_number, bank_code, currency)
client.transfer_recipients.list()
client.transfer_recipients.fetch(id_or_code)
client.transfer_recipients.update(id_or_code, name=None, email=None)
client.transfer_recipients.delete(id_or_code)

Payment Requests

client.payment_requests.create(customer, description, amount, currency)
client.payment_requests.fetch(id_or_code)
client.payment_requests.list()
client.payment_requests.verify(id_or_code)
client.payment_requests.send_notification(id_or_code)

Refunds

client.refunds.create(transaction, amount=None, currency="NGN")
client.refunds.list()
client.refunds.fetch(refund_id)

Subaccounts

client.subaccounts.create(business_name, settlement_bank, account_number, percentage_charge)
client.subaccounts.list()
client.subaccounts.fetch(id_or_code)
client.subaccounts.update(id_or_code, **kwargs)

Webhooks

Verify the x-paystack-signature header on incoming webhook events — no HTTP call made.

payload = request.body  # raw bytes
signature = request.headers["x-paystack-signature"]

is_valid = client.webhooks.verify_signature(payload, signature, secret="your_webhook_secret")

if not is_valid:
    return HttpResponse(status=400)

Error Handling

All errors are subclasses of PaystackError and carry .message, .status_code, and .raw_response.

from paystackpay import Paystack, AuthenticationError, InvalidRequestError, PaystackError

client = Paystack(secret_key="sk_live_...")

try:
    client.transactions.initialize(email="bad", amount=100.0, currency="NGN")
except AuthenticationError as e:
    print(f"Bad key: {e.message}")
except InvalidRequestError as e:
    print(f"Bad params: {e.message}{e.raw_response}")
except PaystackError as e:
    print(f"Paystack error {e.status_code}: {e.message}")
Exception Status code
AuthenticationError 401
InvalidRequestError 400
NotFoundError 404
RateLimitError 429
APIError 5xx

Requirements

  • Python 3.10+
  • httpx
  • pydantic

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

paystackpay-1.0.1.tar.gz (10.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

paystackpay-1.0.1-py3-none-any.whl (13.0 kB view details)

Uploaded Python 3

File details

Details for the file paystackpay-1.0.1.tar.gz.

File metadata

  • Download URL: paystackpay-1.0.1.tar.gz
  • Upload date:
  • Size: 10.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for paystackpay-1.0.1.tar.gz
Algorithm Hash digest
SHA256 6534e818119cbca512723b011767178b22f38cc8eb87caa4de23ebbc048dff3f
MD5 9fda49be73cc106f0bc7e2b132eed5d9
BLAKE2b-256 614145bd416d62d217dd69912839be59068c45d44e2bfb20f684095fe3f71ee4

See more details on using hashes here.

Provenance

The following attestation bundles were made for paystackpay-1.0.1.tar.gz:

Publisher: publish.yml on aglili/paystackpay

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file paystackpay-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: paystackpay-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 13.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for paystackpay-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f7e25bd1426d3ae01b3fabba6f6b7df39922ec85512311680e8e8a0328f01ec9
MD5 d9ea77e905e406b6c95ab690a8b4cbc6
BLAKE2b-256 a3ac8c74e1dfcc7c3155f8264d1d0e4e0daece87b982dc22a200b977e5d3456e

See more details on using hashes here.

Provenance

The following attestation bundles were made for paystackpay-1.0.1-py3-none-any.whl:

Publisher: publish.yml on aglili/paystackpay

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page