# Sandbox public URL (/docs/network/public-url)

<!-- agent-signals: reading_time_min: 4 · est_tokens: 2055 · updated: 2026-07-30 -->
Related: [Internet access](/docs/network/internet-access.md), [Restricting public access](/docs/network/restrict-public-access.md), [Proxy tunneling](/docs/network/ip-tunneling.md), [Custom domain](/docs/network/custom-domain.md)

Every sandbox can be reached by a public URL, which lets you expose and connect to services running inside it. This page covers how to get that URL, connect to a server inside the sandbox, and customize the `Host` header services receive.

To control the sandbox's outbound internet access, see [Internet access](/docs/network/internet-access). To require authentication on the public URL, see [Restricting public access](/docs/network/restrict-public-access).

## Sandbox public URL [#sandbox-public-url]

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

<CodeGroup>
  <CodeBlockTabs defaultValue="JavaScript & TypeScript" groupId="javascript-typescript+python">
    <CodeBlockTabsList>
      <CodeBlockTabsTrigger value="JavaScript & TypeScript">
        JavaScript & TypeScript
      </CodeBlockTabsTrigger>

      <CodeBlockTabsTrigger value="Python">
        Python
      </CodeBlockTabsTrigger>
    </CodeBlockTabsList>

    <CodeBlockTab value="JavaScript & TypeScript">
      ```js highlight={6}  
      import { Sandbox } from 'e2b'

      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}`)
      ```
    </CodeBlockTab>

    <CodeBlockTab value="Python">
      ```python highlight={6}  
      from e2b import Sandbox

      sandbox = Sandbox.create()

      # You need to always pass a port number to get the host
      host = sandbox.get_host(3000)
      print(f'https://{host}')
      ```
    </CodeBlockTab>
  </CodeBlockTabs>
</CodeGroup>

The code above will print something like this:

<CodeGroup>
  <CodeBlockTabs defaultValue="JavaScript & TypeScript" groupId="javascript-typescript+python">
    <CodeBlockTabsList>
      <CodeBlockTabsTrigger value="JavaScript & TypeScript">
        JavaScript & TypeScript
      </CodeBlockTabsTrigger>

      <CodeBlockTabsTrigger value="Python">
        Python
      </CodeBlockTabsTrigger>
    </CodeBlockTabsList>

    <CodeBlockTab value="JavaScript & TypeScript">
      ```bash  
      https://3000-i62mff4ahtrdfdkyn2esc.e2b.app
      ```
    </CodeBlockTab>

    <CodeBlockTab value="Python">
      ```bash  
      https://3000-i62mff4ahtrdfdkyn2esc.e2b.app
      ```
    </CodeBlockTab>
  </CodeBlockTabs>
</CodeGroup>

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

## Connecting to a server running inside the sandbox [#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.

<CodeGroup>
  <CodeBlockTabs defaultValue="JavaScript & TypeScript" groupId="javascript-typescript+python">
    <CodeBlockTabsList>
      <CodeBlockTabsTrigger value="JavaScript & TypeScript">
        JavaScript & TypeScript
      </CodeBlockTabsTrigger>

      <CodeBlockTabsTrigger value="Python">
        Python
      </CodeBlockTabsTrigger>
    </CodeBlockTabsList>

    <CodeBlockTab value="JavaScript & TypeScript">
      ```js  
      import { Sandbox } from 'e2b'

      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()
      ```
    </CodeBlockTab>

    <CodeBlockTab value="Python">
      ```python  
      import requests
      from e2b import Sandbox

      sandbox = Sandbox.create()

      # Start a simple HTTP server inside the sandbox.
      process = sandbox.commands.run("python -m http.server 3000", background=True)
      host = sandbox.get_host(3000)
      url = f"https://{host}"
      print('Server started at:', url)

      # Fetch data from the server inside the sandbox.
      response = requests.get(url)
      data = response.text
      print('Response from server inside sandbox:', data)

      # Kill the server process inside the sandbox.
      process.kill()
      ```
    </CodeBlockTab>
  </CodeBlockTabs>
</CodeGroup>

This output will look like this:

<CodeGroup>
  <CodeBlockTabs defaultValue="JavaScript & TypeScript" groupId="javascript-typescript+python">
    <CodeBlockTabsList>
      <CodeBlockTabsTrigger value="JavaScript & TypeScript">
        JavaScript & TypeScript
      </CodeBlockTabsTrigger>

      <CodeBlockTabsTrigger value="Python">
        Python
      </CodeBlockTabsTrigger>
    </CodeBlockTabsList>

    <CodeBlockTab value="JavaScript & TypeScript">
      ```bash  
      Server started at: https://3000-ip3nfrvajtqu5ktoxugc7.e2b.app
      Response from server inside sandbox: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
      <html>
      <head>
      <meta http-equiv="Content-Type" content="text/html; 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>
      ```
    </CodeBlockTab>

    <CodeBlockTab value="Python">
      ```bash  
      Server started at: https://3000-ip3nfrvajtqu5ktoxugc7.e2b.app
      Response from server inside sandbox: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
      <html>
      <head>
      <meta http-equiv="Content-Type" content="text/html; 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>
      ```
    </CodeBlockTab>
  </CodeBlockTabs>
</CodeGroup>

## Masking request host headers [#masking-request-host-headers]

You can customize the `Host` header that gets sent to services running inside the sandbox using the `maskRequestHost` / `mask_request_host` option. This is useful when your application expects a specific host format.

<CodeGroup>
  <CodeBlockTabs defaultValue="JavaScript & TypeScript" groupId="javascript-typescript+python">
    <CodeBlockTabsList>
      <CodeBlockTabsTrigger value="JavaScript & TypeScript">
        JavaScript & TypeScript
      </CodeBlockTabsTrigger>

      <CodeBlockTabsTrigger value="Python">
        Python
      </CodeBlockTabsTrigger>
    </CodeBlockTabsList>

    <CodeBlockTab value="JavaScript & TypeScript">
      ```js  
      import { Sandbox } from 'e2b'

      // Create sandbox with custom host masking
      const sandbox = await Sandbox.create({
        network: {
          maskRequestHost: 'localhost:${PORT}'
        }
      })

      // The ${PORT} variable will be replaced with the actual port number
      // Requests to the sandbox will have Host header set to for example: localhost:8080
      ```
    </CodeBlockTab>

    <CodeBlockTab value="Python">
      ```python  
      from e2b import Sandbox

      # Create sandbox with custom host masking
      sandbox = Sandbox.create(
          network={
              "mask_request_host": "localhost:${PORT}"
          }
      )

      # The ${PORT} variable will be replaced with the actual port number
      # Requests to the sandbox will have Host header set to for example: localhost:8080
      ```
    </CodeBlockTab>
  </CodeBlockTabs>
</CodeGroup>

The `${PORT}` variable in the mask will be automatically replaced with the actual port number of the requested service.
