Aziz Khan
← Writing

Build the SaaS before you have the API keys

ArchitectureSaaSAI

Pavly is social-first commerce for small shops that sell on Instagram and WhatsApp. It depends on a lot of outside services: Instagram, WhatsApp Cloud API, Razorpay, Shiprocket, MSG91 for OTPs, S3 storage, DNS checks for custom domains, image background removal and Claude for AI features.

I didn't want the product to wait on any of them. Several need business accounts or app reviews before you get production keys. So every integration became an adapter, and every adapter defaults to a mock.

The pattern

Each integration lives in one folder with three things: an interface, a mock and the real implementation. One environment variable picks which one runs:

WHATSAPP_ADAPTER=mock      # or cloud
PAYMENTS_ADAPTER=mock      # or razorpay
SHIPPING_ADAPTER=mock      # or shiprocket

This bought me a lot:

  • The whole product works locally with pnpm dev and seed data. No accounts, no sandboxes, no waiting.
  • Tests are fast and deterministic, because business logic talks to the interface and the tests run against mocks.
  • Going live is config, not code. Switching a provider from mock to real is one variable.
  • Demos never break because a third-party sandbox is down.

Guardrails so mocks don't reach production

Mocks are dangerous if they ship by accident, so the environment is validated with zod at startup and the app refuses to boot on invalid config. There's an explicit production guard on secrets too. Failing at boot is much cheaper than finding out from a customer that payments were mocked.

Rules before AI

The same thinking applies to AI. When a customer comments on a reel, Pavly needs to know if they're asking about price or wanting to buy. Many comments are predictable, like "price?" or "how much". So keyword packs run first. They're free, instant and easy to debug.

Only when no keyword matches does the comment go to a small, fast model. That's how a comment like "ye kitne ka?" (Hindi for "how much is this?") still gets understood when no keyword pack caught it. The model is a fallback, not the first step.

Jobs that fit serverless

Background work like cancelling unconfirmed COD orders, expiring reservations and abandoned-cart reminders runs from a Postgres-backed queue. It can run as a long-lived pnpm worker, or a cron can hit an internal endpoint every minute, where each call does one tick of at most 45 seconds. Same code, so it runs on a VPS or a serverless host.

Why it matters early

Founders often wait for integrations before building the product around them. Adapters flip that. You build and sell the real workflow first, and plug in providers as approvals come through. The interface you write on day one also makes switching providers later a contained change instead of a rewrite.