TypeScript SDK
The PiVirt TypeScript SDK provides a type-safe client for web applications, dashboards, and Node.js backend services to interact with PiVirt APIs via ConnectRPC.
Installation
Section titled “Installation”npm install @pilab/pivirt# oryarn add @pilab/pivirt# orpnpm add @pilab/pivirtQuick Start
Section titled “Quick Start”import { VMServiceClient } from '@pilab/pivirt';
// Create clientconst client = new VMServiceClient('https://agent.pilab.hu:8443', { tls: { ca: fs.readFileSync('ca.crt'), cert: fs.readFileSync('client.crt'), key: fs.readFileSync('client.key'), },});
// List VMsasync function listVMs() { const vms = await client.listVMs({ namespace: 'production', });
for await (const vm of vms) { console.log(`VM: ${vm.name} (Status: ${vm.status})`); }}
listVMs();Authentication
Section titled “Authentication”import { VMServiceClient } from '@pilab/pivirt';import * as fs from 'fs';
const client = new VMServiceClient('https://agent.pilab.hu:8443', { tls: { ca: fs.readFileSync('ca.crt'), cert: fs.readFileSync('client.crt'), key: fs.readFileSync('client.key'), },});Bearer Token
Section titled “Bearer Token”const client = new VMServiceClient('https://agent.pilab.hu:8443', { metadata: { authorization: `Bearer ${token}`, },});VM Operations
Section titled “VM Operations”Create VM
Section titled “Create VM”import { VMServiceClient, VMSpec, Disk, NetworkInterface } from '@pilab/pivirt';
const client = new VMServiceClient('https://agent.pilab.hu:8443');
const spec: VMSpec = { name: 'web-01', vcpus: 2, memoryBytes: BigInt(4294967296), disks: [ { name: 'root', pool: 'default', sizeBytes: BigInt(21474836480), format: 'QCOW2', bus: 'virtio', } as Disk, ], nics: [ { name: 'eth0', bridge: 'virbr0', model: 'virtio', } as NetworkInterface, ],};
const vm = await client.createVM({ spec, namespace: 'production',});
console.log(`Created VM: ${vm.id}`);Get VM
Section titled “Get VM”const vm = await client.getVM({ id: 'vm-abc123', namespace: 'production',});
console.log(`VM: ${vm.name}, Status: ${vm.status}`);Start/Stop VM
Section titled “Start/Stop VM”// Start VMawait client.startVM({ id: 'vm-abc123', namespace: 'production',});
// Stop VMawait client.stopVM({ id: 'vm-abc123', namespace: 'production',});Migrate VM
Section titled “Migrate VM”const operation = await client.migrateVM({ id: 'vm-abc123', namespace: 'production', targetNode: 'node-02', options: { live: true, bandwidthLimitMbps: 1000, downtimeLimitMs: 500, compress: true, },});
console.log(`Migration started: ${operation.id}`);Storage Operations
Section titled “Storage Operations”Create Volume
Section titled “Create Volume”import { StorageServiceClient } from '@pilab/pivirt';
const client = new StorageServiceClient('https://agent.pilab.hu:8443');
const volume = await client.createVolume({ name: 'data-01', pool: 'default', sizeBytes: BigInt(107374182400), format: 'QCOW2',});
console.log(`Created volume: ${volume.id}`);Create Snapshot
Section titled “Create Snapshot”const snapshot = await client.createSnapshot({ volumeId: 'vol-abc123', name: 'backup-01',});
console.log(`Created snapshot: ${snapshot.id}`);Error Handling
Section titled “Error Handling”import { PivirtError, ErrorCode } from '@pilab/pivirt';
try { const vm = await client.getVM({ id: 'vm-abc123' });} catch (error) { if (error instanceof PivirtError) { switch (error.code) { case ErrorCode.NOT_FOUND: console.log('VM not found'); break; case ErrorCode.PERMISSION_DENIED: console.log('Permission denied'); break; case ErrorCode.UNAVAILABLE: console.log('Service unavailable'); break; default: console.error(`Error: ${error.message}`); } }}TypeScript Types
Section titled “TypeScript Types”import { VM, VMSpec, VMStatus, Disk, NetworkInterface, Volume, Snapshot, // ... more types} from '@pilab/pivirt';
// Use types in your codeconst vm: VM = { id: 'vm-abc123', name: 'web-01', status: VMStatus.RUNNING, // ...};Examples
Section titled “Examples”See the examples directory for complete examples.