Back to templates

Template details

ai-saas-starter

Free MIT Next.js 16 AI SaaS boilerplate with Better Auth, Stripe subscriptions, an append-only credits ledger, and AI image generation.

About this template

A free, MIT-licensed Next.js 16 SaaS starter kit that ships the money paths — Better Auth sessions, Stripe subscriptions, and an append-only credits ledger — finished and tested, so you build your product on top instead of rebuilding billing plumbing.

What is ai-saas-starter?

ai-saas-starter is an open-source SaaS boilerplate built on Next.js 16, React 19, and strict TypeScript by Nikandr Surkov. It takes a prompt as input on its AI generation page and produces an image stored in Vercel Blob (or a local .generated folder in dev), with a credits ledger tracking every spend, refund, and grant. Auth, billing, and the database run in your own PostgreSQL instance. The live demo site is fully functional, and the code is a free GitHub repo under the MIT license.

Key Features

  • Next.js 16 + React 19 + strict TypeScript — Server Components by default, Server Actions for mutations, and API routes only for the Stripe webhook and Better Auth handler.
  • Better Auth with Postgres sessions — email/password with verification, Google, GitHub, and magic links, plus a settings page for password change, account linking, and cascade-safe account deletion that cancels the Stripe subscription first.
  • Stripe billing — Checkout and Customer Portal as server actions, a raw-body signature-verified webhook, and a single syncStripeDataToDb function as the only writer of subscription state; plans and prices live in one config file.
  • Append-only credits ledger — credit_transactions rows with UNIQUE idempotency keys (grant_{invoiceId}, topup_{sessionId}); spends are one conditional UPDATE that refuses negative balances; database CHECK constraints reject zero-amount rows.
  • AI image generation loop — prompt → 10/min rate limit → spend 1 credit → AI SDK 6 generateImage → storage → history grid, with automatic refund on provider failure.
  • Testing — 94 Vitest unit/integration tests against real Postgres (double-spend race, idempotent replays, refunds, webhook HMAC fixtures) and 12 Playwright e2e tests.
  • Tailwind v4 + shadcn/ui design system — light and dark themes with tokens documented in DESIGN.md and a dev-only /styleguide page.
  • AI-native repo context — AGENTS.md, CLAUDE.md, path-scoped .claude/rules/, skills like /add-feature, Prettier and typecheck hooks, and a read-only code-reviewer agent.

Who is it for?

  • Indie founders and solo developers who want auth, subscriptions, and credit metering already wired before writing their first product feature.
  • Builders using Claude Code, Cursor, Codex, Copilot, or Gemini CLI who need an agent-briefed repo that resists breaking billing code during vibe-coding sessions.
  • Developers evaluating SaaS starters who want a free, MIT alternative focused on a tested ledger and AI loop rather than admin panels or i18n.

What can you do with ai-saas-starter?

  • Credit-metered AI products: charge one credit per generated image, with refunds handled automatically when a provider call fails.
  • Subscription SaaS: use the three test-mode prices (Pro monthly, Ultra monthly, one-time top-up) as a template for plan cards rendered from a single config file.
  • Local prototyping: set AI_MOCK=true to run generation offline for free, and type FAIL in a prompt to demo the refund path.
  • Production deployment: follow the Vercel + Neon guide, using the pooled connection string at runtime and the direct one for migrations.

How does ai-saas-starter work?

Clone the repo, run pnpm install, start Postgres 17 via Docker Compose, copy .env.example to .env, run pnpm db:migrate, then pnpm dev. Missing keys degrade gracefully: no Stripe keys shows a setup card, no Resend key makes emails no-ops, and missing OAuth keys hide those buttons.

Pricing

Free under the MIT license. A separate Pro version from the same author lists rollover credits, a durable Inngest job pipeline, video and audio providers, R2 gallery storage, teams, admin, and an MCP server; per the README, the free repo is complete and nothing in it is crippled.

FAQ

Is ai-saas-starter free?

Yes. The repository is MIT-licensed and free to clone. Legal pages ship as placeholders, and the product on top is yours to build.

Can I use a different Postgres provider or AI model?

Any provider with a postgres:// URL works, including Neon, Supabase, and RDS. Swap image models with one env string, AI_IMAGE_MODEL=provider/model, and add providers by implementing one interface.

Why is image generation mocked by default?

So a fresh clone runs offline and free via AI_MOCK=true, which is refused in production builds. Set AI_GATEWAY_API_KEY and AI_MOCK=false for real images.

Screenshot of ai-saas-starter