Skip to main content

Code Sandbox Python SDK

Use the Code Sandbox API to run code in 10+ languages from Python.

Install

pip install code-sandbox-sdk

Or from source in this repo:

pip install -e packages/python-sdk

Usage

Cloud (API key required):

from code_sandbox import Client

client = Client(api_key="your-api-key")

# Execute code
result = client.execute(language="python", code="print(2 + 2)")
print(result.stdout)   # 4
print(result.success)  # True
print(result.exit_code)  # 0

# List runtimes
for r in client.runtimes():
    print(r.language, r.version)

Local Piston (no API key):

from code_sandbox import create_local_client

client = create_local_client(base_url="http://localhost:2000/api/v2")
result = client.execute(language="python", code="print(2 + 2)")
print(result.stdout)   # 4
# If nothing prints, check result.success, result.error, result.stderr

Troubleshooting local Piston: If result.stdout is empty, always check result.error and result.stderr — they will show the real error (e.g. "runtime is unknown" means you need to install the Python runtime inside the Piston container using the Piston CLI). Run the test script: python test_local.py (from this package directory) to see success/stdout/stderr/error.

API

  • Client(api_key, base_url=None) – Create a client. base_url defaults to http://localhost:3001.
  • create_local_client(base_url=None) – Create a client for local Piston (Docker). No API key required.
  • client.execute(language, code, stdin=None, version=None) – Run code. Returns an ExecuteResult with success, stdout, stderr, exit_code, run_time_ms, error, compile_output.
  • client.runtimes() – List supported languages and versions. Returns a list of Runtime objects with language, version, aliases.

Publishing to PyPI

For maintainers: to upload a new version to PyPI, build and upload with:

pip install build twine
python -m build
twine upload dist/* -u __token__ -p <PYPI_API_TOKEN>

PyPI API token (for later reference; keep secure, do not commit to a public repo):

pypi-AgEIcHlwaS5vcmcCJDNmM2ZlNmFmLTYxYmItNGRmYS04NDJlLWI2NzY1YWJlNjI5ZgACKlszLCJlMWZjMmY1NS05NGE0LTRmZDMtYTY0OS05MTdjMzRlMTQ4NDkiXQAABiBsLP17pnp737nYcpNRSdYRDWF8IB6vXD4BqqmjFOVRLw

Or set TWINE_USERNAME=__token__ and TWINE_PASSWORD to the token above when running twine upload dist/*.

Release files for code-sandbox-sdk 1.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for code-sandbox-sdk 1.0.2
File Size Uploaded
code_sandbox_sdk-1.0.2.tar.gz 4.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for code-sandbox-sdk 1.0.2
File Interpreter ABI Platform
code_sandbox_sdk-1.0.2-py3-none-any.whl Python 3 none any Details

Total release size: 9.4 kB

Release files / code_sandbox_sdk-1.0.2.tar.gz

Download URL code_sandbox_sdk-1.0.2.tar.gz
Size 4.5 kB
Tags Source
SHA-256 checksum
How to use checksums
4dc08338a91e71b66164e02cad2339f768d10d20bcf165f6e5a15dfb4c0a0e50
BLAKE2b-256 checksum
How to use checksums
a66ba453adedcf8981aad7162319b476056d305fce00120774ff6fee07eea771
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.2

Release files / code_sandbox_sdk-1.0.2-py3-none-any.whl

Download URL code_sandbox_sdk-1.0.2-py3-none-any.whl
Size 4.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4bb05f6baff9051dde48de47a1fbec03464893f83cdfdbda2006b96df1ba234a
BLAKE2b-256 checksum
How to use checksums
36819211a014aa90ae882353ca080cdaa886c9e293680aaf0e7e2a14618ddc82
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.2

Release history Release notifications | RSS feed

This release

1.0.2 This release

2 release files

1.0.1

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page