logo

Vedatrace

Javascript sdk

JavaScript/TypeScript SDK

Get started with the VedaTrace SDK for JavaScript and TypeScript.

Installation

Install the VedaTrace SDK using your preferred package manager.

npm install vedatrace
yarn add vedatrace
bun add vedatrace
pnpm add vedatrace

Basic Usage

The vedatrace function is the main entry point for the SDK. It returns a logger instance that you can use to send logs to VedaTrace.

import { vedatrace } from 'vedatrace'

const logger = vedatrace({
  apiKey: 'your-api-key',
  service: 'my-service'
})

logger.info('Hello world')
logger.error('Something went wrong', { error: 'Error message' })

Frontend (Browser)

In the browser, the SDK automatically detects the runtime and uses batching with lifecycle listeners (pagehide / visibilitychange) to flush logs before the page unloads.

import { vedatrace } from 'vedatrace'

const logger = vedatrace({
  apiKey: 'your-api-key',
  service: 'my-frontend',
})

logger.info('User visited page')

Backend (Node.js / Express)

On Node.js, the SDK uses a batch timer with graceful shutdown via process.on('beforeExit'), process.on('SIGTERM'), and process.on('SIGINT').

import { vedatrace } from 'vedatrace'

const logger = vedatrace({
  apiKey: 'your-api-key',
  service: 'my-backend',
})

logger.info('Server started')

Cloudflare Workers

Cloudflare Workers terminate shortly after returning a Response. Pass the waitUntil function from cloudflare:workers to keep the worker alive during log delivery:

import { vedatrace } from 'vedatrace'
import { waitUntil } from 'cloudflare:workers'

const logger = vedatrace({
  apiKey: env.VEDATRACE_API_KEY,
  service: 'my-worker',
  waitUntil,
})

export default {
  async fetch(request) {
    logger.info('Request received', { url: request.url })
    return new Response('Hello')
  }
}

See the Edge Runtimes guide for other approaches and more details.

Development Mode

For local development, you can use devVedatrace to log to the console without sending data to VedaTrace.

import { devVedatrace } from 'vedatrace'

// Console-only logging for development
const logger = devVedatrace({ service: 'my-service' })

logger.info('This is a test log')

On this page