File size: 1,083 Bytes
841bffd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
"""E2B client - sandboxed code execution + filesystem (HTTPS API surface)."""
import os
import requests

BASE = os.getenv("E2B_API_BASE", "https://api.e2b.dev")
KEY = os.getenv("E2B_API_KEY")


def _h():
    if not KEY:
        raise RuntimeError("E2B_API_KEY not set")
    return {"X-API-Key": KEY, "Content-Type": "application/json"}


def create_sandbox(template="base", **kwargs):
    return requests.post(f"{BASE}/v1/sandboxes", headers=_h(), json={"template": template, **kwargs}, timeout=120).json()


def get_sandbox(sandbox_id: str):
    return requests.get(f"{BASE}/v1/sandboxes/{sandbox_id}", headers=_h(), timeout=60).json()


def list_sandboxes():
    return requests.get(f"{BASE}/v1/sandboxes", headers=_h(), timeout=60).json()


def kill_sandbox(sandbox_id: str):
    return requests.delete(f"{BASE}/v1/sandboxes/{sandbox_id}", headers=_h(), timeout=60).json()


def run_code(sandbox_id: str, code: str, language="python"):
    return requests.post(f"{BASE}/v1/sandboxes/{sandbox_id}/runs", headers=_h(), json={"code": code, "language": language}, timeout=120).json()