Skip to content

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.

Terminal window
npm install @pilab/pivirt
# or
yarn add @pilab/pivirt
# or
pnpm add @pilab/pivirt
import { VMServiceClient } from '@pilab/pivirt';
// Create client
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'),
},
});
// List VMs
async function listVMs() {
const vms = await client.listVMs({
namespace: 'production',
});
for await (const vm of vms) {
console.log(`VM: ${vm.name} (Status: ${vm.status})`);
}
}
listVMs();
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'),
},
});
const client = new VMServiceClient('https://agent.pilab.hu:8443', {
metadata: {
authorization: `Bearer ${token}`,
},
});
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}`);
const vm = await client.getVM({
id: 'vm-abc123',
namespace: 'production',
});
console.log(`VM: ${vm.name}, Status: ${vm.status}`);
// Start VM
await client.startVM({
id: 'vm-abc123',
namespace: 'production',
});
// Stop VM
await client.stopVM({
id: 'vm-abc123',
namespace: 'production',
});
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}`);
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}`);
const snapshot = await client.createSnapshot({
volumeId: 'vol-abc123',
name: 'backup-01',
});
console.log(`Created snapshot: ${snapshot.id}`);
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}`);
}
}
}
import {
VM,
VMSpec,
VMStatus,
Disk,
NetworkInterface,
Volume,
Snapshot,
// ... more types
} from '@pilab/pivirt';
// Use types in your code
const vm: VM = {
id: 'vm-abc123',
name: 'web-01',
status: VMStatus.RUNNING,
// ...
};

See the examples directory for complete examples.