Python SDK

The official Python SDK for LogVault. Supports Python 3.8+ with both sync and async clients.

Installation

Bash
1pip install logvault

Quick Start

Python
1import logvault
2
3# Initialize the client
4client = logvault.Client("lv_live_your_key_here")
5
6# Log an event
7client.log(
8 action="user.login",
9 user_id="user_123",
10 resource="dashboard",
11 metadata={
12 "ip": "192.168.1.1",
13 "browser": "Chrome"
14 }
15)

Async Support

For async applications (FastAPI, aiohttp, etc.), use the async client:

Python
1import logvault
2
3# Initialize async client
4client = logvault.AsyncClient("lv_live_your_key_here")
5
6# Log an event (await required)
7await client.log(
8 action="user.login",
9 user_id="user_123"
10)

Configuration Options

Python
1client = logvault.Client(
2 api_key="lv_live_...",
3 base_url="https://api.logvault.eu", # Default
4 timeout=30, # Request timeout in seconds
5 max_retries=3 # Retry failed requests
6)

Error Handling

Python
1from logvault.exceptions import (
2 LogVaultError,
3 AuthenticationError,
4 ValidationError,
5 RateLimitError
6)
7
8try:
9 client.log(action="user.login", user_id="123")
10except AuthenticationError:
11 print("Invalid API key")
12except ValidationError as e:
13 print(f"Invalid data: {e}")
14except RateLimitError:
15 print("Rate limit exceeded, retry later")
16except LogVaultError as e:
17 print(f"LogVault error: {e}")

Django Integration

Python
1# settings.py
2LOGVAULT_API_KEY = os.environ.get("LOGVAULT_API_KEY")
3
4# views.py
5import logvault
6from django.conf import settings
7
8client = logvault.Client(settings.LOGVAULT_API_KEY)
9
10def login_view(request):
11 # ... authentication logic ...
12 client.log(
13 action="user.login",
14 user_id=str(user.id),
15 metadata={"ip": request.META.get("REMOTE_ADDR")}
16 )
17 return redirect("dashboard")

FastAPI Integration

Python
1from fastapi import FastAPI, BackgroundTasks
2import logvault
3
4app = FastAPI()
5client = logvault.AsyncClient(os.environ["LOGVAULT_API_KEY"])
6
7@app.post("/login")
8async def login(background_tasks: BackgroundTasks):
9 # ... authentication logic ...
10
11 # Log in background (non-blocking)
12 background_tasks.add_task(
13 client.log,
14 action="user.login",
15 user_id=user_id
16 )
17 return {"status": "ok"}

Chain Integrity Verification

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

Python
1# Verify the entire audit log chain
2result = client.verify_chain()
3print(f"Chain valid: {result['is_valid']}")
4print(f"Events verified: {result['events_checked']}")
5
6# Get chain statistics
7stats = client.get_chain_stats()
8print(f"Chain coverage: {stats['chain_coverage']}%")
9
10# Get cryptographic proof for a specific event
11proof = client.get_event_proof("event_id_here")
12print(f"Chain hash: {proof['proof']['chain_hash']}")
13
14# Verify event locally (offline, zero-trust)
15local_result = client.verify_event_locally(proof['event'])
16print(f"Local verification: {local_result['is_valid']}")

Semantic Search

Search your audit logs using natural language queries:

Python
1# Search for events
2results = client.search_events("failed login attempts")
3for event in results['results']:
4 print(f"{event['action']} - {event['user_id']}")

Next Steps