Documentation menu
sdkjavascript

JavaScript / TypeScript SDK

Instrument your Node.js, Deno, Bun, or browser app with the official ScryWatch SDK — zero dependencies, automatic batching, and session tracking.

JavaScript / TypeScript SDK

The ScryWatch JavaScript SDK works in Node.js, Deno, Bun, and modern browsers. Zero dependencies. Automatic event batching. Built-in session tracking.

What you’ll need

  • A ScryWatch project API key (see API Keys & Settings)
  • Node.js 18+ / Deno / Bun, or a browser environment

Step 1: Install the SDK

npm install @scrywatch/sdk

Or with other package managers:

pnpm add @scrywatch/sdk
bun add @scrywatch/sdk

For Deno, import directly:

import { LogMonitor } from 'npm:@scrywatch/sdk';

Step 2: Initialize the client

import { LogMonitor } from '@scrywatch/sdk';

const logger = new LogMonitor({
  endpoint: 'https://api.scrywatch.com',
  apiKey: 'sw_your_api_key_here',
  service: 'my-api',           // optional — tags all events with this service name
  environment: 'production',   // optional — tags all events with this environment
});

Tip: Create one shared LogMonitor instance per service. Pass it around as a module-level singleton or inject it as a dependency.

Step 3: Log events

// Info level
logger.info('User signed in', { userId: '1234', method: 'email' });

// Warning level
logger.warn('Rate limit approaching', { endpoint: '/api/ingest', remaining: 5 });

// Error level
logger.error('Payment failed', { orderId: 'ord_abc', error: err.message });

// Debug level
logger.debug('Cache miss', { key: 'user:1234' });

Each call accepts:

  • message (string) — the log message
  • metadata (object, optional) — any additional fields you want to attach

Step 4: Track sessions

Sessions group events by a shared session ID, letting you trace a user’s journey through your app.

// Start a session
logger.startSession();

// Optionally associate the session with a user
logger.setUserId('1234');

// Log events — they're automatically tagged with the active session ID
logger.info('Product page viewed', { productId: 'prod_xyz' });
logger.info('Added to cart', { productId: 'prod_xyz', quantity: 2 });
logger.error('Checkout failed', { reason: 'card_declined' });

// End the session (flushes pending events and clears the session ID)
logger.endSession();

Sessions appear on the Sessions page in the dashboard.

Step 5: Track page navigation (browser)

logger.logNavigation('/checkout');

Step 6: Track API calls

const start = Date.now();
const response = await fetch('/api/data');
const duration = Date.now() - start;

logger.logApiCall('GET', '/api/data', response.status, duration);

logApiCall automatically picks the log level from the status code: info for < 400, warn for 400–499, error for >= 500.

Step 7: Capture errors

try {
  await riskyOperation();
} catch (err) {
  logger.logError(err as Error, { context: 'riskyOperation' });
}

logError sends the error as a crash-type event with the error’s message, name, and stack trace attached.

Step 8: Understand batching

The SDK buffers events in memory and flushes automatically:

  • Every 10 seconds (configurable via flushInterval) — if there are pending events
  • Every 50 events (configurable via bufferSize) — if the buffer fills up
  • On page hide / before unload — in browser environments, the SDK flushes automatically when the tab is hidden or closed

To force an immediate flush (e.g. before a process exits):

await logger.flush();

Tip: Always call await logger.flush() at the end of serverless function handlers to ensure all events are sent before the function terminates.

When you’re done with a LogMonitor instance (e.g. shutting down a long-running process), call logger.dispose() to flush any remaining events and clean up.

Step 9: Configure retries

The SDK retries failed requests with exponential backoff (maxRetries, default 3). If all retries fail, the batch is dropped — your application continues normally.

const logger = new LogMonitor({
  endpoint: 'https://api.scrywatch.com',
  apiKey: 'your_key',
  bufferSize: 50,
  flushInterval: 10000,
  maxRetries: 3,
});

You’re done

You now know how to:

  • Install and initialize the LogMonitor client
  • Log info, warn, error, and debug events with metadata
  • Track user sessions and navigation
  • Log API calls and captured errors
  • Understand batching, flush, and retry behavior

Full SDK reference — all constructor options, methods, session API, and retry configuration.

API Reference

Want the full API spec for this feature?

View API →