from faststripe.core import StripeApi
from faststripe.page import pages
import os
sapi = StripeApi(os.environ['STRIPE_SECRET_KEY'])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
subscription = sapi.subscription(
product_name='Pro Plan',
amount_cents=1999, # $19.99/month
success_url='https://yoursite.com/welcome',
cancel_url='https://yoursite.com/pricing',
interval='month'
)
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)
annual_sub = sapi.subscription(
product_name='Pro Plan',
amount_cents=19999, # $199.99/year (save $39.89)
success_url='https://yoursite.com/welcome',
cancel_url='https://yoursite.com/pricing',
interval='year'
)
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
subscription = sapi.subscription(
product_name='Pro Plan',
amount_cents=1999,
success_url='https://yoursite.com/welcome',
cancel_url='https://yoursite.com/pricing',
customer_email='[email protected]'
)
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
product = sapi.products.post(
name='Enterprise Plan',
description='Full access to all features'
)
# Create recurring price
price = sapi.prices.post(
product=product.id,
unit_amount=9999, # $99.99
currency='usd',
recurring={
'interval': 'month',
'usage_type': 'licensed' # per-seat billing
}
)
# Create subscription checkout
checkout = sapi.checkout.sessions_post(
mode='subscription',
line_items=[{
'price': price.id,
'quantity': 1
}],
success_url='https://yoursite.com/success?session_id={CHECKOUT_SESSION_ID}',
cancel_url='https://yoursite.com/cancel',
allow_promotion_codes=True, # Enable promo 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
subscriptions = pages(sapi.subscriptions.get, limit=100)
print(f"Found {len(subscriptions)} subscriptions")
# Get a specific subscription
sub = subscriptions[0]
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