Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.mavera.io/llms.txt

Use this file to discover all available pages before exploring further.

Scenario

Analyze charges with expanded payment methods to extract billing countries. Aggregate by region and create Mavera personas for each market, enriched with language preferences and cultural context.

Architecture

Code

import os, time, stripe, requests
from collections import defaultdict

stripe.api_key = os.environ["STRIPE_API_KEY"]
MAVERA_API_KEY = os.environ["MAVERA_API_KEY"]
MAVERA_BASE = "https://app.mavera.io/api/v1"

REGION_MAP = {"US": "North America", "CA": "North America", "MX": "North America",
    "GB": "Europe", "DE": "Europe", "FR": "Europe", "NL": "Europe",
    "JP": "Asia-Pacific", "AU": "Asia-Pacific", "SG": "Asia-Pacific",
    "BR": "Latin America", "AR": "Latin America", "CO": "Latin America"}

def aggregate_by_region():
    regions = defaultdict(lambda: {"count": 0, "revenue": 0, "countries": defaultdict(int)})
    for ch in stripe.Charge.list(limit=100, expand=["data.payment_method"]).auto_paging_iter():
        country = (ch.payment_method_details or {}).get("card", {}).get("country") or \
                  ((ch.billing_details or {}).get("address") or {}).get("country")
        if not country:
            continue
        region = REGION_MAP.get(country, "Other")
        regions[region]["count"] += 1
        regions[region]["revenue"] += ch.amount / 100
        regions[region]["countries"][country] += 1
    return dict(regions)

def create_market_persona(region, stats):
    top = sorted(stats["countries"].items(), key=lambda x: -x[1])[:5]
    resp = requests.post(f"{MAVERA_BASE}/personas", json={
        "name": f"{region} Market Persona",
        "description": f"{region}: {stats['count']} charges, ${stats['revenue']:,.0f} revenue. Top: {', '.join(c for c, _ in top)}.",
        "attributes": {"region": region, "total_charges": stats["count"],
                        "total_revenue": round(stats["revenue"], 2), "country_breakdown": dict(top)},
    }, headers={"Authorization": f"Bearer {MAVERA_API_KEY}"})
    resp.raise_for_status()
    return resp.json()

for region, stats in aggregate_by_region().items():
    if stats["count"] >= 10:
        r = create_market_persona(region, stats)
        print(f"Created: {r['id']}{region} ({stats['count']} charges)")
        time.sleep(0.2)

Example Output

{
  "id": "per_g7h8i9j0",
  "name": "Europe Market Persona",
  "description": "Customers from Europe: 312 charges, $87,430 total revenue. Top: GB, DE, FR, NL, ES.",
  "attributes": { "region": "Europe", "total_charges": 312, "total_revenue": 87430.0,
    "country_breakdown": { "GB": 124, "DE": 89, "FR": 52, "NL": 31, "ES": 16 } },
  "created_at": "2026-03-17T14:35:12Z"
}

Error Handling

Legacy charges may lack payment_method_details. Fall back to billing_details.address.country and skip charges with no geographic signal.
Use PUT to update existing personas, or append a timestamp for versioned snapshots (e.g., "Europe Market Persona — 2026-03").