JavaScript / TypeScript SDK

The official JavaScript SDK for LogVault. Fully typed with TypeScript support. Works with Node.js, Next.js, and any JavaScript runtime.

Installation

Bash
1# npm
2npm install @logvault/client
3
4# yarn
5yarn add @logvault/client
6
7# pnpm
8pnpm add @logvault/client

Quick Start

TypeScript
1import { LogVault } from '@logvault/client';
2
3// Initialize the client
4const client = new LogVault('lv_live_your_key_here');
5
6// Log an event
7await client.log({
8 action: 'user.login',
9 userId: 'user_123',
10 resource: 'dashboard',
11 metadata: {
12 ip: '192.168.1.1',
13 browser: 'Chrome'
14 }
15});

Configuration Options

TypeScript
1const client = new LogVault('lv_live_...', {
2 baseUrl: 'https://api.logvault.eu', // Default
3 timeout: 30000, // Request timeout in ms
4 maxRetries: 3 // Retry failed requests
5});

TypeScript Types

TypeScript
1import { LogVault, LogEvent } from '@logvault/client';
2
3const event: LogEvent = {
4 action: 'user.login',
5 userId: 'user_123',
6 resource: 'dashboard',
7 metadata: {
8 ip: '192.168.1.1'
9 }
10};
11
12await client.log(event);

Error Handling

TypeScript
1import {
2 LogVault,
3 AuthenticationError,
4 ValidationError,
5 RateLimitError
6} from '@logvault/client';
7
8try {
9 await client.log({ action: 'user.login', userId: '123' });
10} catch (error) {
11 if (error instanceof AuthenticationError) {
12 console.error('Invalid API key');
13 } else if (error instanceof ValidationError) {
14 console.error('Invalid data:', error.message);
15 } else if (error instanceof RateLimitError) {
16 console.error('Rate limited, retry after:', error.retryAfter);
17 }
18}

Next.js Integration (Server Actions)

TypeScript
1// app/actions/audit.ts
2'use server';
3
4import { LogVault } from '@logvault/client';
5
6const client = new LogVault(process.env.LOGVAULT_API_KEY!);
7
8export async function logUserAction(action: string, userId: string) {
9 await client.log({
10 action,
11 userId,
12 metadata: {
13 timestamp: new Date().toISOString()
14 }
15 });
16}

Express.js Integration

TypeScript
1import express from 'express';
2import { LogVault } from '@logvault/client';
3
4const app = express();
5const audit = new LogVault(process.env.LOGVAULT_API_KEY!);
6
7app.post('/login', async (req, res) => {
8 // ... authentication logic ...
9
10 // Log in background (fire-and-forget)
11 audit.log({
12 action: 'user.login',
13 userId: user.id,
14 metadata: { ip: req.ip }
15 }).catch(console.error);
16
17 res.json({ success: true });
18});

Chain Integrity Verification

LogVault uses cryptographic hash chaining to ensure audit logs cannot be tampered with. Use these methods to verify chain integrity:

TypeScript
1// Verify the entire audit log chain
2const result = await client.verifyChain();
3console.log(`Chain valid: ${result.is_valid}`);
4console.log(`Events verified: ${result.events_checked}`);
5
6// Get chain statistics
7const stats = await client.getChainStats();
8console.log(`Chain coverage: ${stats.chain_coverage}%`);
9
10// Get cryptographic proof for a specific event
11const proof = await client.getEventProof('event_id_here');
12console.log(`Chain hash: ${proof.proof.chain_hash}`);
13
14// Verify event locally (offline, zero-trust)
15const localResult = client.verifyEventLocally(proof.event);
16console.log(`Local verification: ${localResult.isValid}`);

Semantic Search

Search your audit logs using natural language queries:

TypeScript
1// Search for events
2const results = await client.searchEvents('failed login attempts');
3for (const event of results.results) {
4 console.log(`${event.action} - ${event.user_id}`);
5}

Next Steps