Python SDK

View as Markdown

The official Python SDK for PaymentKit.

Installation

$pip install payment-kit

Quick Start

1from payment_kit import PaymentKit
2
3client = PaymentKit(
4 token="YOUR_API_TOKEN",
5 base_url="https://app.paymentkit.com",
6)
7
8# Create a customer
9customer = client.customers.create_customer(
10 account_id="acc_prod_abc123",
11 email="customer@example.com",
12)
13
14# Create a subscription
15from datetime import datetime
16
17subscription = client.subscriptions.create_subscription(
18 account_id="acc_prod_abc123",
19 customer_id=customer.id,
20 period_start=datetime.now(),
21 items=[{"price_id": "prc_prod_xyz789", "quantity": 1}],
22)
23
24# List invoices
25invoices = client.invoices.list_invoices(
26 account_id="acc_prod_abc123",
27 limit=10,
28)

Environments

EnvironmentBase URL
Productionhttps://app.paymentkit.com
Staginghttps://staging.paymentkit.com
1# Production
2client = PaymentKit(
3 token="YOUR_API_TOKEN",
4 base_url="https://app.paymentkit.com",
5)
6
7# Staging
8client = PaymentKit(
9 token="YOUR_API_TOKEN",
10 base_url="https://staging.paymentkit.com",
11)

Async Client

The SDK provides an async client for non-blocking API calls:

1import asyncio
2from payment_kit import AsyncPaymentKit
3
4client = AsyncPaymentKit(
5 token="YOUR_API_TOKEN",
6 base_url="https://app.paymentkit.com",
7)
8
9async def main():
10 customer = await client.customers.create_customer(
11 account_id="acc_prod_abc123",
12 email="customer@example.com",
13 )
14 print(customer.id)
15
16asyncio.run(main())

Error Handling

1from payment_kit.core.api_error import ApiError
2
3try:
4 customer = client.customers.create_customer(...)
5except ApiError as e:
6 print(e.status_code)
7 print(e.body)

Configuration

Timeouts

Default timeout is 60 seconds. Override at client or request level:

1# Client-level timeout
2client = PaymentKit(
3 token="YOUR_API_TOKEN",
4 base_url="https://app.paymentkit.com",
5 timeout=30.0,
6)
7
8# Request-level timeout
9customer = client.customers.create_customer(
10 ...,
11 request_options={"timeout_in_seconds": 10}
12)

Retries

The SDK automatically retries requests with exponential backoff for:

  • 408 (Request Timeout)
  • 409 (Conflict)
  • 429 (Too Many Requests)
  • 5XX (Server Errors)
1# Configure max retries per request (default is 2)
2customer = client.customers.create_customer(
3 ...,
4 request_options={"max_retries": 3}
5)

Resources