APILensAPILens

tRPC

v10 / v11

Middleware for tRPC v10/v11 — observes every procedure call.

Creates a tRPC middleware function. Attach it to a base procedure so every query and mutation is automatically traced.

Procedure dot-paths are converted to slash-paths for consistent routing in the dashboard.

Install

bash
npm install auto-api-observe

Quick Start

The minimal setup — import, configure, and you're observing.

trpc-quick-start.js
javascript
// server/trpc.ts
import { initTRPC }                         from '@trpc/server';
import { createTrpcObservabilityMiddleware } from 'auto-api-observe';

const t = initTRPC.create();

const observability = createTrpcObservabilityMiddleware({
  apiKey: process.env.APILENS_KEY,
});

export const publicProcedure = t.procedure.use(observability);

Full Example

All available options with custom fields and real-world usage.

trpc-full.js
javascript
// server/trpc.ts
import { initTRPC }                                    from '@trpc/server';
import { createTrpcObservabilityMiddleware, addField } from 'auto-api-observe';

const t = initTRPC.create();

const observability = createTrpcObservabilityMiddleware({
  apiKey:        process.env.APILENS_KEY,
  slowThreshold: 400,
  tags:          { service: 'trpc-api' },
});

export const publicProcedure = t.procedure.use(observability);

// router.ts
import { z } from 'zod';
import { publicProcedure } from './trpc';

export const userRouter = t.router({
  getById: publicProcedure
    .input(z.object({ id: z.string() }))
    .query(async ({ input }) => {
      addField('userId', input.id);
      return db.query('SELECT * FROM users WHERE id = $1', [input.id]);
    }),
});

What's Tracked Automatically

  • Procedure path converted to URL (user.getById → /user/getById)
  • Procedure type (query / mutation)
  • Latency from middleware entry to next() return
  • Errors from failed procedures

Options Reference

OptionTypeDefault
apiKeystring
slowThresholdnumber1000
tagsRecord{}

Custom Fields

Attach any business context to the current request via AsyncLocalStorage — scoped per request, works the same on every framework.

javascript
const { addField } = require('auto-api-observe');

// In any route handler:
addField('userId',  req.user.id);
addField('plan',    req.user.plan);
addField('country', req.headers['cf-ipcountry']);

// Sensitive keys are auto-redacted before shipping:
addField('authorization', token); // → "[REDACTED]"
addField('password',      pass);  // → "[REDACTED]"

Ready to observe your tRPC app?

Sign up free — get your API key in under 30 seconds. No credit card required.

Get your free API key