Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions examples/custom-sandbox-domain-proxy/.env.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# TODO: Get your E2B API key from https://e2b.dev/docs/getting-started/api-key
E2B_API_KEY=""
2 changes: 2 additions & 0 deletions examples/custom-sandbox-domain-proxy/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.env
node_modules
42 changes: 42 additions & 0 deletions examples/custom-sandbox-domain-proxy/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Custom Sandbox Domain Proxy

Example of mapping custom subdomains to your E2B sandboxes. Access sandboxes at `my-app.yourdomain.com` instead of default URLs.

## Tech Stack
- [E2B Code Interpreter SDK](https://github.com/e2b-dev/code-interpreter)
- Express.js

## How It Works
1. Create E2B sandbox, start Python HTTP server inside (port 8000)
2. Generate custom subdomain (e.g., `happy-blue-tiger`)
3. Map subdomain → sandbox ID in `sandboxCustomSubdomains` object
4. Express proxy extracts subdomain from requests, looks up sandbox ID, forwards to E2B sandbox
5. Works with any domain (`.localhost` for dev, your domain in production)

## Setup
### 1. Set up API keys
- Copy `.env.template` to `.env`
- Get your [E2B API KEY](https://e2b.dev/docs/getting-started/api-key) and add it to `.env`

### 2. Install packagesInstall the E2B Code Interpreter SDK and other dependencies

```bash
npm i
```

### 3. Run the example
```bash
npm run start
```

This creates a sandbox, assigns it a custom subdomain, starts the proxy on port 3000, and opens your browser.

Press `Ctrl+C` to stop and cleanup.

## Use Cases
- Branded URLs instead of random IDs
- Multi-tenant apps: one subdomain per user (`user-123.yourapp.com`)
- Named environments: dev/staging/prod

## Learn More
[E2B Documentation](https://e2b.dev/docs)
82 changes: 82 additions & 0 deletions examples/custom-sandbox-domain-proxy/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { Sandbox } from '@e2b/code-interpreter'
import express from 'express'
import { createProxyMiddleware } from 'http-proxy-middleware'
import open from 'open'
import { uniqueNamesGenerator, adjectives, colors, animals } from 'unique-names-generator'
import 'dotenv/config'

// Start sandbox
const sandbox = await Sandbox.create({
apiKey: process.env.E2B_API_KEY,
})
console.log(`Sandbox created: ${sandbox.sandboxId}`)


// Start python file serving server inside of the sandbox
const PORT_IN_SANDBOX = 8000
await sandbox.commands.run(
`python3 -m http.server ${PORT_IN_SANDBOX}`,
{ background: true, cwd: '/', user: 'root' }
)
console.log(`Python HTTP server started on port ${PORT_IN_SANDBOX} inside sandbox`)


// Start express proxy to proxy requests to the sandbox
// Map custom subdomains to sandbox IDs - users can customize these subdomains however they want.
const customSubdomain = uniqueNamesGenerator({
dictionaries: [adjectives, colors, animals],
separator: '-',
length: 3,
})
const sandboxCustomSubdomains: Record<string, string> = {
[customSubdomain]: sandbox.sandboxId,
}

const app = express()
const PROXY_PORT = process.env.PORT ? parseInt(process.env.PORT) : 80

// Proxy middleware that maps custom subdomains to sandbox IDs
app.use((req, res, next) => {
const host = req.headers.host || ''
const subdomain = host.split('.')[0]

// Check if this is a subdomain request (has at least one dot)
if (!host.includes('.')) {
return res.status(400).send('Please use subdomain format: <custom-subdomain>.<domain>')
}

// Look up sandbox ID by custom subdomain
const sandboxId = sandboxCustomSubdomains[subdomain]

if (!sandboxId) {
return res.status(404).send(`Sandbox with subdomain "${subdomain}" not found`)
}

// Get the sandbox hostname for proxying
const sandboxHost = sandbox.getHost(PORT_IN_SANDBOX)

// Create proxy middleware dynamically
const proxy = createProxyMiddleware({
target: `https://${sandboxHost}`,
changeOrigin: true,
secure: true,
})

return proxy(req, res, next)
})

app.listen(PROXY_PORT, () => {
console.log(`Proxy server running on http://localhost:${PROXY_PORT}`)
console.log(`Sandbox ${sandbox.sandboxId} accessible via custom subdomain: "${customSubdomain}"`)
console.log(`Access via: http://${customSubdomain}.localhost:${PROXY_PORT}/`)
})

// Open browser pointing to the custom subdomain
await open(`http://${customSubdomain}.localhost:${PROXY_PORT}/`)

// Keep process alive and handle cleanup
process.on('SIGINT', async () => {
console.log('\nShutting down...')
await sandbox.kill()
process.exit(0)
})
Loading