Guides / Platforms & Tools

How to Log Business Events from Deno Applications

3 min readยทPlatforms & Tools

Why REST API

The LogIt npm SDK doesn't work in Deno directly (different module system). Use the REST API instead โ€” it's just a fetch call.

Deno helper module

ts
// lib/logit.ts
interface LogitEvent {
  channel: string;
  event: string;
  description?: string;
  icon?: string;
  notify?: boolean;
  tags?: Record<string, string>;
  metadata?: Record<string, string | number | boolean>;
}

export async function logit(payload: LogitEvent): Promise<void> {
  const apiKey = Deno.env.get("LOGIT_API_KEY");
  if (!apiKey) return;

  await fetch("https://logit.now/api/v1/events", {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
      Authorization: `Bearer ${apiKey}`,
    },
    body: JSON.stringify(payload),
  });
}

Deno + Oak (web framework)

ts
// main.ts
import { Application, Router } from "https://deno.land/x/oak/mod.ts";
import { logit } from "./lib/logit.ts";

const router = new Router();

router.post("/api/signup", async (ctx) => {
  const body = await ctx.request.body({ type: "json" }).value;

  await logit({
    channel: "signups",
    event: "New user signed up",
    description: body.email,
    icon: "๐Ÿ‘ค",
    notify: true,
    metadata: { email: body.email, source: "deno-api" },
  });

  ctx.response.body = { ok: true };
});

const app = new Application();
app.use(router.routes());
await app.listen({ port: 8000 });

Deno Deploy (edge functions)

ts
// main.ts
import { logit } from "./lib/logit.ts";

Deno.serve(async (req: Request) => {
  const url = new URL(req.url);

  if (url.pathname === "/api/events" && req.method === "POST") {
    const body = await req.json();

    await logit({
      channel: body.channel ?? "events",
      event: body.event,
      description: body.description,
      icon: body.icon ?? "๐Ÿ“Œ",
      notify: body.notify ?? false,
      metadata: { edge: true, region: Deno.env.get("DENO_REGION") ?? "unknown" },
    });

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

  return new Response("Not found", { status: 404 });
});

Environment variables

bash
# Local
LOGIT_API_KEY=lk_live_xxxxxxxxxxxxxxxx deno run --allow-net --allow-env main.ts

# Deno Deploy: set via the Deno Deploy dashboard โ†’ Environment Variables

Try LogIt free

7-day trial. No credit card required.

Start free