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

# Get Started

> Create a GPU sandbox, run a command in it, read its output, and terminate it with the Telnyx CLI or curl.

Each tab below does the same thing: it creates a GPU sandbox, runs a command in it, reads the result, and terminates the sandbox. Use the Telnyx CLI from a terminal, or the REST API directly with curl.

> [!NOTE]
>
> The sandbox rents a whole GPU node and is billed per second from the moment it is `running` until it is `terminated`. Each example sets the sandbox's timeout to 30 minutes so a stopped script cannot leave it running indefinitely.

## Before You Start

* Your account must be enabled for the GPU Sandboxes preview. See [Check access](https://developers.telnyx.com/docs/gpu-sandboxes/api-conventions.md#check-access).
* Set `TELNYX_API_KEY` to your Telnyx API key. Don't put it in shared logs.

## Run a Sandbox

> [tab] CLI
>
> Install the Telnyx CLI, `telnyx-edge`, as described in [Installation](https://developers.telnyx.com/docs/edge-compute/reference/cli.md#installation), then store your API key in it:
>
> ```bash theme={null}
> telnyx-edge auth api-key set "$TELNYX_API_KEY"
> ```
>
> The examples use `jq`. Run them in order in the same shell.
>
> **1. Choose a GPU type**
>
> ```bash theme={null}
> telnyx-edge sandbox-gpu gpu-types
>
> GPU=$(telnyx-edge sandbox-gpu gpu-types --json | jq -er '.[0] | "\(.type):\(.allowed_counts[0])"')
> echo "$GPU"
> ```
>
> `gpu-types` lists each GPU type with its GPU count per node, and the CPU, memory, and disk that come with each GPU. `--gpu` takes a type and its count as `TYPE:COUNT`, for example `B300:8`. Currently each type has one count, the number of GPUs on its node, so the second line takes both from the list instead of hard-coding them.
>
> **2. Create a sandbox**
>
> ```bash theme={null}
> SANDBOX=$(telnyx-edge sandbox-gpu create --name quickstart \
>   --image ubuntu:24.04 --gpu "$GPU" \
>   --timeout 1800 --queue-timeout 600 --tag purpose=quickstart \
>   --wait -- sh -c 'echo sandbox started; exec sleep infinity')
> echo "$SANDBOX"
> ```
>
> The command after `--` replaces the image's own: it prints a line, then sleeps so the sandbox stays up for commands. `create` prints the sandbox's ID, and `--wait` returns once the sandbox is `running`. The commands below take the ID or the name.
>
> If the sandbox ends before `running`, `telnyx-edge sandbox-gpu get "$SANDBOX"` shows its `termination_reason`: for example `queue_timeout` when no node became free in time, or `image_pull_failed`.
>
> **3. Run a command**
>
> ```bash theme={null}
> telnyx-edge sandbox-gpu exec "$SANDBOX" -- sh -c 'nvidia-smi | tee /tmp/gpus.txt'
> ```
>
> `nvidia-smi` lists the GPUs you rented. The driver and `nvidia-smi` are provided in every GPU sandbox, so the plain Ubuntu image needs nothing added; see [GPUs](https://developers.telnyx.com/docs/gpu-sandboxes/images.md#gpus). `exec` streams the command's output to your terminal and exits with its exit code. Put the command after `--` so its own flags aren't read as the CLI's.
>
> **4. Read the file**
>
> ```bash theme={null}
> telnyx-edge sandbox-gpu files get "$SANDBOX" /tmp/gpus.txt -
> telnyx-edge sandbox-gpu files ls "$SANDBOX" /tmp
> ```
>
> `-` writes the file to stdout. Without it, the file is saved under its own name in the current directory.
>
> **5. Read the logs**
>
> ```bash theme={null}
> telnyx-edge sandbox-gpu logs "$SANDBOX"
> ```
>
> The logs hold the main process's output, `sandbox started`, but not the exec's. Add `-f` to keep following new output.
>
> **6. Terminate the sandbox**
>
> ```bash theme={null}
> telnyx-edge sandbox-gpu terminate "$SANDBOX" --yes --wait
> ```
>
> The main process gets `SIGTERM`, then `SIGKILL` if it is still running after a grace period. `--yes` skips the confirmation prompt, and `--wait` returns once the sandbox is `terminated`.

> [tab] curl
>
> Run the blocks below in order in the same **Bash** shell with `curl`, `jq`, and `uuidgen` installed. Every ID below comes from your own responses.
>
> **1. Set up requests**
>
> ```bash theme={null}
> set -euo pipefail
> : "${TELNYX_API_KEY:?Set TELNYX_API_KEY to your Telnyx API key first}"
> API="https://api.telnyx.com/v2/ai"
>
> api() {
>   curl --fail-with-body --silent --show-error --connect-timeout 10 \
>     --max-time 60 -H "Authorization: Bearer $TELNYX_API_KEY" "$@"
> }
> ```
>
> **2. Check access**
>
> ```bash theme={null}
> api "$API/sandbox-access" | jq -e '.data.whitelisted'
> ```
>
> `true` means the account is enabled for the preview. `false` means every other GPU Sandboxes endpoint answers `403 sandboxes_not_whitelisted`.
>
> **3. Choose a GPU type**
>
> ```bash theme={null}
> api "$API/sandbox-gpu-types" | jq '.data[] | {type, allowed_counts, cpu, memory_mib, disk_gib, root_disk_gib, prices}'
>
> GPU_TYPE=$(api "$API/sandbox-gpu-types" | jq -er '.data[0].type')
> GPU_COUNT=$(api "$API/sandbox-gpu-types" | jq -er --arg t "$GPU_TYPE" '.data[] | select(.type == $t) | .allowed_counts[0]')
> printf 'Renting a %s node with %s GPUs\n' "$GPU_TYPE" "$GPU_COUNT"
> ```
>
> `allowed_counts` lists the GPU counts a type can be rented with. Currently each type lists one, the number of GPUs on its node, so the script takes it from the list instead of hard-coding it. The sandbox gets that type's `cpu`, `memory_mib`, and `disk_gib` defaults multiplied by the count.
>
> **4. Create a sandbox**
>
> The main process prints a line, then sleeps so the sandbox stays up for execs. The `Idempotency-Key` makes the create safe to retry: a retry with the same key returns the same sandbox.
>
> ```bash theme={null}
> CREATE_BODY=$(jq -n --arg type "$GPU_TYPE" --argjson count "$GPU_COUNT" '{
>   name: "quickstart",
>   image: {ref: "ubuntu:24.04"},
>   command: ["sh", "-c", "echo sandbox started; exec sleep infinity"],
>   gpu: {type: $type, count: $count},
>   timeout: 1800,
>   queue_timeout: 600,
>   tags: {purpose: "quickstart"}
> }')
>
> # Set once, so running this block again after a lost response retries with the same key.
> : "${CREATE_KEY:=$(uuidgen)}"
> SANDBOX=$(api -X POST "$API/sandboxes" \
>   -H "Content-Type: application/json" \
>   -H "Idempotency-Key: $CREATE_KEY" \
>   -d "$CREATE_BODY")
> SANDBOX_ID=$(jq -er '.data.id' <<< "$SANDBOX")
> printf 'Sandbox: %s (%s)\n' "$SANDBOX_ID" "$(jq -r '.data.state' <<< "$SANDBOX")"
> ```
>
> Creation returns `201` with `state: queued`.
>
> **5. Wait until it is running**
>
> ```bash theme={null}
> while :; do
>   SANDBOX=$(api "$API/sandboxes/$SANDBOX_ID")
>   STATE=$(jq -er '.data.state' <<< "$SANDBOX")
>   case "$STATE" in
>     running) break ;;
>     terminated)
>       jq '.data | {termination_reason, termination_message, exit_code}' <<< "$SANDBOX" >&2
>       exit 1 ;;
>   esac
>   printf 'state: %s\n' "$STATE"
>   sleep 5
> done
> jq '.data | {state, ready, started_at, cpu, memory_mib, disk_gib, resolved}' <<< "$SANDBOX"
> ```
>
> `resolved.image` shows the image pinned by digest. If the sandbox ends before `running`, `termination_reason` says why: for example `queue_timeout` when no node became free within `queue_timeout`, or `image_pull_failed` with details in `termination_message`.
>
> **6. Run a command**
>
> ```bash theme={null}
> # Set once, so running this block again retries with the same key and never runs the command twice.
> : "${EXEC_KEY:=$(uuidgen)}"
> EXEC=$(api -X POST "$API/sandboxes/$SANDBOX_ID/execs" \
>   -H "Content-Type: application/json" \
>   -H "Idempotency-Key: $EXEC_KEY" \
>   -d '{"command": ["sh", "-c", "nvidia-smi > /tmp/gpus.txt"]}')
> EXEC_ID=$(jq -er '.data.id' <<< "$EXEC")
>
> # Long-poll: each call returns when the exec ends or after at most 10 s.
> while :; do
>   EXEC=$(api "$API/sandboxes/$SANDBOX_ID/execs/$EXEC_ID?wait=10")
>   case "$(jq -er '.data.status' <<< "$EXEC")" in
>     exited|failed|terminated) break ;;
>   esac
> done
> jq '.data | {status, exit_code, reason}' <<< "$EXEC"
> ```
>
> `nvidia-smi` lists the GPUs you rented; the driver and `nvidia-smi` are provided in every GPU sandbox, see [GPUs](https://developers.telnyx.com/docs/gpu-sandboxes/images.md#gpus). The exec runs in the sandbox's working directory and environment, and writes its output to a file read in the next step. To stream an exec's output and write to its stdin, use the [WebSocket attach](https://developers.telnyx.com/docs/gpu-sandboxes/attach.md).
>
> **7. Read the file**
>
> ```bash theme={null}
> api "$API/sandboxes/$SANDBOX_ID/files?path=/tmp/gpus.txt"
> api "$API/sandboxes/$SANDBOX_ID/files?path=/tmp&list=true" | jq '.data[] | {name, type, size_bytes}'
> ```
>
> A read returns the file's raw bytes. With `list=true`, the endpoint lists the directory as JSON instead.
>
> **8. Read the logs**
>
> ```bash theme={null}
> api "$API/sandboxes/$SANDBOX_ID/logs" | jq '.data[] | {ts, stream, line}'
> ```
>
> Output reaches the logs a few seconds after the process prints it. Add `follow=true` to stream it as Server-Sent Events.
>
> **9. Terminate the sandbox**
>
> ```bash theme={null}
> api -X POST "$API/sandboxes/$SANDBOX_ID/terminate" | jq '.data.state'
>
> until [[ $(api "$API/sandboxes/$SANDBOX_ID/wait?timeout=10" | jq -er '.data.state') == terminated ]]; do :; done
> api "$API/sandboxes/$SANDBOX_ID" | jq '.data | {state, termination_reason, exit_code, terminated_at}'
>
> # Clear the keys so that a later run creates a new sandbox and a new exec.
> unset CREATE_KEY EXEC_KEY
> ```
>
> The main process gets `SIGTERM`, then `SIGKILL` if it is still running after a grace period. The sandbox ends with `termination_reason: terminated_by_user`.

Terminated sandboxes stay readable for at least 30 days.

## Next Steps

* [Lifecycle](https://developers.telnyx.com/docs/gpu-sandboxes/lifecycle.md): states, timeouts, readiness probes, and termination reasons.
* [Execs](https://developers.telnyx.com/docs/gpu-sandboxes/execs.md) and [WebSocket attach](https://developers.telnyx.com/docs/gpu-sandboxes/attach.md): interactive commands and resumable output.
* [Images and environment](https://developers.telnyx.com/docs/gpu-sandboxes/images.md): how the command, working directory, and environment are resolved.
