Computer basics
In this guide, we'll walk through the basics of creating, starting, stopping, and managing the lifecycle of a computer.
Create a computer
You can create a computer with the Computer.create
method. The computer will stay alive for 5 minutes by default but you can change it with the defaultTimeoutMs
parameter. After the time passes, the computer will be automatically stopped.
import { Computer } from "@metallichq/sdk";
// Create computer
const computer = await Computer.create({
defaultTimeoutMs: 60_000, // 1 minute
});
// Save the computer ID for later use
console.log(computer.id);
Start and stop a computer
Start and stop a computer with the Computer.start
and Computer.stop
methods. The computer's state (e.g. memory, filesystem, etc.) is automatically persisted across sessions, so you can pick up right where you left off.
import { Computer } from "@metallichq/sdk";
// Start computer
const computer = await Computer.start("your-computer-id");
// Wait for computer to start
await computer.waitForState("started");
// Stop computer
await computer.stop();
Connect to a computer
To connect to a running computer, you can use the Computer.connect
method. This is useful if you want to connect to the same computer from different environments.
import { Computer } from "@metallichq/sdk";
// Connect to computer
const computer = await Computer.connect("your-computer-id");
// Log the computer state
console.log(computer.state);
Destroy a computer
To destroy a computer, you can use the Computer.destroy
method. This will stop the computer and permanently delete all of its resources.
import { Computer } from "@metallichq/sdk";
// Start computer
const computer = await Computer.start("your-computer-id");
// Wait for computer to start
await computer.waitForState("started");
// Destroy computer
await computer.destroy();