Skip to main content

Example: Interactive PTY

You use this pattern when exec is not enough: you need a TTY for REPLs, full-screen TUIs, or a persistent shell that survives disconnects. You connect through sandbox.pty, send input with sendInput / send_input, and optionally resize or run helper commands.

What you accomplish

You open a WebSocket-backed PTY session (details live in OpenAPI under PTY). You choose ephemeral mode (no session id) or persistent mode (createSession / create_session first). You reuse session ids when you want buffered output after reconnect.

Prerequisites

You completed Examples overview. You read the conceptual PTY page.
Python connect is async; you typically use AsyncVoidRun in examples below. TypeScript connect returns a handle you await per your SDK version: follow the package types.

Pattern A: Ephemeral PTY (TypeScript)

You connect without a sessionId. You write input as if you typed into a terminal. You close when finished.

Pattern B: Persistent session (TypeScript)

You create a session, you capture sessionId, you connect with it. You close the WebSocket without deleting the session if you want to reconnect later.

Pattern C: Ephemeral PTY (Python, async)

You mirror the TypeScript flow with AsyncVoidRun.

Pattern D: Persistent session (Python)

You call create_session() on the sync PTY object (HTTP), then await connect with session_id. Inner field names follow the generated models (often session_id on nested data).
You inspect create_session response types in your installed voidrun package if resp.data.data.session_id differs after an OpenAPI regen. The Python SDK page describes VoidRunResponse shapes.

Helpers you use in production

  • runCommand / run_command: send a command and wait for a prompt regex (see SDK README).
  • resize: set terminal geometry when UIs depend on columns/rows.
  • list / deleteSession / delete_session: manage persistent sessions.

Practices you should follow

  • You rate-limit PTY sessions per user; interactive shells are expensive.
  • You delete persistent sessions when you abandon them so you do not leak TTY state. Disconnect alone does not auto-close a persistent session.
  • An open PTY does not keep the sandbox awake: with autoSleep: true, idle auto-snapshot can still run after ~60s without API activity. Use autoSleep: false or periodic API calls for long interactive sessions. See PTY lifecycle and Sandbox lifecycle.
  • You never paste secrets into transcripts you log.

PTY concepts

When to choose PTY vs exec

TypeScript SDK

PTY class surface