Aller au contenu principal

Reference · JS / TS SDK

JS / TS SDK

Public surface of the grotte npm package. Hand-curated from JSDoc — see the package source for full type signatures.

Install

bash
npm install grotte

The single export everything starts from:

ts
import Sandbox from "grotte";
// or named:
import { Sandbox, AsyncSandbox } from "grotte";

The default Sandbox export is async (every method returns a Promise). There's no separate sync entrypoint in JS — runtime is async by nature.


Sandbox

Lifecycle

MethodReturnsDescription
Sandbox.create(template?, opts?)Promise<Sandbox>Create a new sandbox from a template (or default base).
Sandbox.connect(sandboxId, opts?)Promise<Sandbox>Reconnect to a running sandbox by id.
Sandbox.list(opts?)SandboxPaginatorList sandboxes (running by default). Iterate with for await (const sb of paginator) { ... }.
sandbox.isRunning(opts?)Promise<boolean>Health probe.
sandbox.kill(opts?)Promise<void>Terminate immediately.
sandbox.pause(opts?)Promise<boolean>Pause + persist filesystem.
sandbox.resume(opts?)Promise<this>Resume a paused sandbox. Alias for connect().
ts
const sandbox = await Sandbox.create("base");
const result = await sandbox.commands.run('echo "Hello"');
console.log(result.stdout);
await sandbox.kill();

Timeout / TTL

MethodReturnsDescription
sandbox.setTimeout(ms, opts?)Promise<void>Set total max lifetime (from now).
sandbox.updateTimeout(ms, opts?)Promise<void>Adjust the timeout in flight.
sandbox.refreshTtl(seconds, opts?)Promise<void>Add seconds to the current TTL (heartbeat-style).

Network

MethodReturnsDescription
sandbox.setNetwork({ internet_access }, opts?)Promise<void>Toggle outbound internet access. Defaults to enabled.
ts
await sandbox.setNetwork({ internet_access: false }); // cut egress

Snapshots

MethodReturnsDescription
sandbox.createSnapshot(opts?)Promise<SnapshotInfo>Snapshot the current filesystem state into a reusable template.

URLs

MethodReturnsDescription
sandbox.getHost(port)stringHost-only string (<port>-<id>.sandbox.grotte.parallactic.fr).
sandbox.getUrl(port)stringFull https://… URL — wraps getHost. Prefer this for browser links and HTTP clients.
sandbox.uploadUrl(path?, opts?)Promise<string>Pre-signed upload URL.
sandbox.downloadUrl(path, opts?)Promise<string>Pre-signed download URL.

MCP

MethodReturnsDescription
sandbox.getMcpToken()Promise<string | undefined>Token for MCP gateway access from inside the sandbox.

sandbox.filesystem

MethodReturnsDescription
read(path, opts?)Promise<string | Uint8Array | Blob>Read a file. Format inferred from opts.formattext (default), bytes, blob, stream.
write(path, content, opts?)Promise<void>Write a file. content accepts string, Uint8Array, or a stream.
writeFiles(files, opts?)Promise<void>Batch-write many files in one round-trip.
list(path, opts?)Promise<EntryInfo[]>List directory entries.
makeDir(path, opts?)Promise<boolean>mkdir -p.
rename(src, dst, opts?)Promise<void>Rename / move.
remove(path, opts?)Promise<void>rm -rf.
exists(path, opts?)Promise<boolean>Check if a path exists.
ts
await sandbox.filesystem.write("/tmp/hello.txt", "world");
const contents = await sandbox.filesystem.read("/tmp/hello.txt");
console.log(contents); // "world"

sandbox.commands

MethodReturnsDescription
run(command, opts?)Promise<CommandResult>Run a command, wait for exit, return { stdout, stderr, exitCode }.
connect(pid, opts?)Promise<CommandHandle>Attach to an already-running command (returns a handle with stdout/stderr streams).
list(opts?)Promise<ProcessInfo[]>List currently-running processes.
kill(pid, opts?)Promise<boolean>SIGKILL a process by pid.
sendStdin(pid, data, opts?)Promise<void>Pipe data into a running process's stdin.
closeStdin(pid, opts?)Promise<void>EOF a running process's stdin.
ts
const result = await sandbox.commands.run("python -c 'print(2+2)'");
console.log(result.stdout); // "4\n"

For long-running commands, drop --background-style by passing background: true and reading from the returned handle's streams:

ts
const handle = await sandbox.commands.run("npm run dev", {
  background: true,
});
for await (const chunk of handle.stdout) process.stdout.write(chunk);

Templates (build-time)

The Template builder is for authoring custom templates programmatically — see Defining a template for the full guide.

ts
import { Template } from "grotte";
 
export const template = Template()
  .fromImage("ubuntu:22.04")
  .runCmd("apt-get update && apt-get install -y python3-pip")
  .runCmd("pip install pandas")
  .setWorkdir("/opt/work");

Common option types

Every method accepts an opts object with at least:

KeyTypeDescription
requestTimeoutMsnumberPer-request timeout.
signalAbortSignalCancellation.

Auth is read from process.env.GROTTE_API_KEY by default; override per-call via opts.apiKey.


See also