from faststripe.core import StripeApi
from faststripe.page import pages
import os
= StripeApi(os.environ['STRIPE_SECRET_KEY']) sapi
How to Create Subscriptions
Set up recurring payments for services
This guide shows you how to create recurring subscription payments using FastStripe.
Problem
You want to charge customers on a recurring basis (monthly, yearly, etc.) for a service or product.
Solution
Use FastStripe’s subscription()
method which creates the necessary recurring price and checkout session.
Monthly subscription
# Create a monthly subscription
= sapi.subscription(
subscription ='Pro Plan',
product_name=1999, # $19.99/month
amount_cents='https://yoursite.com/welcome',
success_url='https://yoursite.com/pricing',
cancel_url='month'
interval
)
print(f"Subscription URL: {subscription.url[:64]}...")
Subscription URL: https://billing.answer.ai/c/pay/cs_test_a1q4GJfynTyGLxkOf1IehI9H...
Annual subscription with discount
# Create an annual subscription (typically discounted)
= sapi.subscription(
annual_sub ='Pro Plan',
product_name=19999, # $199.99/year (save $39.89)
amount_cents='https://yoursite.com/welcome',
success_url='https://yoursite.com/pricing',
cancel_url='year'
interval
)
print(f"Annual subscription URL: {annual_sub.url[:64]}...")
Annual subscription URL: https://billing.answer.ai/c/pay/cs_test_a1xC4H6OaKLZUl3HrQreiVLq...
Pre-fill customer email
# Pre-fill customer email in checkout
= sapi.subscription(
subscription ='Pro Plan',
product_name=1999,
amount_cents='https://yoursite.com/welcome',
success_url='https://yoursite.com/pricing',
cancel_url='[email protected]'
customer_email
)
print(f"Subscription with email: {subscription.url[:64]}...")
Subscription with email: https://billing.answer.ai/c/pay/cs_test_a167XV8A7Bxe4O5eZCYqxP2t...
Manual subscription setup
For more control over subscription features:
# Create product
= sapi.products.post(
product ='Enterprise Plan',
name='Full access to all features'
description
)
# Create recurring price
= sapi.prices.post(
price =product.id,
product=9999, # $99.99
unit_amount='usd',
currency={
recurring'interval': 'month',
'usage_type': 'licensed' # per-seat billing
}
)
# Create subscription checkout
= sapi.checkout.sessions_post(
checkout ='subscription',
mode=[{
line_items'price': price.id,
'quantity': 1
}],='https://yoursite.com/success?session_id={CHECKOUT_SESSION_ID}',
success_url='https://yoursite.com/cancel',
cancel_url=True, # Enable promo codes
allow_promotion_codes={
subscription_data'trial_period_days': 14 # 14-day free trial
}
)
print(f"Enterprise subscription: {checkout.url[:64]}...")
Enterprise subscription: https://billing.answer.ai/c/pay/cs_test_b1t6IouoLfFJ5XuLfM1BJsxj...
Managing existing subscriptions
Retrieve and modify existing subscriptions:
# List all subscriptions
= pages(sapi.subscriptions.get, limit=100)
subscriptions print(f"Found {len(subscriptions)} subscriptions")
# Get a specific subscription
= subscriptions[0]
sub print(f"Subscription {sub.id}: {sub.status}")
Found 228 subscriptions
Subscription sub_1S0qf0KGhqIw9PXmGUUxLL6S: active
Best practices
- Always use webhooks to handle subscription lifecycle events
- Consider offering free trials to increase conversions
- Use promotion codes for discounts and marketing campaigns
- Set up proper success/cancel URLs that match your user flow
- Test subscription flows thoroughly in test mode