What you’ll learn
- How to create a sandbox from a container image.
- How to wait until the sandbox is ready to accept commands.
- How to run a command and read its output.
- How to stream the sandbox’s logs.
- How to terminate the sandbox so billing stops.
Requirements
- A Runpod account with a positive balance.
- A Runpod API key.
curlandjqfor the command-line examples, or Python 3 with therequestspackage for the Python examples.
Step 1: Create a sandbox
Create a sandbox from thepython:3.12-slim image with a five-minute idle timeout. The Idempotency-Key header makes the request safe to retry if the response is lost:
id, which you use in every later request:
{}) creates a sandbox from the default image. See Create a sandbox for all fields.
By default, the sandbox has open outbound internet access. See Network access.
Step 2: Wait for the sandbox to be ready
Poll the sandbox until itsstate is RUNNING:
FAILED, check terminationReason in the response.
Step 3: Run a command
Send a command to the sandbox as an argv array. Commands aren’t run through a shell, so wrap them in["sh", "-c", "..."] when you need shell syntax such as pipes or &&:
exitCode is 0 when the command succeeds. output always contains stdout and stderr combined.
Step 4: Stream logs
Stream the sandbox’s logs as Server-Sent Events. The stream includes the main process’s stdout (container) and lifecycle events such as image pulls (system). Output from commands you run with exec doesn’t appear here. An open log stream counts as activity, so it keeps the sandbox from timing out as idle.
Ctrl+C to stop streaming.
Step 5: Terminate the sandbox
When you’re done, terminate the sandbox to stop billing:204 with no body. Terminating a sandbox is final: you can still read it with GET, but you can’t restart it.
Next steps
- Learn about timeouts, ports, storage, and network access.
- Extend a running sandbox’s lifetime.
- Browse the full Sandboxes API reference.