SvelteKitTypeScriptEdge-ready

Add a customer database to SvelteKit without leaving your stack

SvelteKit's file-based routing gives you clear server/client boundaries via +server.ts and +page.server.ts. TinyCRM fits naturally in that server layer — one call per event, no extra configuration, no third-party browser scripts.

Installation

npm install tinycrm-sdk

Then add your API key to .env:

TINYCRM_API_KEY=tcrm_proj_xxxxxxxxxxxx

SvelteKit architecture and where TinyCRM lives

SvelteKit has two types of server-side files: +server.ts (API endpoints) and +page.server.ts (server load + form actions). TinyCRM belongs in either — wherever you handle user creation or significant state changes.

src/routes/
  auth/
    signup/
      +server.ts         ← Call identify() here (API endpoint)
  (app)/
    dashboard/
      +page.server.ts    ← Or here (form action)
  hooks.server.ts        ← Or here for post-request hooks

Getting started: SvelteKit integration

Step 1 — Create a shared SDK instance

// src/lib/server/tinycrm.ts
import { TinyCRM } from "tinycrm-sdk";
import { TINYCRM_API_KEY } from "$env/static/private";

export const tinycrm = new TinyCRM({ apiKey: TINYCRM_API_KEY });

Step 2 — Identify in a +server.ts route handler

// src/routes/auth/signup/+server.ts
import { json } from "@sveltejs/kit";
import type { RequestHandler } from "./$types";
import { tinycrm } from "$lib/server/tinycrm";
import { db } from "$lib/server/db";

export const POST: RequestHandler = async ({ request }) => {
  const { email, name } = await request.json();

  const user = await db.users.create({ email, name });

  // Fire-and-forget: don't block response
  tinycrm.identify({
    email: user.email,
    name: user.name,
    status: "free",
    params: { plan: "starter" },
  });

  return json({ ok: true });
};

Step 3 — Use in a form action with Lucia Auth

// src/routes/signup/+page.server.ts
import { fail, redirect } from "@sveltejs/kit";
import type { Actions } from "./$types";
import { tinycrm } from "$lib/server/tinycrm";
import { lucia } from "$lib/server/auth";
import { db } from "$lib/server/db";

export const actions: Actions = {
  default: async ({ request, cookies }) => {
    const data = await request.formData();
    const email = data.get("email") as string;
    const name = data.get("name") as string;

    const user = await db.users.create({ email, name });
    const session = await lucia.createSession(user.id, {});

    // Sync to TinyCRM
    tinycrm.identify({ email, name, status: "free" });

    const sessionCookie = lucia.createSessionCookie(session.id);
    cookies.set(sessionCookie.name, sessionCookie.value, {
      path: ".",
      ...sessionCookie.attributes,
    });

    redirect(302, "/dashboard");
  },
};

Step 4 — Track upgrades from a payment webhook

// src/routes/webhooks/stripe/+server.ts
import { json } from "@sveltejs/kit";
import type { RequestHandler } from "./$types";
import { tinycrm } from "$lib/server/tinycrm";
import { verifyStripe } from "$lib/server/stripe";

export const POST: RequestHandler = async ({ request }) => {
  const event = await verifyStripe(request);

  if (event.type === "checkout.session.completed") {
    const email = event.data.object.customer_email!;
    tinycrm.identify({ email, status: "paid", params: { plan: "pro" } });
  }

  return json({ received: true });
};

Works alongside popular SvelteKit tools

Lucia Auth

Integrate in session creation routes.

Drizzle ORM

Call identify() after db.insert(users).

Prisma

Works after prisma.user.create().

Auth.js (SvelteKit)

Use in the signIn callback.

Cloudflare D1

Compatible with the CF Workers runtime.

Vercel

Deploys normally via @sveltejs/adapter-vercel.

SvelteKit FAQ

Can I use tinycrm-sdk in a SvelteKit +page.server.ts?

Yes. +page.server.ts files run server-side only and have full Node.js access. You can safely import and call tinycrm.identify() inside load functions or form actions.

Does TinyCRM work with SvelteKit form actions?

Yes. Form actions in +page.server.ts are server-side. Call identify() inside your action handler after processing the form, typically right after inserting the user to your database.

How do I use TinyCRM with Lucia Auth in SvelteKit?

Call identify() in the form action or API route where you create the user session. After lucia.createSession(), add tinycrm.identify({ email, status: 'free' }) on the same route.

Does the Cloudflare Workers adapter affect SDK compatibility?

The SDK uses native fetch and no Node.js-specific APIs, so it is fully compatible with the Cloudflare Workers runtime used by the SvelteKit Cloudflare adapter.

SvelteKit-native customer tracking

14-day free trial. No credit card. Two lines of code.

Start free trial
npm install tinycrm-sdk