Access Private Resources from Daytona Sandboxes

Connect Daytona sandboxes to Twingate so the agent in each sandbox can reach only the private Resources you assign to it.

Daytona runs AI-generated code in isolated sandboxes that you create and control through the Daytona SDK (Daytona docs). Each sandbox connects to Twingate as a Service and has access only to the Resources you assign to that Service, such as a staging API or an internal package registry. Only the connections that the sandbox opens to those Resources go through Twingate (Daytona’s Twingate guide).

How it works

For each sandbox, your code does the following before the agent starts:

  • Creates the sandbox with the Daytona SDK.
  • Installs the Twingate Client in the sandbox.
  • Uploads the Service Key, runs headless setup, and deletes the uploaded key file.
  • Starts the Client and waits until twingate status prints online.
  • Starts the agent. Its connections to the Service’s Resources go through Twingate, and its other traffic goes to the internet as before.

The examples on this page are based on the ones in Daytona’s Twingate guide.

Prerequisites

  • A Daytona organization on Tier 3 or higher (Daytona tiers).
  • Daytona’s default network policy. Under it, a sandbox on Tier 3 or Tier 4 has full internet access (network limits).
  • A Daytona API key in the DAYTONA_API_KEY environment variable, which the SDK reads (Daytona environment variables).
  • The Daytona SDK for Python (daytona) or TypeScript (@daytona/sdk).

Step 1: Create a Service

Set up a Service for the agent (Create a New Service):

  • Create a Service.
  • Assign it the Resources the agent needs to reach.
  • Generate a Service Key with a reasonable expiration (Service Key lifecycle).

Step 2: Give your code the key

Store the key wherever you keep your other secrets, and make it available as the TWINGATE_SERVICE_KEY environment variable. Your code will use it to set up the Client in each sandbox.

Step 3: Connect each sandbox

The connect_to_twingate function installs the Client, runs headless setup with the key, starts the Client, and checks every 5 seconds, up to 24 times, for it to come online. Call it after you create each sandbox and before you start the agent.

Python

import os
import time
from daytona import Daytona
daytona = Daytona() # Reads DAYTONA_API_KEY
def run(sandbox, command, timeout=60):
response = sandbox.process.exec(command, timeout=timeout)
if response.exit_code != 0:
raise RuntimeError(f"{command} failed: {response.result}")
return response.result.strip()
def connect_to_twingate(sandbox):
run(sandbox, "curl -fsSL -o /tmp/twingate-install.sh https://binaries.twingate.com/client/linux/install.sh && sudo bash /tmp/twingate-install.sh", timeout=300)
sandbox.fs.upload_file(os.environ["TWINGATE_SERVICE_KEY"].encode(), "/tmp/service_key.json")
try:
run(sandbox, "sudo twingate setup --headless /tmp/service_key.json")
finally:
run(sandbox, "rm -f /tmp/service_key.json")
run(sandbox, "sudo twingate service-start")
for _ in range(24):
if sandbox.process.exec("twingate status", timeout=30).result.strip() == "online":
return
time.sleep(5)
raise RuntimeError("Twingate didn't come online:\n" + run(sandbox, "tail -n 50 /var/log/twingated.log"))
sandbox = daytona.create()
connect_to_twingate(sandbox)
# Start your agent in the sandbox here.

TypeScript

import { Daytona, Sandbox } from '@daytona/sdk'
const daytona = new Daytona() // Reads DAYTONA_API_KEY
async function run(sandbox: Sandbox, command: string, timeout = 60): Promise<string> {
const response = await sandbox.process.executeCommand(command, undefined, undefined, timeout)
if (response.exitCode !== 0) throw new Error(`${command} failed: ${response.result}`)
return response.result.trim()
}
async function connectToTwingate(sandbox: Sandbox): Promise<void> {
await run(sandbox, 'curl -fsSL -o /tmp/twingate-install.sh https://binaries.twingate.com/client/linux/install.sh && sudo bash /tmp/twingate-install.sh', 300)
await sandbox.fs.uploadFile(Buffer.from(process.env.TWINGATE_SERVICE_KEY!), '/tmp/service_key.json')
try {
await run(sandbox, 'sudo twingate setup --headless /tmp/service_key.json')
} finally {
await run(sandbox, 'rm -f /tmp/service_key.json')
}
await run(sandbox, 'sudo twingate service-start')
for (let i = 0; i < 24; i++) {
const status = await sandbox.process.executeCommand('twingate status', undefined, undefined, 30)
if (status.result.trim() === 'online') return
await new Promise((resolve) => setTimeout(resolve, 5000))
}
throw new Error("Twingate didn't come online:\n" + (await run(sandbox, 'tail -n 50 /var/log/twingated.log')))
}
async function main() {
const sandbox = await daytona.create()
await connectToTwingate(sandbox)
// Start your agent in the sandbox here.
}
main()

Step 4: Verify

After connect_to_twingate returns, run these commands in the sandbox. In Python:

print(run(sandbox, "twingate resources"))
print(run(sandbox, "curl -sS -o /dev/null -w '%{http_code}' https://staging.internal.example.com"))

In TypeScript, add these lines inside main(), after connectToTwingate:

console.log(await run(sandbox, 'twingate resources'))
console.log(await run(sandbox, "curl -sS -o /dev/null -w '%{http_code}' https://staging.internal.example.com"))

Replace https://staging.internal.example.com with the URL of a web Resource. The first command will list the Service’s Resources, and the second will print the HTTP status code that the Resource returns, such as 200. For a Resource that isn’t a web service, such as a database, connect to it with a client for its protocol instead.

Network access

On Tier 3 and Tier 4, a sandbox has full internet access by default, and the examples on this page use that default. Daytona’s networkAllowList, domainAllowList, and networkBlockAll settings restrict outbound traffic (Daytona network limits), and they can block the install or the Client’s connections. The ports the Client needs are listed in Endpoint Requirements.

Troubleshooting

The Client writes its log to /var/log/twingated.log in the sandbox (Daytona’s Twingate guide). Sandboxes may connect to Resources through Relays instead of peer-to-peer, which can add latency (Daytona’s Twingate guide).

  • If the install command or the Client can’t connect to the internet and your organization is on Tier 1 or Tier 2, you need to move to Tier 3 or higher. On Tier 3 or Tier 4, check that the sandbox doesn’t have an allow list or networkBlockAll set.
  • If the Client doesn’t come online and the log shows Invalid service key, check the value of TWINGATE_SERVICE_KEY. A Daytona secret won’t work, because inside the sandbox it’s a placeholder instead of the key.
  • If twingate status prints not-running after a sandbox stops and starts again, run sudo twingate service-start. You don’t need to run setup again.
  • If a command fails with a timeout, pass a longer timeout to run.

For anything else, see Troubleshooting in Linux Headless Mode.

Last updated