← Send a metric

How to send a metric from Node.js

Node.js 18 and newer have fetch built in, so a metric is one request with no package to install. The same code works in Express, Next.js route handlers, serverless functions and scripts.

Before you start

Create a metric in Umlogger and copy its ingest key (pmk_…) from the metric's page. Choose count for things that happen, or value for measurements (which one?). The examples read the key from an environment variable.

The helper

It never throws, so a network problem can never break your app, and it gives up after five seconds.

const UMLOGGER_URL = 'https://data.umlogger.com/api/m'

export async function sendMetric(key, value = 1, timestamp) {
  const url = new URL(UMLOGGER_URL)
  url.searchParams.set('key', key)
  url.searchParams.set('value', String(value))
  if (timestamp) url.searchParams.set('timestamp', timestamp)
  try {
    const res = await fetch(url, { signal: AbortSignal.timeout(5000) })
    if (!res.ok) console.error('umlogger:', res.status, await res.text())
  } catch (error) {
    console.error('umlogger:', error)
  }
}

Count an event

Each call adds 1. In a request handler you can skip the await so the response isn’t delayed.

void sendMetric(process.env.UMLOGGER_SIGNUPS_KEY)

Record a value

For measurements, use a value metric and send the number. Umlogger averages the samples on the chart.

const start = performance.now()
await buildReport()
await sendMetric(process.env.UMLOGGER_REPORT_TIME_KEY, Math.round(performance.now() - start))

Backfill old data

Add a timestamp with a time zone (ISO 8601). toISOString() always gives one.

const day = new Date('2026-11-30T00:00:00Z')
await sendMetric(process.env.UMLOGGER_SIGNUPS_KEY, 37, day.toISOString())

Tips

  • In serverless functions (Lambda, Vercel, Cloudflare Workers), await the call: the function may be frozen as soon as it returns.
  • Using TypeScript? Add types to the parameters: key: string, value = 1, timestamp?: string.
  • Want to hear when the numbers stop arriving? Add an alert.

Questions

Which key do I use?

Use the metric's own ingest key (pmk_…), shown on the metric's page in Umlogger. It can only add samples to that one metric. Keep it in an environment variable or a secret store rather than in your code. If it leaks, replace it from the metric's page; the old key stops working immediately and your data stays.

How many samples can I send?

Up to 120 requests per minute per key. For busier events, add them up and send the total: a count metric adds up its samples, so one request with value=25 counts as 25 events.

Should my metric be a count or a value?

Use a count for things that happen (signups, orders, errors): Umlogger adds the samples up per hour or day. Use a value for measurements (response time, queue length, MRR): Umlogger averages them.

Other languages

Python · Go · PHP · Bash and curl · GitHub Actions · Full API reference

Start free trial →