Skip to main content

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.

This cookbook demonstrates the full stdio API surface through six self-contained demos. Each one starts a process with an open stdin pipe, sends data, and reads the response.

What you’ll learn

  • Sending stdin and receiving stdout via sandbox.stdio.start(cmd)
  • Separate on_stdout and on_stderr callbacks
  • Multi-round interactive conversations (read-echo loop)
  • Iterator / stream protocol for consuming output
  • Closing stdin to signal EOF (proc.close_stdin())
  • Custom environment variables and working directory
  • Killing a long-running process (proc.kill())

Prerequisites

export DECLAW_API_KEY="your-api-key"
export DECLAW_DOMAIN="your-declaw-instance.example.com:8080"
Install the SDK:
pip install declaw

1. Basic echo (cat)

Start cat, send a line, close stdin, and read the echoed output.
from declaw import Sandbox

sbx = Sandbox.create(timeout=300)
chunks = []
proc = sbx.stdio.start("cat", on_stdout=lambda d: chunks.append(d))
proc.send_stdin("hello from stdio!\n")
proc.close_stdin()
result = proc.wait(timeout=10)
print(b"".join(chunks).decode().strip())  # "hello from stdio!"
print(result.exit_code)                    # 0

2. Stdout and stderr callbacks

Receive stdout and stderr on separate callbacks.
out, err = [], []
proc = sbx.stdio.start(
    "sh -c 'echo this-is-stdout; echo this-is-stderr >&2'",
    on_stdout=lambda d: out.append(d),
    on_stderr=lambda d: err.append(d),
)
result = proc.wait(timeout=10)
print(b"".join(out).decode().strip())  # "this-is-stdout"
print(b"".join(err).decode().strip())  # "this-is-stderr"

3. Multi-round interactive conversation

Send multiple lines to a process that reads in a loop.
replies = []
proc = sbx.stdio.start(
    "sh -c 'while read line; do echo \"reply: $line\"; done'",
    on_stdout=lambda d: replies.append(d),
)
for i in range(5):
    proc.send_stdin(f"message {i}\n")
proc.close_stdin()
proc.wait(timeout=10)
# reply: message 0 ... reply: message 4

4. Line counting with EOF

Send lines to wc -l and close stdin so it sees EOF and reports.
out = []
proc = sbx.stdio.start("wc -l", on_stdout=lambda d: out.append(d))
proc.send_stdin("line one\nline two\nline three\n")
proc.close_stdin()
result = proc.wait(timeout=10)
print(b"".join(out).decode().strip())  # "3"

5. Environment variables and working directory

Pass env vars and set the working directory.
out = []
proc = sbx.stdio.start(
    "sh -c 'echo $GREETING from $(pwd)'",
    envs={"GREETING": "hello-env"},
    cwd="/tmp",
    on_stdout=lambda d: out.append(d),
)
proc.wait(timeout=10)
print(b"".join(out).decode().strip())  # "hello-env from /tmp"

6. Kill a long-running process

Start a process, wait briefly, kill it, and read the exit code.
import time

proc = sbx.stdio.start("sleep 300")
time.sleep(1)
killed = proc.kill()          # True
result = proc.wait(timeout=10)
print(result.exit_code)       # -1

Full runnable examples

Complete self-contained scripts with all six demos and cleanup are available in the SDK repositories. Install the SDK (pip install declaw or npm install @declaw/sdk) and run the example directly.