> ## Documentation Index
> Fetch the complete documentation index at: https://docs.declaw.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# PTY

> Go SDK reference for sbx.PTY — create and drive interactive pseudo-terminals inside the sandbox.

```go theme={null}
import "github.com/declaw-ai/declaw-go"
```

`sbx.PTY` is the `*PTY` sub-object available on every `Sandbox` instance. It provides methods to create interactive terminal sessions, send input, resize, stream output, and kill sessions.

## `sbx.PTY.Create()`

Create a new PTY session. The sandbox spawns an interactive shell and returns a `*PtyHandle`.

```go theme={null}
handle, err := sbx.PTY.Create(ctx, declaw.PtySize{Cols: 120, Rows: 30})
fmt.Println("PID:", handle.PID)
```

If no `PtySize` is provided, the default 80x24 is used:

```go theme={null}
handle, err := sbx.PTY.Create(ctx)
```

<ParamField body="size" type="PtySize" default="PtySize{Cols: 80, Rows: 24}">
  Initial terminal dimensions (optional variadic argument).
</ParamField>

**Returns** `(*PtyHandle, error)`

***

## PtyHandle

`PtyHandle` is returned by `PTY.Create()`. It exposes the full session lifecycle.

### `handle.PID`

**Type** `int` — the process ID of the shell process backing the PTY.

### `handle.SendInput()`

Forward keystrokes or text to the shell.

```go theme={null}
err := handle.SendInput(ctx, []byte("echo hello\n"))
err = handle.SendInput(ctx, []byte{0x03}) // Ctrl-C
```

<ParamField body="data" type="[]byte" required>
  Raw bytes to send to the PTY stdin.
</ParamField>

**Returns** `error`

***

### `handle.SetSize()`

Change the remote terminal dimensions. Fires `SIGWINCH` inside so ncurses apps redraw.

```go theme={null}
err := handle.SetSize(ctx, 160, 50)
```

<ParamField body="cols" type="int" required>
  Number of columns.
</ParamField>

<ParamField body="rows" type="int" required>
  Number of rows.
</ParamField>

**Returns** `error`

***

### `handle.Stream()`

Returns a read-only channel that receives output data from the PTY as `[]byte` chunks. The channel is closed when the PTY session ends or the context is canceled.

```go theme={null}
ch, err := handle.Stream(ctx)
if err != nil {
    log.Fatal(err)
}
for data := range ch {
    fmt.Print(string(data))
}
```

The stream uses Server-Sent Events (SSE). Output chunks are base64-decoded automatically.

**Returns** `(<-chan []byte, error)`

***

### `handle.Kill()`

Terminate the PTY session (SIGKILL to the process group).

```go theme={null}
err := handle.Kill(ctx)
```

**Returns** `error`

***

## `PtySize`

```go theme={null}
type PtySize struct {
    Cols int
    Rows int
}
```

***

## Example: interactive PTY session

```go theme={null}
ctx := context.Background()

// Create PTY
handle, err := sbx.PTY.Create(ctx, declaw.PtySize{Cols: 100, Rows: 30})
if err != nil {
    log.Fatal(err)
}
defer handle.Kill(ctx)

// Send a command
err = handle.SendInput(ctx, []byte("ls -la && exit\n"))
if err != nil {
    log.Fatal(err)
}

// Stream output
ch, err := handle.Stream(ctx)
if err != nil {
    log.Fatal(err)
}
for data := range ch {
    fmt.Print(string(data))
}
```
