async-stripe
async-stripe is an asynchronous wrapper around Stripe's official `stripe-python` library, allowing its use in `asyncio` environments without blocking the event loop. It mirrors the `stripe-python` API, making all methods awaitable. The current version is 6.1.0, and its release cadence closely follows major and minor updates of `stripe-python`.
Warnings
- breaking Major versions of `async-stripe` align with major versions of `stripe-python`. Upgrading `async-stripe` may introduce breaking changes inherited from `stripe-python`. Always review `stripe-python`'s changelog when upgrading `async-stripe`'s major version.
- gotcha All Stripe API calls via `StripeClient` methods are coroutines and *must* be `await`ed. Forgetting `await` will result in a coroutine object being returned, not the actual API response, leading to silent failures or incorrect logic downstream.
- gotcha Avoid setting `stripe.api_key = '...'` globally in `asyncio` applications. This synchronous method is not thread-safe or async-safe and can lead to race conditions or incorrect API key usage in concurrent requests.
- gotcha Do not attempt to mix direct `stripe` module imports (e.g., `from stripe import Customer`) with `async_stripe.StripeClient` operations within the same async context. `async-stripe` wraps the underlying `stripe` objects and makes their methods awaitable. Direct `stripe` module usage will be synchronous and block the event loop.
Install
-
pip install async-stripe
Imports
- StripeClient
from async_stripe import StripeClient
Quickstart
import asyncio
import os
from async_stripe import StripeClient
async def main():
# It's recommended to load API key from environment variables
api_key = os.environ.get('STRIPE_API_KEY', 'sk_test_...')
# Use async with for proper resource management
async with StripeClient(api_key=api_key) as stripe:
try:
customer = await stripe.Customer.create(email="test_user@example.com", description="Test Customer")
print(f"Customer created: {customer.id}")
# Example: Retrieve the customer
retrieved_customer = await stripe.Customer.retrieve(customer.id)
print(f"Retrieved customer email: {retrieved_customer.email}")
except Exception as e:
print(f"An error occurred: {e}")
if __name__ == "__main__":
asyncio.run(main())