Skip to main content

Installation

After installing the SDK, create a Protekt client instance and configure it with your project credentials and any options relevant to your application.

Basic Setup

import { Protekt } from '@protekt/node';

const protekt = new Protekt({
loginId: process.env.PROTEKT_LOGIN_ID,
apiKey: process.env.PROTEKT_API_KEY,
});

Create the client once — at module load time — and export it for use throughout your application. Avoid creating a new instance per request.

// lib/protekt.js — create once, import everywhere
import { Protekt } from '@protekt/node';

if (!process.env.PROTEKT_LOGIN_ID || !process.env.PROTEKT_API_KEY) {
throw new Error('Missing Protekt environment variables');
}

export const protekt = new Protekt({
loginId: process.env.PROTEKT_LOGIN_ID,
apiKey: process.env.PROTEKT_API_KEY,
});

Configuration Options

const protekt = new Protekt({
// Required
loginId: 'lp_7xqm9...',
apiKey: 'pk_live_AbCdEfGh...',

// Optional
tokenExpiry: 3600, // JWT expiry override in seconds (default: project setting)
timeout: 5000, // HTTP request timeout in ms (default: 5000)
baseUrl: 'https://auth.protekt.io/v1', // Override API base URL (rarely needed)
debug: false, // Log SDK activity to console (default: false)
});

Option Reference

OptionTypeDefaultDescription
loginIdstringRequired. Your project's Login ID
apiKeystringRequired. Your project API key
tokenExpirynumberProject settingOverride JWT expiry in seconds for this client
timeoutnumber5000HTTP request timeout in milliseconds
baseUrlstringhttps://auth.protekt.io/v1Override the API base URL
debugbooleanfalseEnable verbose logging to console.debug

TypeScript Configuration

The SDK is fully typed. Pass a generic to Protekt to type your user metadata:

import { Protekt } from '@protekt/node';

interface UserMetadata {
plan: 'free' | 'pro' | 'enterprise';
organizationId?: string;
}

const protekt = new Protekt<UserMetadata>({
loginId: process.env.PROTEKT_LOGIN_ID!,
apiKey: process.env.PROTEKT_API_KEY!,
});

// user.metadata is now typed as UserMetadata
const { user } = await protekt.auth.verifyToken(token);
console.log(user.metadata.plan); // 'free' | 'pro' | 'enterprise'

Multiple Projects

If your application manages multiple Protekt projects (for example, separate projects per tenant), create a separate client instance per project:

// lib/protekt.js
import { Protekt } from '@protekt/node';

export function getProtektClient(loginId, apiKey) {
return new Protekt({ loginId, apiKey });
}

// Usage
const tenantClient = getProtektClient(tenant.loginId, tenant.apiKey);
const { user } = await tenantClient.auth.verifyToken(token);

Testing and CI

For unit tests, use the PROTEKT_API_KEY for your test project and stub outbound calls with msw or nock. Alternatively, the SDK exposes a Protekt.mock() factory:

// In tests
import { Protekt } from '@protekt/node';

const protekt = Protekt.mock({
verifyToken: async () => ({
user: { id: 'usr_test', email: 'test@example.com' },
error: null,
}),
});

Next Steps