All case studies

ShopKit PKProduct5 min read

ShopKit PK: a multi-store commerce platform for Pakistani sellers, built for cash on delivery

A platform that runs many online shops from one codebase, priced in rupees and designed around cash on delivery, with store templates that launch a new shop in about ten seconds.

Role
Founder and sole developer
Timeline
2026 – in progress
Stack
Next.jsExpressPostgreSQLPrismaTurborepoCloudflare R2
ShopKit PK: three store templates on phones
49
Store tables, all isolated by store_id
6
Store templates with sample products
~10 s
To create a new shop from a template

Context

Most small online sellers in Pakistan sell through Instagram and WhatsApp and get paid in cash when the parcel arrives. Global store builders charge in dollars, expect card payments, and need a stack of paid apps for things these sellers use every day.

ShopKit PK is my answer to that: not a feature-for-feature Shopify clone, but the easiest way for a Pakistani seller to run cash-on-delivery orders, priced in rupees, without paying for ten apps in dollars. The first shop it was built for is Silken, a clothing brand.

The problem

Run many independent shops, each on its own domain and with its own design, from one codebase, without ever letting one shop's data leak into another or selling stock that doesn't exist.

Constraints

  • Strict isolation: every shop's products, customers and orders must stay separate.
  • Start on free hosting tiers without painting the code into a corner.
  • Cash on delivery first, with online payments (PayFast) alongside.
  • Shop owners aren't developers: design and settings are edited through forms, not code.
  • Stock must never oversell, even when two customers check out at the same moment.

Architecture

01 · Storefront

  • Next.js
  • Host → shop routing
  • Page caching per shop
  • 6 templates

02 · Admin

  • Next.js
  • Settings as forms
  • Orders + invoices
  • New-order alerts

03 · Express API

  • Store resolver
  • Roles + permissions
  • 15 modules
  • Outbox

04 · Data + services

  • PostgreSQL
  • store_id on 49 tables
  • Cloudflare R2
  • PayFast
Next.js storefront and admin in front of an Express modular monolith; every store-owned table carries store_id, and side effects go through an outbox.

The approach

  1. 01

    One codebase, many shops

    The storefront works out which shop to show from the domain, and every store-owned table carries a store_id. A registry lists all 49 store tables, and CI fails if a new table forgets it.

  2. 02

    A modular backend with clear boundaries

    The Express API is split into modules (catalog, inventory, cart, checkout, orders, payments, shipping and more) that only talk through each other's public surface, with cross-module side effects sent through an outbox.

  3. 03

    Correct stock and payments under pressure

    Stock is reserved with a single atomic database update, checkout is idempotent, and payment webhooks are verified and stored once before anything else happens.

  4. 04

    Shops that look their own

    Six store templates with sample products create a new shop in about ten seconds. Owners change branding, homepage sections, menus and more through forms, and the Almari template adds a 3D clothing rack customers can swipe through.

Deep dives

Routing every domain to the right shop

Each shop has its own domain, but they all hit the same Next.js storefront. A small proxy rewrites every request to an internal path that includes the host, so the page knows which shop it's rendering, and the result is cached separately for each shop.

Browser requests for the cart and checkout go to the shop's own domain and are forwarded to the API with the store attached. Cookies stay first-party on each shop's domain, so no cross-site setup is needed. The admin never guesses the shop from the domain; the store comes from the URL and access needs a membership and permission.

ts
// apps/storefront/src/proxy.ts (simplified)
export function proxy(request: NextRequest) {
  const host = request.headers.get("host")!.toLowerCase();
  const url = request.nextUrl.clone();
  url.pathname = `/s/${host}${url.pathname}`;
  return NextResponse.rewrite(url);
}
Simplified host routing: /products on any shop domain renders /s/<host>/products.

Stock that can't be oversold

Available stock is what's on hand minus what's reserved. Stock is reserved when an order is placed, not when it's added to the cart, using one atomic statement that only succeeds if enough is left. If two customers race for the last item, exactly one wins.

Order lines are reserved in a fixed order so two multi-item orders can never deadlock. Online-payment reservations expire if the customer doesn't pay, and they're released lazily the next time stock is checked, so correctness never depends on a cron job running on time.

sql
UPDATE inventory_levels
   SET reserved = reserved + $q
 WHERE store_id = $s
   AND variant_id = $v
   AND track_inventory
   AND on_hand - reserved >= $q
RETURNING on_hand, reserved;
The reservation rule: succeeds only when enough unreserved stock exists.

Idempotent checkout and an outbox for side effects

A customer on a flaky mobile connection may submit checkout several times. Each request carries an idempotency key, the key is locked inside the transaction, and a repeat returns the original order. In testing, many identical requests produced one order and one reservation.

Sending emails, SMS or WhatsApp messages happens through an outbox table written in the same transaction as the order. A worker delivers them afterwards, so a slow email provider can never fail or slow down a checkout.

ts
await db.$transaction(async (tx) => {
  await raw.lockKey(tx, `order-idem:${storeId}:${input.idempotencyKey}`);
  const existing = await tx.order.findUnique({
    where: { storeId_idempotencyKey: { storeId, idempotencyKey: input.idempotencyKey } },
  });
  if (existing) return existing; // replay: same order, no new reservation

  await reserveLines(tx, storeId, lines); // in variant_id order
  const order = await createOrder(tx, storeId, input);
  await outbox.add(tx, "order.placed", { storeId, orderId: order.id });
  return order;
});
Simplified from the checkout service.

Key decisions

A modular monolith, not microservices
One deployable API is cheap to host and easy to reason about. Module boundaries, checked in CI, keep the code split cleanly so a module can move out later if it ever needs to.
Platform-neutral code from day one
The same Express app runs as a serverless function or a plain Node server in Docker, so moving from free tiers to a VPS is a deploy change, not a rewrite.
Resize images in the admin's browser
Images are resized before a direct upload to Cloudflare R2, so the server needs no image processing and stays within free-tier limits.
Compete on local fit, not feature count
Cash on delivery, rupee pricing, local couriers and wallets matter more to these sellers than matching every Shopify feature.

Results

A working multi-store platform with storefront, admin and API, six store templates and a launch roadmap. It is being prepared for its first real sellers.

The platform already covers the full selling loop: catalogue, cart, checkout with cash on delivery or PayFast, order tracking, reviews, printable invoices and packing slips, Google sign-in for customers, and new-order alerts for shop owners.

Before taking real sellers, the next steps are a production server with off-site backups, error tracking, automatic SMS/WhatsApp order messages and a first courier integration.

Lessons

  • 01Make tenant isolation a rule the build enforces, not a habit people remember.
  • 02Design correctness so it never depends on a background job running on time.
  • 03Picking who you're for (cash-on-delivery sellers in Pakistan) makes every product decision easier.

FAQ

Can you build a multi-store or multi-tenant platform?

Yes. ShopKit runs many shops from one codebase with strict per-store data isolation, per-domain routing and shared templates.

Does ShopKit support cash on delivery?

Yes, cash on delivery is the default, with PayFast for online payments. JazzCash, Easypaisa and courier integration are on the roadmap.

Can I get early access to ShopKit for my shop?

ShopKit is preparing for its first sellers. Book a call if you sell online in Pakistan and want to be one of them.

Have a similar problem?

Book a call and let's talk about your project.

More case studies