Express.js

Set up OneMinute Logs in Express.js.

Install the Express SDK, register its request-logging middleware, and send structured events through the logger attached to each request.

If you are using Cursor, Claude Code, Codex, Antigravity, or any other AI IDE, follow our Setup with AI guide.

1. Install the SDK

Add the OneMinute Logs SDK for this framework.

npm install @oneminutelogs/express

2. Create an API key

Create an API key from your OneMinute Logs dashboard and add it to your environment variables. Open the API key guide.

ONE_MINUTE_LOGS_API_KEY=your_oml_api_key

3. Create src/app.ts

Initialize the logger once and import it anywhere you need to send events.

import express from "express";
import { withOneMinuteLogs } from "@oneminutelogs/express";

const app = express();

app.use(express.json());
app.use(
  withOneMinuteLogs({
    apiKey: process.env.ONE_MINUTE_LOGS_API_KEY!,
    appName: "my-express-api",
    environment: process.env.NODE_ENV || "development",
  }),
);

The middleware automatically logs every completed request and attaches a type-safe logger to req.logger for route-specific events.

Standalone logger for workers and scripts

Outside an Express request, create one logger and reuse it in background jobs or command-line scripts.

import { createLogger } from "@oneminutelogs/express";

export const logs = createLogger({
  apiKey: process.env.ONE_MINUTE_LOGS_API_KEY!,
  appName: "my-express-worker",
  environment: process.env.NODE_ENV || "development",
});

4. Send events

Use event types like info, warning, error, audit, and metric to keep your production timeline useful and searchable.

// Continue in src/app.ts
app.post("/checkout", async (req, res) => {
  await req.logger.info({
    message: "Checkout started",
    service: "api",
  });

  try {
    const orderId = req.body.orderId;
    const amount = req.body.amount;

    await req.logger.audit({
      message: "Customer charged successfully",
      service: "billing",
      security: {
        auth_status: "success",
      },
      track: {
        custom: {
          orderId,
        },
      },
    });

    await req.logger.metric({
      message: "Checkout completed",
      service: "billing",
      metrics: {
        custom: {
          amount,
        },
      },
    });

    res.json({ ok: true });
  } catch (error) {
    await req.logger.error({
      message: "Checkout failed",
      service: "billing",
      operation: "checkout",
      importance: "critical",
    });

    res.status(500).json({ ok: false });
  }
});

app.listen(3000);

What's next?

After events are flowing, alerts, webhooks, live dashboards, and search all work from the same event pipeline.

Found something unexpected? Let us know.