openclaw-node
Node.js bindings for the community Rust implementation of OpenClaw
Part of openclaw-rs, a community Rust implementation of OpenClaw. This package provides napi-rs bindings exposing the Rust core to Node.js applications.
Features
- AI Providers: Anthropic Claude and OpenAI GPT clients with streaming support
- Authentication: Safe API key handling with encrypted credential storage (AES-256-GCM)
- Event Store: Append-only event storage with CRDT projections
- Tool Registry: Register and execute tools from JavaScript
- Configuration: Load and validate OpenClaw config files
- Validation: Input validation and session key building
Installation
# From npm (when published)
# From source
Quick Start
const = require;
// Create an Anthropic provider
const anthropic = ;
// Create a completion
const response = await anthropic.;
console.log;
API Reference
Providers
AnthropicProvider
// Create with API key
const provider = ;
// Or with custom base URL (for proxies)
const provider = ;
// Get provider name
console.log; // "anthropic"
// List available models
const models = await provider.;
// Create completion
const response = await provider.;
console.log;
console.log; // { inputTokens, outputTokens, ... }
OpenAIProvider
// Create with API key
const provider = ;
// With organization ID
const provider = ;
// With custom base URL (for Azure, LocalAI, etc.)
const provider = ;
// Same API as AnthropicProvider
const response = await provider.;
Streaming
provider.;
Request/Response Types
interface JsMessage {
role: 'user' | 'assistant' | 'system' | 'tool';
content: string;
toolUseId?: string; // For tool results
toolName?: string; // For tool results
}
interface JsCompletionRequest {
model: string;
messages: JsMessage[];
system?: string;
maxTokens: number;
temperature?: number;
stop?: string[];
tools?: JsTool[];
}
interface JsCompletionResponse {
id: string;
model: string;
content: string;
stopReason?: 'end_turn' | 'max_tokens' | 'stop_sequence' | 'tool_use';
toolCalls?: JsToolCall[];
usage: JsTokenUsage;
}
interface JsStreamChunk {
chunkType: 'message_start' | 'content_block_start' | 'content_block_delta'
| 'content_block_stop' | 'message_delta' | 'message_stop';
delta?: string;
index?: number;
stopReason?: string;
}
Authentication
NodeApiKey
Safe API key wrapper that prevents accidental logging.
const key = ;
// Safe for logging - shows "[REDACTED]"
console.log; // "[REDACTED]"
// Get actual value when needed for API calls
const secret = key.;
// Check properties without exposing
console.log; // false
console.log; // 13
console.log; // true
CredentialStore
Encrypted credential storage using AES-256-GCM.
const crypto = require;
// Generate a 32-byte encryption key (store this securely!)
const encryptionKey = crypto..;
// Create store
const store = ;
// Store a credential
const apiKey = ;
await store.;
// Load a credential
const loaded = await store.;
console.log; // 'sk-ant-...'
// List stored credentials
const names = await store.; // ['anthropic-main']
// Delete a credential
await store.;
Tool Registry
Register and execute tools from JavaScript.
const = require;
const registry = ;
// Register a tool
registry.;
// List registered tools
console.log; // ['get_weather']
// Execute a tool
const result = await registry.;
console.log; // { success: true, result: ... }
Event Store
Append-only event storage with CRDT projections.
const = require;
// Create event store
const store = ;
// Build a session key
const sessionKey = ;
// Append events
store.;
store.;
// Get all events for a session
const events = JSON.;
// Get materialized projection
const projection = JSON.;
// List all sessions
const sessions = store.;
// Flush to disk
store.;
Configuration
const = require;
// Load config from path
const config = JSON.;
// Load from default location (~/.openclaw/openclaw.json)
const defaultConfig = JSON.;
// Validate a config file
const validation = JSON.;
if else
Validation
const = require;
// Validate message content
const msgResult = JSON.;
if
// Validate file path (prevents traversal attacks)
const pathResult = JSON.;
if
Error Handling
Errors are returned as JSON with structured information:
try catch
Error codes:
PROVIDER_API_ERROR- API returned an errorRATE_LIMITED- Rate limit exceeded (checkretryAfter)NETWORK_ERROR- Network connectivity issueCONFIG_ERROR- Configuration errorCREDENTIAL_NOT_FOUND- Credential not in storeCRYPTO_ERROR- Encryption/decryption failed
Building from Source
# Prerequisites
# - Rust 1.85+
# - Node.js 20+
# Clone the repository
# Build with napi
# Run tests
License
MIT License - see LICENSE for details.