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.2.tar.gz (4.5 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.2-py3-none-any.whl (4.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: code_sandbox_sdk-1.0.2.tar.gz
  • Upload date:
  • Size: 4.5 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.2.tar.gz
Algorithm Hash digest
SHA256 4dc08338a91e71b66164e02cad2339f768d10d20bcf165f6e5a15dfb4c0a0e50
MD5 6db9b4accb721531db4d50ce2bd2cd5c
BLAKE2b-256 a66ba453adedcf8981aad7162319b476056d305fce00120774ff6fee07eea771

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for code_sandbox_sdk-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 4bb05f6baff9051dde48de47a1fbec03464893f83cdfdbda2006b96df1ba234a
MD5 00b5654855a694ae5d37057e96ff0501
BLAKE2b-256 36819211a014aa90ae882353ca080cdaa886c9e293680aaf0e7e2a14618ddc82

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