# Ports and URLs Each port of a sandbox has its own HTTPS address, private by default. ## The URL of a port ``` https://-.sbx.fdtn.dev ``` `sbx.get_host(8000)` / `sbx.getHost(8000)` returns the host name without `https://`. HTTP and WebSocket work. Serve plain HTTP; TLS ends at our edge. ## Private and public ports - **Private** (default): only requests with the `e2b-traffic-access-token` header. Others get `403`. - **Public**: anyone with the URL. Set it at create. Python: ```python sbx = Sandbox.create("base", network={"allow_public_traffic": True}) ``` TypeScript: ```typescript const sbx = await Sandbox.create('base', { network: { allowPublicTraffic: true } }) ``` CLI: ```bash $ fdtn create base -d --public ``` Do not serve secrets from a public port. Read the token from `sbx.traffic_access_token` / `sbx.trafficAccessToken`. ```bash $ curl https://8000-.sbx.fdtn.dev/ -H "e2b-traffic-access-token: $TOKEN" ``` ## 1. Start a server Run it in the background with timeout `0`, listening on `0.0.0.0`. Python (serve.py): ```python from fdtn import Sandbox sbx = Sandbox.create( "base", timeout=1800, # 30 minutes network={"allow_public_traffic": True}, ) sbx.files.write("/home/user/site/index.html", "

Hello

") server = sbx.commands.run( "python3 -m http.server 8000 --bind 0.0.0.0", cwd="/home/user/site", background=True, timeout=0, ) url = f"https://{sbx.get_host(8000)}" print(url) ``` TypeScript (serve.mjs): ```typescript import { Sandbox } from '@fdtn/sdk' const sbx = await Sandbox.create('base', { timeoutMs: 1_800_000, // 30 minutes network: { allowPublicTraffic: true }, }) await sbx.files.write('/home/user/site/index.html', '

Hello

') const server = await sbx.commands.run('python3 -m http.server 8000 --bind 0.0.0.0', { cwd: '/home/user/site', background: true, timeoutMs: 0, }) const url = `https://${sbx.getHost(8000)}` console.log(url) ``` CLI (terminal): ```bash $ fdtn create base -d --public --timeout 1800 --name web $ fdtn exec -b -c /tmp @web python3 -m http.server 8000 --bind 0.0.0.0 # open https://8000-.sbx.fdtn.dev ``` ## 2. Wait for the server Python: ```python sbx.commands.run( "for i in $(seq 1 30); do curl -sf http://localhost:8000 >/dev/null && exit 0; sleep 1; done; exit 1", timeout=40, ) ``` TypeScript: ```typescript await sbx.commands.run( 'for i in $(seq 1 30); do curl -sf http://localhost:8000 >/dev/null && exit 0; sleep 1; done; exit 1', { timeoutMs: 40_000 }, ) ``` If the server does not answer in 30 seconds, the SDK raises an error. Then open the URL. To stop only the server, call `server.kill()` or `fdtn process kill `. ## Rules - The URL works only while the sandbox runs. - Port 49983 is reserved for the sandbox agent. - Firewalls must allow `*.sbx.fdtn.dev` on port 443. ## Next - [**Preview a web app**: A full tested example.](https://fdtn.dev/docs/examples/preview.md) - [**Security and isolation**: Network rules and secrets.](https://fdtn.dev/docs/security.md) Source: https://fdtn.dev/docs/ports/