Get started with the Node.js SDK

npmv1.5.8310.5 kB gzip
For Node.js developers10 minBeginner

This guide helps you install the Node.js SDK (@amplitude/analytics-node), initialize it, and send one event from your server to Amplitude. Each step lists the conditions that commonly break that step, so you can fix a problem where it happens. For the full option list, go to Configuration options. For changes between versions, go to the Node.js SDK release notes.

The Node.js SDK is for server-side code that sends events on behalf of many users. It doesn't store a user ID, device ID, or session between calls, so you pass the user's identity with each event. For browser code, use Browser SDK 2 instead.

Prerequisites

  • An Amplitude project and its API key. Find the key in Settings > Projects > your project > General.
  • The data center your organization uses: US (default) or EU.
  • The latest Node.js SDK release, or a pinned version you check against the Node.js SDK release notes for the features you use.
  • A Node.js runtime that supports the global URL class and ES2021. The package doesn't declare a minimum Node.js version. The examples on these pages run on Node.js 22.

Install the SDK

Install the package with your package manager.

bash
npm install @amplitude/analytics-node

The package ships a CommonJS build and an ES module build. Import it with either syntax:

js
import { init, track } from "@amplitude/analytics-node";
js
const { init, track } = require("@amplitude/analytics-node");

Fix installation problems

Initialize the SDK

Call init() once when your process starts, before it handles requests. init() returns an object whose promise resolves when the SDK is ready. Calls you make before it resolves wait in a queue and run in order.

js
import { init } from "@amplitude/analytics-node";

init("AMPLITUDE_API_KEY");

If your project uses the EU data center, pass serverZone: "EU" as the second argument: init("AMPLITUDE_API_KEY", { serverZone: "EU" }). For other options, go to Configuration options.

Fix initialization problems

Send a first event

Track one event for a user and wait for the upload result. Pass the user's ID in the third argument, because the SDK doesn't remember identity between calls. track() returns an object whose promise resolves after the upload attempt.

js
import { init, track } from "@amplitude/analytics-node";

init("AMPLITUDE_API_KEY");

const result = await track(
  "Sign Up Completed",
  { plan: "starter" },
  { user_id: "user-12345" },
).promise;

console.log(result.code, result.message);
// 200 Event tracked successfully

The SDK uploads queued events every 10 seconds, in requests of up to 200 events. As a result, the promise resolves about 10 seconds after the call. In a server, don't await the promise in a request handler. Go to Use the SDK in request handlers.

Fix upload problems

Confirm the event in Amplitude

  1. In Amplitude, open User Lookup and search for the user ID you passed, user-12345 in the example.
  2. Open the Activity tab and turn on Live event updates.
  3. Confirm that Sign Up Completed appears with the plan property.

If the event doesn't appear within a minute, follow Verify events. That page walks through each stage between track() and Amplitude's servers.

Next steps

Was this helpful?

For AI agents: a documentation index is available at /docs/llms.txt. Append .md to any page URL for markdown, or send Accept: text/markdown.

On this page