Node.js

Amplitude Node.js SDK Installation & Quick Start

npm versionnpm version

This is Amplitude Node.js SDK written in Typescript, the 1st backend SDK for Amplitude. We would like to hear your ideas too!

While the Client SDK's are optimized to track session and attribution for a single user/device, the Node SDK's focus is to provide a helpful developer experience to help back-end services reliably and correctly send events from many users and sources. Out of the box, the Node SDK provides:

  • Batching of events to send multiple events in the same request.
  • Retry handling mechanisms to handle when a network request fails, or a payload is throttled/invalid.
  • Useful utilities and typing help debug instrumentation issues.

By default, the Node SDK uses and works best with the HTTP API V2.

Installation

Run npm install @amplitude/node in your project directory, the same level with package.json.

EU Data Residency

Sending data to Amplitude's EU servers, you need to configure the server url during the initialization.

client = Amplitude.init(<AMPLITUDE_API_KEY>, {
    serverUrl: "https://api.eu.amplitude.com/2/httpapi"
});

Usage

  • Please see the code snippet below.

Note The Node SDK uses the HTTP V2 API and follows the same constraints for events. Make sure that all events logged in the Node SDK have the event_type field and at least one of device_id or user_id, and follows the HTTP API's constraints on each of those fields.

import * as Amplitude from '@amplitude/node';

const client = Amplitude.init(<AMPLITUDE_API_KEY>);

client.logEvent({
  event_type: 'Node.js Event',
  user_id: '[email protected]',
  location_lat: 37.77,
  location_lng: -122.39,
  ip: '127.0.0.1',
  event_properties: {
    keyString: 'valueString',
    keyInt: 11,
    keyBool: true
  }
});

// Send any events that are currently queued for sending.
// Will automatically happen on the next event loop.
client.flush();
// ES5 Syntax
const Amplitude = require('@amplitude/node');
// ES6 Syntax
import * as Amplitude from '@amplitude/node';

var client = Amplitude.init(<AMPLITUDE_API_KEY>);
client.logEvent({
  event_type: 'Node.js Event',
  user_id: '[email protected]',
  location_lat: 37.77,
  location_lng: -122.39,
  ip: '127.0.0.1',
  event_properties: {
    keyString: 'valueString',
    keyInt: 11,
    keyBool: true
  }
});

// Send any events that are currently queued for sending.
// Will automatically happen on the next event loop.
client.flush();

Need Help?

If you have any problems or issues over our SDK, feel free to create a github issue or submit a request on Amplitude Help.


Did this page help you?