# How to Create Subscriptions


<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->

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.

``` python
from faststripe.core import *
import os

sapi = StripeApi(os.environ['STRIPE_SECRET_KEY'])
```

## Monthly subscription

``` python
# Create a monthly subscription
subscription = sapi.subscription(
    product_name='Pro Plan',
    amount_cents=19_99,  # $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_a1YQn5kS4oYiNldiazJRxtoT...

## Annual subscription with discount

``` python
# Create an annual subscription (typically discounted)
annual_sub = sapi.subscription(
    product_name='Pro Plan',
    amount_cents=199_99,  # $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_a1Icxv2IwvHVBtZa66JCp1qm...

## Pre-fill customer email

``` python
# Pre-fill customer email in checkout
subscription = sapi.subscription(
    product_name='Pro Plan',
    amount_cents=19_99,
    success_url='https://yoursite.com/welcome',
    cancel_url='https://yoursite.com/pricing',
    customer_email='user@example.com'
)

print(f"Subscription with email: {subscription.url[:64]}...")
```

    Subscription with email: https://billing.answer.ai/c/pay/cs_test_a1uSlojJSlUQMOJMXpPFK8Cy...

## Manual subscription setup

For more control over subscription features:

``` python
# 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=99_99,  # $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_b14ZoLwpDsJuqFFV7AEu7jNO...

## Managing existing subscriptions

Retrieve and modify existing subscriptions:

``` python
# 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 181 subscriptions
    Subscription sub_1SwObZKGhqIw9PXmwQczCKzX: 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
