Skip to main content

Overview

Client is the main entry point for the Mirador SDK. It handles authentication and creates trace builders.

Import

Web SDK:
import { Client } from '@miradorlabs/web-sdk';
Node.js SDK:
import { Client } from '@miradorlabs/nodejs-sdk';

Constructor

Web SDK:
new Client(apiKey: string, options?: ClientOptions)
Node.js SDK:
new Client(apiKey?: string, options?: ClientOptions)

Parameters

ParameterTypeRequiredDescription
apiKeystringYes (Web) / No (Node.js)API key for authentication
optionsClientOptionsNoConfiguration options

Options

interface ClientOptions {
  apiUrl?: string;              // Gateway URL (default: ingest-gateway-dev.mirador.org:443)
  keepAliveIntervalMs?: number; // Keep-alive ping interval in milliseconds (default: 10000)
}

Examples

Basic initialization:
const client = new Client('your-api-key');
With custom gateway:
const client = new Client('your-api-key', {
  apiUrl: 'https://custom-gateway.example.com:443'
});
With custom keep-alive interval:
const client = new Client('your-api-key', {
  keepAliveIntervalMs: 15000  // Ping every 15 seconds
});

Methods

trace()

Creates a new trace builder. Web SDK:
trace(options?: TraceOptions): Trace
Node.js SDK:
trace(options?: TraceOptions): Trace

Parameters

ParameterTypeRequiredDescription
optionsTraceOptionsNoTrace configuration

TraceOptions

Web SDK:
OptionTypeDefaultDescription
namestringundefinedName identifying the trace
includeUserMetabooleantrueInclude browser metadata
maxRetriesnumber3Maximum retry attempts
retryBackoffnumber1000Base backoff delay (ms)
autoClosebooleanfalseAutomatically close trace on page unload
Node.js SDK:
OptionTypeDefaultDescription
namestringundefinedName identifying the trace
captureStackTracebooleantrueCapture stack trace at trace creation
maxRetriesnumber3Maximum retry attempts
retryBackoffnumber1000Base backoff delay (ms)

Returns

Trace - A trace builder instance

Examples

Web SDK - Basic trace:
const trace = client.trace({ name: 'UserAction' });
Web SDK - With full options:
const trace = client.trace({
  name: 'CriticalOperation',
  includeUserMeta: true,
  maxRetries: 5,
  retryBackoff: 2000
});
Node.js SDK - Basic trace:
const trace = client.trace({ name: 'UserAction' });
Node.js SDK - Without stack trace:
const trace = client.trace({ name: 'UserAction', captureStackTrace: false });

Usage Patterns

Single Client Instance

Create one client instance and reuse it: Web SDK:
// client.ts
import { Client } from '@miradorlabs/web-sdk';

export const mirador = new Client(process.env.MIRADOR_API_KEY);

// component.ts
import { mirador } from './client';

function handleAction() {
  const trace = mirador.trace({ name: 'ComponentAction' });
  // ...
}
Node.js SDK:
// client.ts
import { Client } from '@miradorlabs/nodejs-sdk';

export const mirador = new Client(process.env.MIRADOR_API_KEY);

// handler.ts
import { mirador } from './client';

async function handleAction() {
  const trace = mirador.trace({ name: 'ComponentAction' });
  await trace.create();
  // ...
}

Multiple Traces

A single client can create multiple concurrent traces: Web SDK:
const client = new Client('api-key');

// User flow trace
const userTrace = client.trace({ name: 'UserSession' });

// Background operation trace
const bgTrace = client.trace({ name: 'BackgroundSync' });

// Both traces operate independently
userTrace.addEvent('user_clicked');
bgTrace.addEvent('sync_started');
Node.js SDK:
const client = new Client('api-key');

// User flow trace
const userTrace = client.trace({ name: 'UserSession' });
await userTrace.create();

// Background operation trace
const bgTrace = client.trace({ name: 'BackgroundSync' });
await bgTrace.create();

// Both traces operate independently
userTrace.addEvent('user_clicked');
bgTrace.addEvent('sync_started');

Environment-Based Configuration

const client = new Client(
  process.env.MIRADOR_API_KEY
);

Authentication

The API key is sent with every request in the x-ingest-api-key header. Keep your API key secure:
  • Don’t commit it to version control
  • Use environment variables
  • Consider using different keys for dev/prod

Next Steps