TypeScript SDK
Official TypeScript client for the IC GPU Service API. Zero dependencies beyond native fetch, full type safety, async iteration, and automatic retries.
Prerequisites
- Node.js 18 or later (or any runtime with native fetch)
- An IC GPU Service account with an API key (
sk-ic-...)
Getting Started
1
Install the SDK
npm install @ic-gpu/client2
Initialise the client
setup.ts
import { ICGPUClient } from "@ic-gpu/client";
const client = new ICGPUClient({
apiKey: "sk-ic-your-api-key",
});3
Make your first API call
list-instances.ts
import { ICGPUClient } from "@ic-gpu/client";
const client = new ICGPUClient({ apiKey: "sk-ic-your-api-key" });
const instances = await client.instances.list().toArray();
for (const instance of instances) {
console.log(`${instance.name} ${instance.status} ${instance.tier}`);
}Client Options
options.ts
import { ICGPUClient } from "@ic-gpu/client";
const client = new ICGPUClient({
apiKey: "sk-ic-your-api-key", // or use token for OIDC auth
baseUrl: "https://api.gpu.local", // default
timeout: 30_000, // 30 seconds (default)
maxRetries: 3, // automatic retries (default)
});Options Reference
| Option | Type | Default | Description |
|---|---|---|---|
| apiKey | string | - | API key (sk-ic-...) |
| token | string | - | OIDC bearer token |
| baseUrl | string | https://api.gpu.local | API base URL |
| timeout | number | 30000 | Request timeout (ms) |
| maxRetries | number | 3 | Max retry attempts |
GPU Instances
instances.ts
import { ICGPUClient } from "@ic-gpu/client";
const client = new ICGPUClient({ apiKey: "sk-ic-your-api-key" });
// Create an instance
const instance = await client.instances.create({
name: "my-workspace",
tier: "timesliced", // "timesliced" (no memory isolation), "dedicated", or "mig"
inferenceEngine: "vllm",
tags: { project: "research", team: "ml" },
});
console.log(`Created: ${instance.id} Status: ${instance.status}`);
// Get instance details
const details = await client.instances.get(instance.id);
console.log(`SSH: ssh gpuuser@${details.ssh_host} -p ${details.ssh_port}`);
// Stop and start
await client.instances.stop(instance.id);
await client.instances.start(instance.id);
// View logs
const logs = await client.instances.logs(instance.id);
console.log(logs);
// Terminate
await client.instances.delete(instance.id);LLM Inference
llm.ts
import { ICGPUClient } from "@ic-gpu/client";
const client = new ICGPUClient({ apiKey: "sk-ic-your-api-key" });
// Chat completion
const response = await client.llm.chat({
model: "llama-3-8b",
messages: [
{ role: "system", content: "You are a helpful assistant." },
{ role: "user", content: "Explain GPU computing in one paragraph." },
],
temperature: 0.7,
maxTokens: 256,
});
console.log(response.choices[0].message.content);
// Text completion
const completion = await client.llm.complete({
model: "llama-3-8b",
prompt: "The benefits of GPU computing are:",
maxTokens: 256,
});
console.log(completion.choices[0].text);
// Embeddings
const embedding = await client.llm.embed({
model: "bge-large",
input: "GPU computing enables parallel processing",
});
console.log(`Dimensions: ${embedding.data[0].embedding.length}`);
// List available models
const models = await client.llm.models();
for (const m of models.data) {
console.log(` ${m.id}`);
}Model Deployments
models.ts
import { ICGPUClient } from "@ic-gpu/client";
const client = new ICGPUClient({ apiKey: "sk-ic-your-api-key" });
// Deploy a model
const deployment = await client.models.deploy({
modelName: "my-llama",
huggingfaceRepo: "meta-llama/Meta-Llama-3-8B-Instruct",
engine: "vllm",
gpuCount: 1,
minReplicas: 1,
maxReplicas: 3,
});
console.log(`Deploying: ${deployment.model_name}`);
// Check status
const status = await client.models.get("my-llama");
console.log(`Status: ${status.status}`);
// Scale
await client.models.updateScaling("my-llama", {
minReplicas: 2,
maxReplicas: 5,
});
// List all deployments
for await (const model of client.models.list()) {
console.log(` ${model.model_name} ${model.status}`);
}
// Stop
await client.models.stop("my-llama");Virtual Machines
vms.ts
import { ICGPUClient } from "@ic-gpu/client";
const client = new ICGPUClient({ apiKey: "sk-ic-your-api-key" });
// Create a VM
const vm = await client.vms.create({
name: "dev-server",
templateId: "tpl-ubuntu-24",
});
console.log(`Created: ${vm.id} Status: ${vm.status}`);
// Lifecycle management
await client.vms.stop(vm.id);
await client.vms.start(vm.id);
await client.vms.reboot(vm.id);
// List all VMs using async iteration
for await (const v of client.vms.list()) {
console.log(` ${v.name} ${v.status}`);
}
// Delete (must be stopped first)
await client.vms.stop(vm.id);
await client.vms.delete(vm.id);Kubernetes Clusters
clusters.ts
import { ICGPUClient } from "@ic-gpu/client";
import { writeFileSync } from "node:fs";
const client = new ICGPUClient({ apiKey: "sk-ic-your-api-key" });
// Create a cluster
const cluster = await client.clusters.create({ name: "ml-cluster" });
console.log(`Created: ${cluster.id} Status: ${cluster.status}`);
// Download kubeconfig
const kubeconfig = await client.clusters.kubeconfig(cluster.id);
writeFileSync("kubeconfig.yaml", kubeconfig);
console.log("Kubeconfig saved");
// List clusters
for await (const c of client.clusters.list()) {
console.log(` ${c.name} ${c.status}`);
}
// Delete
await client.clusters.delete(cluster.id);API Keys and SSH Keys
keys.ts
import { ICGPUClient } from "@ic-gpu/client";
const client = new ICGPUClient({ apiKey: "sk-ic-your-api-key" });
// Create a scoped API key
const key = await client.apiKeys.create({
name: "ci-pipeline",
scopes: ["instances", "models"],
permissions: ["read", "write"],
});
console.log(`Key prefix: ${key.prefix}`);
// List API keys
for await (const k of client.apiKeys.list()) {
console.log(` ${k.name} scopes=${k.scopes}`);
}
// Upload SSH public key
const sshKey = await client.sshKeys.create({
name: "laptop",
publicKey: "ssh-ed25519 AAAA... user@laptop",
});
// Generate a key pair (private key returned once)
const generated = await client.sshKeys.generate("auto-key");
console.log(`Private key:\n${generated.private_key}`);
// List SSH keys
for await (const sk of client.sshKeys.list()) {
console.log(` ${sk.name} ${sk.fingerprint}`);
}Webhooks
webhooks.ts
import { ICGPUClient } from "@ic-gpu/client";
const client = new ICGPUClient({ apiKey: "sk-ic-your-api-key" });
// Create a webhook
const webhook = await client.webhooks.create({
url: "https://example.com/hooks/gpu",
events: ["instance.created", "instance.terminated", "balance.low"],
});
console.log(`Webhook: ${webhook.id}`);
// Send a test delivery
await client.webhooks.test(webhook.id);
// List webhooks
for await (const w of client.webhooks.list()) {
console.log(` ${w.id} ${w.url} events=${w.events}`);
}
// Delete
await client.webhooks.delete(webhook.id);Tags and Spending Alerts
tags-alerts.ts
import { ICGPUClient } from "@ic-gpu/client";
const client = new ICGPUClient({ apiKey: "sk-ic-your-api-key" });
// Tag a resource
await client.tags.create({
resourceType: "instance",
resourceId: "inst-abc123",
tags: { environment: "production", team: "ml-infra" },
});
// List tags
const tags = await client.tags.list("instance", "inst-abc123");
for (const t of tags.tags) {
console.log(` ${t.key}=${t.value}`);
}
// Remove tags
await client.tags.delete({
resourceType: "instance",
resourceId: "inst-abc123",
keys: ["team"],
});
// Create a spending alert
const alert = await client.alerts.create({
name: "low-balance",
threshold: 10000,
notifyVia: "dashboard",
});
console.log(`Alert: ${alert.id}`);
// List alerts
for await (const a of client.alerts.list()) {
console.log(` ${a.id} threshold=${a.threshold}`);
}Token Balance and Usage
tokens.ts
import { ICGPUClient } from "@ic-gpu/client";
const client = new ICGPUClient({ apiKey: "sk-ic-your-api-key" });
// Check balance
const balance = await client.tokens.balance();
console.log(`Balance: ${balance.balance} tokens`);
// View packages
const packages = await client.tokens.packages();
for (const pkg of packages.packages) {
console.log(` ${pkg.name}: ${pkg.tokens} tokens for ${pkg.price} ${pkg.currency}`);
}
// Purchase tokens
await client.tokens.purchase({ packageId: "pkg-starter" });
// Daily usage (last 30 days)
const daily = await client.tokens.daily(30);
for (const day of daily.daily) {
console.log(` ${day.date}: ${day.tokens_used} tokens`);
}
// Usage by API key
const byKey = await client.tokens.byKey();
for (const entry of byKey.keys) {
console.log(` ${entry.key_name}: ${entry.tokens_used}`);
}Error Handling
errors.ts
import { ICGPUClient } from "@ic-gpu/client";
import {
ICGPUError,
ValidationError,
NotFoundError,
RateLimitError,
AccessDeniedError,
} from "@ic-gpu/client/errors";
const client = new ICGPUClient({ apiKey: "sk-ic-your-api-key" });
try {
await client.instances.get("nonexistent-id");
} catch (error) {
if (error instanceof NotFoundError) {
console.log(`Not found: ${error.message}`);
console.log(` Status: ${error.status}`); // 404
console.log(` Code: ${error.code}`); // "ResourceNotFoundException"
console.log(` Hint: ${error.hint}`);
console.log(` Request ID: ${error.requestId}`);
} else if (error instanceof ValidationError) {
console.log(`Bad request: ${error.message}`);
} else if (error instanceof RateLimitError) {
console.log(`Rate limited: ${error.message}`);
} else if (error instanceof ICGPUError) {
console.log(`API error ${error.status}: ${error.message}`);
} else {
throw error;
}
}Pagination
List methods return a PageIterator that supports async iteration and collecting all results.
pagination.ts
import { ICGPUClient } from "@ic-gpu/client";
const client = new ICGPUClient({ apiKey: "sk-ic-your-api-key" });
// Async iteration (lazy, fetches pages on demand)
for await (const instance of client.instances.list()) {
console.log(instance.name);
}
// Collect all results into an array
const all = await client.instances.list().toArray();
console.log(`Total: ${all.length}`);
// Control page size
for await (const instance of client.instances.list({ maxResults: 10 })) {
console.log(instance.name);
}
// Manual page-by-page fetching
const iterator = client.instances.list({ maxResults: 5 });
const page1 = await iterator.fetchPage();
console.log(`Page 1: ${page1.items.length} items`);
if (page1.nextToken) {
const page2 = await iterator.fetchPage(page1.nextToken);
console.log(`Page 2: ${page2.items.length} items`);
}Complete Example
full-example.ts
import { ICGPUClient } from "@ic-gpu/client";
import { ICGPUError } from "@ic-gpu/client/errors";
async function main() {
const client = new ICGPUClient({ apiKey: process.env.IC_GPU_API_KEY! });
try {
// 1. Check balance
const balance = await client.tokens.balance();
console.log(`Token balance: ${balance.balance}`);
// 2. Create an SSH key
const sshKey = await client.sshKeys.generate("demo-key");
console.log(`SSH key: ${sshKey.id}`);
// 3. Create a GPU instance
const instance = await client.instances.create({
name: "demo-workspace",
tier: "timesliced",
tags: { purpose: "demo" },
});
console.log(`Instance: ${instance.id}`);
// 4. Wait for running status
let status = await client.instances.get(instance.id);
while (status.status !== "running") {
console.log(` Status: ${status.status}...`);
await new Promise((r) => setTimeout(r, 5000));
status = await client.instances.get(instance.id);
}
console.log(`Running at ${status.ssh_host}:${status.ssh_port}`);
// 5. Chat with an LLM
const response = await client.llm.chat({
model: "llama-3-8b",
messages: [{ role: "user", content: "What is CUDA?" }],
maxTokens: 128,
});
console.log(`\nLLM: ${response.choices[0].message.content}`);
// 6. Set up alert
await client.alerts.create({
name: "demo-alert",
threshold: 5000,
notifyVia: "dashboard",
});
// 7. Clean up
await client.instances.delete(instance.id);
await client.sshKeys.delete(sshKey.id);
console.log("\nDone!");
} catch (error) {
if (error instanceof ICGPUError) {
console.error(`API error: ${error.message} (status=${error.status})`);
} else {
throw error;
}
}
}
main();