Every sandbox has access to the internet and can be reached by a public URL.

Sandbox public URL

Every sandbox has a public URL that can be used to access running services inside the sandbox.

import { Sandbox } from '@e2b/code-interpreter'

const sandbox = await Sandbox.create()

// You need to always pass a port number to get the host
const host = sandbox.getHost(3000)
console.log(`https://${host}`)

// Example output:
// https://3000-idpw1qdrbcciscn2r8lw7-82b888ba.sandbox.novita.ai

The first leftmost part of the host is the port number we passed to the method.

Connecting to a server running inside the sandbox

You can start a server inside the sandbox and connect to it using the approach above.

In this example we will start a simple HTTP server that listens on port 3000 and responds with the content of the directory where the server is started.

import { Sandbox } from '@e2b/code-interpreter'

const sandbox = await Sandbox.create()

// Start a simple HTTP server inside the sandbox.
const process = await sandbox.commands.run('python -m http.server 3000', { background: true })
const host = sandbox.getHost(3000)
const url = `https://${host}`
console.log('Server started at:', url)

// Fetch data from the server inside the sandbox.
const response = await fetch(url);
const data = await response.text();
console.log('Response from server inside sandbox:', data);

// Kill the server process inside the sandbox.
await process.kill()

// Example output:
// Server started at: https://3000-ibbw4zmqp38s77v1vbykj-d0b9e1e2.sandbox.novita.ai
// Response from server inside sandbox: <!DOCTYPE HTML>
// <html lang="en">
// <head>
// <meta charset="utf-8">
// <title>Directory listing for /</title>
// </head>
// <body>
// <h1>Directory listing for /</h1>
// <hr>
// <ul>
// <li><a href=".bash_logout">.bash_logout</a></li>
// <li><a href=".bashrc">.bashrc</a></li>
// <li><a href=".profile">.profile</a></li>
// </ul>
// <hr>
// </body>
// </html>