> ## 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.

# Stdio

> Go SDK reference for sandbox.Stdio — start interactive subprocesses with bidirectional stdin/stdout/stderr.

The Go SDK exposes stdio through `sandbox.Stdio`. Use
`Stdio.Start()` to launch a process with an open stdin pipe, then
send data, receive output, and close stdin or kill the process.

For conceptual background see the
[Stdio feature overview](/features/stdio).

## `sandbox.Stdio.Start(ctx, cmd, opts)` → `(*StdioHandle, error)`

Start a subprocess with an open stdin pipe.

```go theme={null}
handle, err := sandbox.Stdio.Start(ctx, "cat", &declaw.StdioStartOpts{
    Envs: map[string]string{"FOO": "bar"},
    User: "user",
    Cwd:  "/workspace",
})
```

### `StdioStartOpts`

| Field  | Type                | Default  | Description                                        |
| ------ | ------------------- | -------- | -------------------------------------------------- |
| `User` | `string`            | `"user"` | User the process runs as.                          |
| `Cwd`  | `string`            | `""`     | Working directory.                                 |
| `Envs` | `map[string]string` | `nil`    | Environment variables merged into the process env. |

Pass `nil` for opts to use all defaults.

## `StdioHandle`

Handle for an interactive subprocess with stdin pipe.

### Fields

* `handle.CmdID string` — server-assigned command identifier.

### Methods

#### `handle.SendStdin(ctx, data) error`

Send data to the process's stdin. Data is `[]byte`.

```go theme={null}
handle.SendStdin(ctx, []byte("hello\n"))
```

#### `handle.CloseStdin(ctx) error`

Close the process's stdin pipe, sending EOF.

#### `handle.Kill(ctx) error`

Terminate the process.

#### `handle.Wait(ctx) (*StdioResult, error)`

Block until the process exits. Equivalent to calling `Stream` with no
callbacks.

```go theme={null}
result, err := handle.Wait(ctx)
fmt.Println(result.ExitCode)
```

#### `handle.Stream(ctx, opts) (*StdioResult, error)`

Connect to the SSE output stream and deliver stdout/stderr chunks via
callbacks. Blocks until the process exits or the context is cancelled.

```go theme={null}
result, err := handle.Stream(ctx, &declaw.StdioStreamOpts{
    OnStdout: func(data []byte) {
        fmt.Print(string(data))
    },
    OnStderr: func(data []byte) {
        fmt.Fprint(os.Stderr, string(data))
    },
})
```

### `StdioStreamOpts`

| Field      | Type                | Description                             |
| ---------- | ------------------- | --------------------------------------- |
| `OnStdout` | `func(data []byte)` | Callback invoked for each stdout chunk. |
| `OnStderr` | `func(data []byte)` | Callback invoked for each stderr chunk. |

Pass `nil` for opts to drain the stream without callbacks (same as `Wait`).

## `StdioResult`

```go theme={null}
type StdioResult struct {
    ExitCode int
}
```

`ExitCode` is `-1` if the stream ended without a clean exit frame.
