Docs
← Back to site

Install and authenticate

1 min readUpdated November 11, 2026

Install#

Terminal
npm install ./nahnu-analytics-sdk-1.4.0.tgz

It needs Node 18 or later, or any runtime with fetch and Web Crypto (browsers, Deno, Bun, edge runtimes).

Make a platform key#

In wp-admin: Nahnu Analytics › Data & API › API keys › new key with Can: Platform. It works for all clients and allows 20,000 requests an hour.

Warning

Keep the key on your server only, never in a browser bundle or an app.

.env
NAHNU_ANALYTICS_URL=https://stats.example.com   # the WordPress site with the plugin
NAHNU_ANALYTICS_KEY=nak_…

Create a client#

lib/stats.ts
import { createClient } from 'nahnu-analytics-sdk';

export const stats = createClient(); // reads the two variables above

// Or pass everything yourself:
export const statsEu = createClient({
  url: 'https://stats.example.com',
  key: process.env.STATS_KEY,
  timeout: 15000, // ms before a request gives up
  retries: 2,     // after a network error, 429 or 5xx, when it can't double anything
  onError: (e) => logger.warn(e), // where track() reports failures
});
OptionDefaultWhat it does
urlNAHNU_ANALYTICS_URLThe WordPress site with the plugin
keyNAHNU_ANALYTICS_KEYA platform key (nak_…)
restBaseurl + '/wp-json'Without pretty permalinks: https://example.com/?rest_route=
timeout15000Milliseconds before a request gives up
retries2Tries again only when that can’t double anything
trustfrom the environmentWhich header carries the visitor’s address, for track(request)
onErrorconsole.warnWhere track() reports a failure
fetchglobal fetchYour own fetch

Check the setup#

For a health check or your settings screen:

Health check
const { ok, problem, version } = await stats.check();
// problem says what is wrong: the key, a key that is not a platform key, or the address

Next: Your users and their sites.

Was this helpful?