Public
Likestripe-sub-template
Val Town is a collaborative website to build and scale JavaScript apps.
Deploy APIs, crons, & store data โ all from the browser, and deployed in milliseconds.
Viewing readonly version of main branch: v29View latest version
A complete, remixable template for gating content behind a Stripe subscription on Val Town.
| Layer | How |
|---|---|
| Auth | Google Sign-In via LastLogin โ zero API keys needed |
| Payments | Stripe Checkout (Apple Pay, Google Pay, all cards โ automatic) |
| Billing Portal | Stripe Customer Portal for self-serve manage / cancel |
| Database | Val Town SQLite โ email โ stripe_customer_id โ subscription_status |
| Webhooks | Handles checkout.session.completed, subscription.updated, subscription.deleted |
Visitor โ Landing page โ Sign In (Google) โ Paywall โ Stripe Checkout โ Premium Dashboard
โ
Stripe Customer Portal
(manage / cancel)
2. In Stripe Dashboard:
- Create a Product with a recurring Price (e.g. $9/month)
- Create a Webhook โ
YOUR_VAL_URL/api/webhook- Events:
checkout.session.completed,customer.subscription.updated,customer.subscription.deleted
- Events:
- Enable Apple Pay / Google Pay in Settings โ Payment Methods
- Configure Customer Portal in Settings โ Billing โ Customer Portal
| Variable | Value |
|---|---|
STRIPE_SECRET_KEY | sk_test_... |
STRIPE_PRICE_ID | price_... |
STRIPE_WEBHOOK_SECRET | whsec_... |
main.tsx โ HTTP handler + routing (entry point)
db.ts โ SQLite database layer
stripe.ts โ Stripe API helpers (checkout, portal, webhooks)
styles.ts โ All CSS (design tokens, page-specific styles)
pages/
landing.tsx โ Public landing page with pricing
dashboard.tsx โ Premium content (subscribers) / paywall (non-subscribers)
setup.tsx โ Setup instructions (shown when env vars are missing)
| Route | Method | Purpose |
|---|---|---|
/ | GET | Landing / Dashboard / Setup (context-dependent) |
/api/checkout | POST | Creates Stripe Checkout Session |
/api/portal | POST | Creates Stripe Customer Portal Session |
/api/webhook | POST | Receives Stripe webhook events |
/api/user | GET | Returns subscription status (JSON) |
/auth/login | GET | Google auth (LastLogin middleware) |
/auth/logout | GET | Logout (LastLogin middleware) |
Edit STRIPE_PRICE_ID env var. The $9/mo shown in the UI is just placeholder text in pages/dashboard.tsx and pages/landing.tsx.
Edit PremiumContent component in pages/dashboard.tsx.
Edit styles.ts โ all CSS variables and page styles are in one place.
Store tier info in the plan_name column and gate content by tier instead of just active/inactive.
Stripe Checkout automatically shows these buttons when:
- The customer's browser/device supports them
- You've enabled them in Stripe Dashboard โ Settings โ Payment Methods
No extra code needed.