import os, time, stripe, requests
stripe.api_key = os.environ["STRIPE_API_KEY"]
MAVERA_API_KEY = os.environ["MAVERA_API_KEY"]
MAVERA_BASE = "https://app.mavera.io/api/v1"
def find_growth_customers(min_months=12):
candidates = []
for cust in stripe.Customer.list(limit=100).auto_paging_iter():
paid = sorted(stripe.Invoice.list(customer=cust.id, limit=100, status="paid").data,
key=lambda i: i.created)
if len(paid) < min_months:
continue
first, latest = paid[0].amount_paid / 100, paid[-1].amount_paid / 100
if latest > first * 1.2:
candidates.append({"name": cust.name or cust.email, "months_active": len(paid),
"first_invoice": first, "latest_invoice": latest,
"growth_pct": round((latest - first) / first * 100, 1)})
time.sleep(0.05)
return sorted(candidates, key=lambda c: -c["growth_pct"])[:10]
def draft_case_study(d):
resp = requests.post(f"{MAVERA_BASE}/mave/chat", json={
"input": [
{"role": "system", "content": "You are a B2B SaaS marketing writer."},
{"role": "user", "content": f"Draft a 200-word case study for {d['name']}. "
f"Started at ${d['first_invoice']}/mo, grew to ${d['latest_invoice']}/mo over "
f"{d['months_active']} months ({d['growth_pct']}% growth). "
"Include headline, challenge, solution, results."},
],
"max_tokens": 1000,
}, headers={"Authorization": f"Bearer {MAVERA_API_KEY}"})
resp.raise_for_status()
return resp.json()
for cust in find_growth_customers()[:3]:
result = draft_case_study(cust)
print(f"\n--- {cust['name']} ---\n{result['choices'][0]['message']['content']}")
time.sleep(0.5)