Track your first AI request with Node.js
Send a trace from your Node.js backend, check the receipt, and find it in the console. No extra package required.
Before you start
Node.js 20 or later, a MeterTrail account and a project API key.
Keep project API keys out of browser code, screenshots and source control. No model API key is required for this test.
1. Create a project and API key
Create a free account, verify your email, then create or select a project in Project management. Open API keys for that project, create a key and complete email verification. Save the key when it is displayed.
Create a free account2. Set your environment
# macOS / Linux / WSL export METERTRAIL_ENDPOINT="https://metertrail.com" printf 'Project API key (input is visible): ' IFS= read -r METERTRAIL_API_KEY export METERTRAIL_API_KEY # Windows PowerShell (Node.js / Python tutorials) $env:METERTRAIL_ENDPOINT = "https://metertrail.com" $env:METERTRAIL_API_KEY = Read-Host "Project API key" -MaskInput
3. Send a sample trace
Save as send.mjs and run from the same terminal where you set the environment variables.
import { randomUUID, randomBytes } from "node:crypto";
const batch = {
schemaVersion: "1.0",
agent: {
serviceName: "node-example",
environment: "development",
instanceId: randomUUID(),
},
events: [
{
eventId: randomUUID(),
objectId: randomUUID(),
type: "trace",
revision: 1,
traceId: randomBytes(16).toString("hex"),
startedAt: new Date().toISOString(),
data: { name: "Example request", status: "success", durationMs: 120 },
},
],
};
const headers = {
Authorization: `Bearer ${process.env.METERTRAIL_API_KEY}`,
"Content-Type": "application/json",
};
const response = await fetch(
`${process.env.METERTRAIL_ENDPOINT}/v1/events/batch`,
{ method: "POST", headers, body: JSON.stringify(batch) },
);
console.log(response.status, await response.json());
node send.mjs
Run this code on your server after the AI request finishes. Replace the sample name, status and duration with your measured values. This example reports metadata only; it does not call a model or include prompts.
4. Check the result
A successful upload returns HTTP 200. pending is a receipt for asynchronous processing, not a confirmation that data is already queryable.
{"batchId":"<batch-id>","received":1,"status":"pending"}Optionally query the returned batchId using the same project key:
curl --fail-with-body --max-time 10 "https://metertrail.com/v1/batches/<batch-id>" \ -H "Authorization: Bearer $METERTRAIL_API_KEY"
5. Find the trace in your console
Select the project associated with your key and open Traces. Refresh after processing and find Example request. Model cost can be Unknown because this minimal example does not report model usage or prices.
Troubleshooting
401: check the key and project. 400: check required fields and timestamps. For quota or subscription errors, check Billing. After a network timeout retry the same event IDs and content rather than creating new objects.
Full ingestion field referenceOther tutorials:
Track your first AI request with Python · Send your first trace with REST and cURL