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# npm2npm install @logvault/client34# yarn5yarn add @logvault/client67# pnpm8pnpm add @logvault/client
Quick Start
TypeScript
1import { LogVault } from '@logvault/client';23// Initialize the client4const client = new LogVault('lv_live_your_key_here');56// Log an event7await 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', // Default3 timeout: 30000, // Request timeout in ms4 maxRetries: 3 // Retry failed requests5});
TypeScript Types
TypeScript
1import { LogVault, LogEvent } from '@logvault/client';23const event: LogEvent = {4 action: 'user.login',5 userId: 'user_123',6 resource: 'dashboard',7 metadata: {8 ip: '192.168.1.1'9 }10};1112await client.log(event);
Error Handling
TypeScript
1import {2 LogVault,3 AuthenticationError,4 ValidationError,5 RateLimitError6} from '@logvault/client';78try {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.ts2'use server';34import { LogVault } from '@logvault/client';56const client = new LogVault(process.env.LOGVAULT_API_KEY!);78export 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';34const app = express();5const audit = new LogVault(process.env.LOGVAULT_API_KEY!);67app.post('/login', async (req, res) => {8 // ... authentication logic ...910 // 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);1617 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 chain2const result = await client.verifyChain();3console.log(`Chain valid: ${result.is_valid}`);4console.log(`Events verified: ${result.events_checked}`);56// Get chain statistics7const stats = await client.getChainStats();8console.log(`Chain coverage: ${stats.chain_coverage}%`);910// Get cryptographic proof for a specific event11const proof = await client.getEventProof('event_id_here');12console.log(`Chain hash: ${proof.proof.chain_hash}`);1314// 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 events2const results = await client.searchEvents('failed login attempts');3for (const event of results.results) {4 console.log(`${event.action} - ${event.user_id}`);5}