How to Automate Your Stripe Subscription Tracking with a Simple Script

If you’re running a subscription product, you probably spend a chunk of time every week checking who’s active, who’s churned, and what your MRR looks like. You log into Stripe, click through the dashboard, maybe export a CSV. It’s not terrible, but it’s a distraction from building your product.

Here’s a better way: a script that pulls your subscription data automatically, summarizes it into a neat report, and sends it to your inbox. You set it up once, and it runs on a schedule — every Monday at 9am, for example. No more manual checking.

### The script in a nutshell

We wrote a Python script that uses the Stripe API to fetch all active subscriptions, calculate MRR, and detect recent cancellations. It outputs a plain-text summary that’s easy to glance at. You can run it manually, or set up a cron job to execute it weekly.

The script is about 80 lines, uses only the `requests` library, and doesn’t require any fancy setup. You’ll need your Stripe API key and a way to send email (we’ll use SendGrid’s API, but you can swap in anything else).

### Step 1: Get your Stripe API key

Log into your Stripe dashboard, go to Developers > API keys, and copy your secret key. Store it in an environment variable — don’t hardcode it in the script. If you’re using a version control system, you don’t want to commit that key.

### Step 2: Set up the script

Here’s a bare-bones version of the script. I’ve included comments so you can see what each section does. You’ll need to replace `YOUR_API_KEY` and `YOUR_SENDGRID_KEY` with your actual keys.

“`python
# subscription_report.py
import os
import requests
from datetime import datetime, timedelta

stripe_key = os.environ.get(‘STRIPE_API_KEY’)
sendgrid_key = os.environ.get(‘SENDGRID_API_KEY’)

# Fetch subscriptions from Stripe
response = requests.get(
‘https://api.stripe.com/v1/subscriptions’,
headers={‘Authorization’: f’Bearer {stripe_key}’},
params={‘status’: ‘active’, ‘limit’: 100}
)
subscriptions = response.json()[‘data’]

# Calculate MRR (assuming all plans are monthly; adjust as needed)
mrr = sum(sub[‘plan’][‘amount’] for sub in subscriptions if sub[‘plan’][‘interval’] == ‘month’)

# Detect cancellations in the last 7 days
week_ago = datetime.now() – timedelta(days=7)
canceled = [sub for sub in subscriptions if sub[‘canceled_at’] and datetime.fromtimestamp(sub[‘canceled_at’]) > week_ago]

# Compose email body
body = f”Active subs: {len(subscriptions)}nMRR: ${mrr/100:.2f}nCancelled this week: {len(canceled)}”

# Send email via SendGrid
requests.post(
‘https://api.sendgrid.com/v3/mail/send’,
headers={‘Authorization’: f’Bearer {sendgrid_key}’},
json={
‘personalizations’: [{‘to’: [{’email’: ‘[email protected]’}]}],
‘from’: {’email’: ‘[email protected]’},
‘subject’: ‘Weekly Subscription Report’,
‘content’: [{‘type’: ‘text/plain’, ‘value’: body}]
}
)

print(‘Report sent’)
“`

### Step 3: Schedule it with cron

Open your crontab (`crontab -e`) and add a line like this to run every Monday at 9am:

“`
0 9 * * 1 cd /path/to/script && python subscription_report.py
“`

Make sure the script is executable and that the Python environment has the `requests` library installed. If you’re not familiar with cron, there are dozens of tutorials online.

### What this saves you

Once this script is running, you stop logging into Stripe just to check numbers. You get a clean summary in your inbox. If you want to extend it, you can add graphs, compare week-over-week, or send it to your whole team.

This is a classic example of the 80/20 rule: 20% of the effort gives you 80% of the benefit. You don’t need a full analytics suite; you just need the essential numbers in your inbox.

### Take it further

If you want to auto-sync to Google Sheets or post to Slack, those are just a few lines away. I’ve written a version that does exactly that, and it’s available in the Kintly library. But even if you stick with the email version, you’re saving yourself 15 minutes every week.

That’s 13 hours a year. What could you do with that?