Integrations / Frameworks / Express

๐Ÿš‚

Express

Add business event alerts to Express route handlers or middleware. Works with any Express middleware stack and any database.


Install

bash
npm install logit express

Initialize

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

export const logit = init({ token: process.env.LOGIT_API_KEY! });

In a route

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

const app = express();
app.use(express.json());

app.post("/signup", async (req, res) => {
  const { email, plan } = req.body as { email: string; plan: string };

  // ... persist user

  await logit.now("signups", {
    event: "New user signed up",
    description: `${email} joined on ${plan}`,
    icon: "๐Ÿ‘ค",
    notify: true,
    tags: { plan },
    metadata: { email, ip: req.ip },
  });

  res.json({ ok: true });
});

Error middleware

ts
import type { Request, Response, NextFunction } from "express";

app.use(async (err: Error, req: Request, res: Response, _next: NextFunction) => {
  await logit.now("errors", {
    event: "Unhandled error",
    description: err.message,
    icon: "โŒ",
    notify: true,
    tags: { path: req.path, method: req.method },
    metadata: { stack: err.stack },
  });
  res.status(500).json({ error: "Internal Server Error" });
});

Environment variable

bash
# .env
LOGIT_API_KEY=lk_live_xxxxxxxxxxxxxxxx

Ready to connect Express?

7-day free trial. No credit card required.

Start free