Integrations / Frameworks / Astro

๐Ÿš€

Astro

Track events from Astro API endpoints and server actions. Works with any Astro adapter โ€” Node, Vercel, Cloudflare.


Install

bash
npm install logit

Initialize

ts
// src/lib/logit.ts
import { init } from "logit";

export const logit = init({ token: import.meta.env.LOGIT_API_KEY });

API endpoint

ts
// src/pages/api/signup.ts
import type { APIRoute } from "astro";
import { logit } from "@/lib/logit";

export const POST: APIRoute = async ({ request }) => {
  const { email } = await request.json() as { email: string };

  // ... create user

  await logit.now("signups", {
    event: "New user signed up",
    description: email,
    icon: "๐Ÿ‘ค",
    notify: true,
    metadata: { email },
  });

  return new Response(JSON.stringify({ ok: true }), {
    headers: { "Content-Type": "application/json" },
  });
};

Server action (Astro 4.x+)

ts
// src/actions/index.ts
import { defineAction } from "astro:actions";
import { z } from "astro:schema";
import { logit } from "@/lib/logit";

export const server = {
  signup: defineAction({
    input: z.object({ email: z.string().email() }),
    handler: async ({ email }) => {
      await logit.now("signups", {
        event: "New user signed up",
        description: email,
        icon: "๐Ÿ‘ค",
        notify: true,
      });
      return { ok: true };
    },
  }),
};

Environment variable

bash
# .env
LOGIT_API_KEY=lk_live_xxxxxxxxxxxxxxxx

Tip: Use import.meta.env not process.env in Astro โ€” and prefix with LOGIT_ to keep it server-only (no PUBLIC_ prefix).

Ready to connect Astro?

7-day free trial. No credit card required.

Start free