Use sandbox.getHost(port) to get a public access URL for a specific port in the sandbox. This is suitable for accessing HTTP services, development servers, or debugging services started inside the sandbox.
Start and access a service
TypeScript example:
import { Sandbox } from "e2b";
const sandbox = await Sandbox.create("code-interpreter-v1", {
apiKey: process.env.E2B_API_KEY,
apiUrl: process.env.E2B_API_URL,
domain: process.env.E2B_DOMAIN,
});
try {
const process = await sandbox.commands.run("python3 -m http.server 8000", {
background: true,
});
const host = sandbox.getHost(8000);
const url = `https://${host}`;
console.log(url);
const response = await fetch(url);
console.log(await response.text());
await process.kill();
} finally {
await sandbox.kill();
}Python example:
import os
import urllib.request
from e2b import Sandbox
sandbox = Sandbox.create(
template="code-interpreter-v1",
api_key=os.environ["E2B_API_KEY"],
api_url=os.environ["E2B_API_URL"],
domain=os.environ["E2B_DOMAIN"],
)
try:
process = sandbox.commands.run(
"python3 -m http.server 8000",
background=True,
)
host = sandbox.get_host(8000)
url = f"https://{host}"
print(url)
with urllib.request.urlopen(url, timeout=10) as response:
print(response.read().decode())
process.kill()
finally:
sandbox.kill()Notes
Before calling
getHost(port), make sure a service inside the sandbox is already listening on that port.getHost(port)returns a host value, so you usually need to prependhttps://when constructing the URL.Background services do not stop automatically. Stop the process or terminate the sandbox when the task finishes.
Do not expose sensitive data, internal debugging interfaces, or long-lived credentials through unauthenticated port services.