Authentication

The SDK uses a two-tier credential model designed for browser environments: a publishable API key that initializes the client, and a session token issued by the server when you call identify(). The session token — not the API key — authorizes per-user actions like tracking events and fetching in-app content.

API keys

Every init() call requires an API key scoped to a single project. Keys come in two variants:

PrefixUseNotes
tk_live_…ProductionTreat as a public, write-limited credential
tk_test_…DevelopmentAuto-enables debug logging in the SDK
import { init } from '@treliq/sdk';

const treliq = init({
  apiKey: 'tk_live_xxx',
  projectId: 'your-project-id',
});

API keys are publishable. They can ship in your bundle, in a <script data-api-key="…"> tag, or in a meta tag. Their scope is intentionally narrow: they can call /v1/engine/sdk/ingest (identify) and a handful of read endpoints, nothing else. They cannot read user data, list events, modify workflows, or call any backoffice endpoint.

Server-side operations (creating workflows, reading analytics, managing users) use a separate secret API key with a different prefix and a different endpoint surface. Never embed those in browser code.

Session tokens

When you call identify(externalUserId, attributes), the server issues a short-lived signed session token bound to:

  • the project ID,
  • the resolved end-user ID, and
  • the issuing API key.

The SDK stores the token in memory and attaches it (as a JSON field, not a cookie) to every subsequent track, getInAppContent, and reportAction request. You never construct or read the session token yourself — the SDK manages it transparently.

await treliq.identify('user_123', { email: '[email protected]' });

// Now authenticated as user_123 — track() will use the session token internally
treliq.track('page_viewed');

The token’s lifetime is bounded; calling identify() again refreshes it. Calling reset() discards it.

Logout flow

When the user logs out of your app, call reset() so the next identify() starts a fresh session and so any in-app content tied to the previous identity is torn down:

treliq.reset();

This clears the in-memory session token and removes any tq_* keys from localStorage and sessionStorage as a defensive cleanup.

Common mistakes

  • Calling track() before identify() throws Treliq: call identify() before track(). Always identify first.
  • Sharing one SDK instance across users — call reset() between users on shared devices, then identify() again.
  • Hard-coding test keys in production — tk_test_… keys hit the test environment and emit verbose logs.