Expose Ports

Koyeb Sandboxes are currently in public preview.

Port Access

When working with a sandbox code environment, you might need to expose a specific port externally for testing.

Expose a port synchronously

To expose a sandbox port synchronously, use the .expose_port method, passing the port number to expose:

exposed = sandbox.expose_port(8080)

The following code shows launching a server at port 8080, exposing the port, and then making a request to the endpoint to verify exposure:

import os
import time
 
import requests
 
from koyeb import Sandbox
 
 
sandbox.filesystem.write_file(
    "/tmp/test.html", "<h1>Hello from Sandbox!</h1><p>Port 8080</p>"
)
print("Test file created")
 
# Start a simple HTTP server on port 8080
print("\nStarting HTTP server on port 8080...")
process_id = sandbox.launch_process(
    "python3 -m http.server 8080",
    cwd="/tmp",
)
print(f"Server started with process ID: {process_id}")
 
# Wait for server to start
print("Waiting for server to start...")
time.sleep(3)
 
# Expose port 8080
print("\nExposing port 8080...")
exposed = sandbox.expose_port(8080)
print(f"Port exposed: {exposed.port}")
print(f"Exposed at: {exposed.exposed_at}")
 
# Wait a bit for the port to be ready
print("Waiting for port to be ready...")
time.sleep(2)
 
# Make a request to verify it's working
print("\nMaking HTTP request to verify port exposure...")
try:
    response = requests.get(f"{exposed.exposed_at}/test.html", timeout=10)
    response.raise_for_status()
    print(f"✓ Request successful! Status: {response.status_code}")
    print(f"✓ Response content: {response.text.strip()}")
except requests.RequestException as e:
    print(f"⚠ Request failed: {e}")
    print("Note: Port may still be propagating. Try again in a few seconds.")
 
# List processes to show the server is running
print("\nRunning processes:")
processes = sandbox.list_processes()
for process in processes:
    if process.status == "running":
        print(f"  {process.id}: {process.command} - {process.status}")

Expose a port asynchronously

To expose a sandbox port asynchronously, use the .expose_port method, passing the port number to expose:

exposed = await sandbox.expose_port(8080)

The following code shows asynchronously launching a server at port 8080, exposing the port, and then making a request to the endpoint to verify exposure:

import asyncio
import os
 
import requests
 
from koyeb import AsyncSandbox
 
print("\nCreating test file...")
await sandbox.filesystem.write_file(
    "/tmp/test.html", "<h1>Hello from Sandbox!</h1><p>Port 8080</p>"
)
print("Test file created")
 
# Start a simple HTTP server on port 8080
print("\nStarting HTTP server on port 8080...")
process_id = await sandbox.launch_process(
    "python3 -m http.server 8080",
    cwd="/tmp",
)
print(f"Server started with process ID: {process_id}")
 
# Wait for server to start
print("Waiting for server to start...")
await asyncio.sleep(3)
 
# Expose port 8080
print("\nExposing port 8080...")
exposed = await sandbox.expose_port(8080)
print(f"Port exposed: {exposed.port}")
print(f"Exposed at: {exposed.exposed_at}")
 
# Wait a bit for the port to be ready
print("Waiting for port to be ready...")
await asyncio.sleep(2)
 
# Make a request to verify it's working
print("\nMaking HTTP request to verify port exposure...")
try:
    loop = asyncio.get_running_loop()
    response = await loop.run_in_executor(
        None, requests.get, f"{exposed.exposed_at}/test.html"
    )
    response.raise_for_status()
    print(f"✓ Request successful! Status: {response.status_code}")
    print(f"✓ Response content: {response.text.strip()}")
except Exception as e:
    print(f"⚠ Request failed: {e}")
    print("Note: Port may still be propagating. Try again in a few seconds.")
 
# List processes to show the server is running
print("\nRunning processes:")
processes = await sandbox.list_processes()
for process in processes:
    if process.status == "running":
        print(f"  {process.id}: {process.command} - {process.status}")