Overview
The SnapPay Python SDK provides a comprehensive, async-first interface for integrating payment processing, subscription management, and real-time event streaming into your Python applications. Built with modern asyncio patterns and type safety, it offers seamless integration with SnapPayβs platform. Requirements: Python 3.8+Key Features
- Async-first design with aiohttp for optimal performance
- Context manager support for automatic session management
- Real-time event streaming via Server-Sent Events (SSE)
- Full type safety with TypedDict definitions
- Service-based architecture for organized functionality
- Automatic retry logic and error handling
- Environment-based configuration
Installation
Configuration & Initialization
The SDK is configured through environment variables and the clientβs constructor. All operations require proper authentication and should use the async context manager pattern for optimal resource management.API Key Authentication
Authentication is handled via an API key that must start withpk_test_ (for testing) or pk_live_ (for production). Configure your API key in one of these ways:
Environment Variable (Recommended):
Core Methods
All methods are async coroutines and must be called withawait. The SDK uses a service-based architecture where methods are organized into logical groups: customers, checkout, access, and usage.
Customer Management
customers.get
Retrieves or creates a customer record (upsert logic).customer_id(str): Customer identifieremail(Optional[str]): Customer email addressname(Optional[str]): Customer full name
Customer object with:
customer_id(str): SnapPay customer IDemail(str): Customer emailname(Optional[str]): Customer name
Checkout Sessions
checkout.create_session
Creates a payment checkout session URL for customer purchases.customer_id(str): SnapPay customer IDproduct_id(str): Product ID from your SnapPay dashboardsuccess_url(str): URL to redirect after successful paymentcancel_url(Optional[str]): URL to redirect on cancellationprice_id(Optional[str]): The specific price you want to create the checkout session with
CheckoutSession object with:
session_id(str): Unique session identifierurl(str): Checkout URL for customerexpires_at(str): Session expiration timestamp (additional fields may be present depending on server response)
Access Control
access.check
Checks if a customer has access to a specific feature based on their subscription and usage limits.customer_id(str): SnapPay customer IDfeature_id(str): Feature identifier
AccessCheck object with:
has_access(bool): Whether customer has accessfeature_id(str): Feature being checkedusage(Optional[int]): Total usage (null if the feature is unlimited/no access)allowance(Optional[int]): Maximum usage allowed (null if the feature is unlimited/no access)next_reset_at(Optional[int]): Next reset timestamp (null if the feature is unlimited/no access)
Usage Tracking
usage.track
Reports usage for a metered feature. This action is idempotent to prevent duplicate tracking.customer_id(str): SnapPay customer IDfeature_id(str): Feature identifier for usage trackingusage(int): Usage amount to trackidempotency_key(Optional[str]): Prevents duplicate tracking (default to a random UUID if not set)
TrackUsageResponse object with tracking confirmation details.
usage.get
Retrieves current usage details for a customerβs feature.customer_id(str): SnapPay customer IDfeature_id(str): Feature identifier
GetUsageResponse objects. Each object will contain:
total_usage(int): The cumulative amount of the feature used by the customer.product_id(str): Product identifier associated with the usage.feature_id(str): Specific feature identifier for which the usage is tracked.remaining(Optional[int]): The remaining amount of the feature usage allowed.limit(Optional[int]): The maximum usage limit set for the feature.next_reset_at(Optional[str]): Timestamp indicating when the usage will reset.
Real-time Event Handling
SnapPay provides real-time events via Server-Sent Events (SSE). Subscribe to standardized backend events without managing webhooks.π« What You DONβT Need:
- No webhook endpoints to create and maintain
- No webhook signature verification
- No webhook retry logic or failure handling
- No webhook security concerns
- No debugging webhook delivery issues
β What You GET:
- Real-time events delivered instantly to your application
- Guaranteed delivery with automatic reconnection
- Clean, structured data - no raw webhook payloads
- Multiple consumption patterns to fit your architecture
- Type-safe event objects with full IDE support