Skip to content

SDK API

@rep-protocol/sdk — Zero-dependency, framework-agnostic TypeScript SDK for reading REP-injected environment variables.

Terminal window
npm install @rep-protocol/sdk

Retrieve a PUBLIC tier variable. Synchronous — no network call, no promise.

function get(key: string): string | undefined;
function get(key: string, defaultValue: string): string;
ParameterTypeDescription
keystringVariable name (after prefix stripping, e.g. 'API_URL')
defaultValuestringOptional fallback if the variable is not present

Returns: The variable value, defaultValue, or undefined.

import { rep } from '@rep-protocol/sdk';
const apiUrl = rep.get('API_URL'); // string | undefined
const apiUrl = rep.get('API_URL', 'http://localhost'); // string (never undefined)

Retrieve a SENSITIVE tier variable. Fetches a session key, decrypts the blob, and caches all sensitive values.

function getSecure(key: string): Promise<string>;
ParameterTypeDescription
keystringVariable name (e.g. 'ANALYTICS_KEY')

Returns: Promise<string> — the decrypted value.

Throws: REPError if the session key endpoint is unreachable, the key has expired, or decryption fails.

const key = await rep.getSecure('ANALYTICS_KEY');

Retrieve all PUBLIC tier variables as a frozen object.

function getAll(): Readonly<Record<string, string>>;

Returns: A frozen Record<string, string> of all public variables. Empty object if no payload is present.

const allVars = rep.getAll();
console.log(allVars.API_URL);

Check whether the REP payload is present and its integrity is valid.

function verify(): boolean;

Returns: true if the payload is present, parseable, and the SRI hash matches. false if missing, malformed, or tampered.

if (!rep.verify()) {
console.error('REP payload missing or tampered');
}

Returns metadata about the current REP payload.

function meta(): REPMeta | null;
interface REPMeta {
version: string;
injectedAt: Date;
integrityValid: boolean;
publicCount: number;
sensitiveAvailable: boolean;
hotReloadAvailable: boolean;
}

Returns: REPMeta object, or null if no payload is present.

const m = rep.meta();
if (m) {
console.log('REP version:', m.version);
console.log('Injected at:', m.injectedAt);
console.log('Public vars:', m.publicCount);
}

Register a callback for when a specific variable changes via hot reload.

function onChange(
key: string,
callback: (newValue: string, oldValue: string | undefined) => void
): () => void;

Returns: An unsubscribe function. Call it to stop listening.

The SSE connection is established lazily on the first onChange() or onAnyChange() call. It is closed when all listeners have been removed.

const unsub = rep.onChange('FEATURE_FLAGS', (newValue, oldValue) => {
console.log(`Changed: ${oldValue}${newValue}`);
});
// Later:
unsub();

Register a callback for any variable change.

function onAnyChange(
callback: (key: string, newValue: string, oldValue: string | undefined) => void
): () => void;

Returns: An unsubscribe function.

const unsub = rep.onAnyChange((key, newValue) => {
console.log(`${key} updated to ${newValue}`);
});

Custom error class thrown by SDK operations.

class REPError extends Error {
name: 'REPError';
}

Thrown by getSecure() when the session key endpoint is unreachable, the key has expired, or decryption fails.

Both named exports and a namespace object are available:

// Named imports
import { get, getSecure, verify, onChange } from '@rep-protocol/sdk';
// Namespace import (recommended)
import { rep } from '@rep-protocol/sdk';
rep.get('API_URL');

On import, the SDK synchronously:

  1. Locates <script id="__rep__"> in the DOM
  2. Parses the JSON content
  3. Triggers async SRI verification (non-blocking)
  4. Freezes the public object
  5. Sets _available and _tampered internal flags

No network calls are made during initialization.