Skip to main content

Python SDK for Code Sandbox API

Project description

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/*.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

code_sandbox_sdk-1.0.1.tar.gz (4.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

code_sandbox_sdk-1.0.1-py3-none-any.whl (4.9 kB view details)

Uploaded Python 3

File details

Details for the file code_sandbox_sdk-1.0.1.tar.gz.

File metadata

  • Download URL: code_sandbox_sdk-1.0.1.tar.gz
  • Upload date:
  • Size: 4.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.2

File hashes

Hashes for code_sandbox_sdk-1.0.1.tar.gz
Algorithm Hash digest
SHA256 cf87bfb9dc8415f9e963f540b36a7cad987fbc06f9ad17a3ce30c277f3b35fa7
MD5 cb7418539c1724688b1bd89212ae0a67
BLAKE2b-256 fd3ecf4713cbc68ae9d5c70e68753b59a2f85b930514a2caffcf726c05bdfd6a

See more details on using hashes here.

File details

Details for the file code_sandbox_sdk-1.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for code_sandbox_sdk-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 5b8cceb1ce7b4325857a2bdfb0410788cb198c55e495b73a17ca92139ce6e7f4
MD5 fc825ac697ef505549d6c9a2b862d9bd
BLAKE2b-256 578bafab93d2213cddcd529255dac5d36730a4ed9dfcab5d03e075c0ccc4b5c3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page