WBX Sandbox
Tutorials

Run a web app with a public URL

Start a web app, an API or a dev server inside a sandbox and open it from any browser or phone. Each port of the sandbox can get its own HTTPS address, protected by a token – handy for previews, demos, and letting an agent show what it built.

What you'll build

A tiny web app (a page and a JSON endpoint) running in a sandbox, a private https:// link you can open in a browser, and a call to its API from your own program. This is exactly what Claude did for the Snake game in Your first result – here from your code.

What you need

  • A WBX Sandbox API key in WBXSANDBOX_API_KEY; pip install wbxsandbox or npm install wbxsandbox.
  • About 10 minutes.
  • Using an AI assistant instead? Ask: “Use WBX Sandbox to run this app and give me a link I can open.”

Steps

  1. The whole script

    Three rules make the address work: the app listens on 0.0.0.0 (not only 127.0.0.1), it runs in the background so the command returns, and the sandbox lives long enough – here one hour.

    # webapp.py
    import urllib.request
    from wbxsandbox import Sandbox
    
    APP = r'''
    import json, time
    from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
    START = time.time()
    
    class Handler(BaseHTTPRequestHandler):
        def do_GET(self):
            if self.path == "/api/uptime":
                body, ctype = json.dumps({"uptime_s": round(time.time() - START, 1)}).encode(), "application/json"
            else:
                body, ctype = b"<h1>Hello from a WBX sandbox</h1><a href='/api/uptime'>uptime</a>", "text/html; charset=utf-8"
            self.send_response(200)
            self.send_header("Content-Type", ctype)
            self.send_header("Content-Length", str(len(body)))
            self.end_headers()
            self.wfile.write(body)
    
    ThreadingHTTPServer(("0.0.0.0", 8000), Handler).serve_forever()
    '''
    WAIT = "for i in $(seq 1 30); do curl -sf -o /dev/null http://127.0.0.1:8000/ && exit 0; sleep 0.5; done; exit 1"
    
    sb = Sandbox.create(template="base", timeout=3600)        # one hour; extend with sb.set_timeout()
    try:
        sb.files.write("/home/user/app.py", APP)
        sb.run("nohup python3 /home/user/app.py >/tmp/app.log 2>&1 &")   # in the background
        sb.run(WAIT, timeout=30).check()                        # until it answers inside the sandbox
    
        u = sb.get_url(8000)
        print("Open in a browser:", u["url_with_token"])
    
        # from code: the address without the token + the X-WBX-Token header
        req = urllib.request.Request(u["url"].rstrip("/") + "/api/uptime", headers={"X-WBX-Token": u["token"]})
        print(urllib.request.urlopen(req, timeout=30).read().decode())
    
        input("Press Enter to stop the app and delete the sandbox… ")
    finally:
        sb.kill()
    // webapp.mts (an ES module – top-level await)
    import { createInterface } from "node:readline/promises";
    import { WBX } from "wbxsandbox";
    
    const APP = `
    import json, time
    from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
    START = time.time()
    
    class Handler(BaseHTTPRequestHandler):
        def do_GET(self):
            if self.path == "/api/uptime":
                body, ctype = json.dumps({"uptime_s": round(time.time() - START, 1)}).encode(), "application/json"
            else:
                body, ctype = b"<h1>Hello from a WBX sandbox</h1><a href='/api/uptime'>uptime</a>", "text/html; charset=utf-8"
            self.send_response(200)
            self.send_header("Content-Type", ctype)
            self.send_header("Content-Length", str(len(body)))
            self.end_headers()
            self.wfile.write(body)
    
    ThreadingHTTPServer(("0.0.0.0", 8000), Handler).serve_forever()
    `;
    const WAIT = "for i in $(seq 1 30); do curl -sf -o /dev/null http://127.0.0.1:8000/ && exit 0; sleep 0.5; done; exit 1";
    
    const wbx = new WBX();
    const sb = await wbx.sandboxes.create({ template: "base", timeoutSeconds: 3600 }); // one hour; extend with sb.setTimeout()
    try {
      await sb.files.write("/home/user/app.py", APP);
      const { pid } = await sb.processes.start("python3 /home/user/app.py", { cwd: "/home/user", tag: "web" }); // in the background
      (await sb.commands.run(WAIT, { timeoutMs: 30_000 })).check();  // until it answers inside the sandbox
    
      const u = await sb.getUrl(8000);
      console.log("Open in a browser:", u.url_with_token);
    
      // from code: the address without the token + the X-WBX-Token header
      const res = await fetch(u.url.replace(/\/$/, "") + "/api/uptime", { headers: { "X-WBX-Token": u.token } });
      console.log(res.status, await res.text());
    
      const rl = createInterface({ input: process.stdin, output: process.stdout });
      await rl.question("Press Enter to stop the app and delete the sandbox… ");
      rl.close();
    } finally {
      await sb.kill();
    }
    H=(-H "Authorization: Bearer $WBXSANDBOX_API_KEY")
    API=https://sandbox.wbx.cz
    
    SB=$(curl -s -X POST $API/v1/sandboxes "${H[@]}" -H "Content-Type: application/json" \
      -d '{"template": "base", "timeout_s": 3600}' | jq -r .id)
    
    # a static site is enough to try it: one page, served by Python's built-in server
    echo '<h1>Hello from a WBX sandbox</h1>' |
      curl -s -X PUT "$API/v1/sandboxes/$SB/files?path=/home/user/site/index.html" "${H[@]}" \
        -H "Content-Type: application/octet-stream" --data-binary @-
    curl -s -X POST $API/v1/sandboxes/$SB/exec "${H[@]}" -H "Content-Type: application/json" \
      -d '{"cmd": "nohup python3 -m http.server 8000 --bind 0.0.0.0 --directory /home/user/site >/tmp/web.log 2>&1 &"}'
    
    curl -s "$API/v1/sandboxes/$SB/url?port=8000" "${H[@]}" | tee url.json | jq -r .url_with_token
    curl -s "$(jq -r .url url.json)" -H "X-WBX-Token: $(jq -r .token url.json)"
    
    curl -s -X DELETE $API/v1/sandboxes/$SB "${H[@]}"     # when you are done
  2. Open the link

    Open the printed url_with_token in a browser – on your phone too. The token moves into a cookie for that address and disappears from the address bar. Anyone who has the link can use the app until the sandbox ends, so share it only with people who should see it.

How to tell it works

  • The browser shows Hello from a WBX sandbox; the uptime link returns JSON such as {"uptime_s": 4.2}.
  • Your script prints {"uptime_s": …} from the call with the X-WBX-Token header (TypeScript also prints status 200).
  • After the sandbox is deleted, the same link no longer opens the app.

Common problems

What you seeMeaning and fix
An error page instead of the appThe app is not listening on that port on 0.0.0.0, or it crashed. Look at its log: sb.run("cat /tmp/app.log") (TypeScript starts the app with sb.processes.start() without a log file: for await (const ev of sb.processes.stream(pid)) console.log(ev) replays what it printed – stdout/stderr events, then exit with its exit code if it crashed; sb.processes.list() shows whether it still runs).
The link worked, later it does notThe sandbox's lifetime ended (or it fell asleep), which stops the app. Extend it in time with set_timeout / setTimeout; a sleeping sandbox is not woken by its link (409 sandbox_sleeping) – wake it, start the app again.
Your own call gets refused without the tokenSend X-WBX-Token: <token> with every request, or use url_with_token in a browser.
501 public_urls_disabledPublic addresses are turned off on this platform – contact support.
400 from get_urlThe port must be 1–65535.

HTTP and WebSockets work, over IPv4 and IPv6. Your app gets the visitor's address in X-Forwarded-For. Details: Public URLs.

Next steps