Open-source, secure environment with real-world tools for enterprise-grade agents.
Python agentaiai-agentai-agentscode-interpretercopilotdevelopmentdevtoolsgptgpt-4javascriptllmnextjsopenaipythonreactsoftwaretypescript
Update Connect to running sandbox
Tereza Tizkovaa05112133d27
diff --git a/apps/docs/src/app/sandbox/api/reconnect/page.mdx b/apps/docs/src/app/sandbox/api/reconnect/page.mdxnew file mode 100644index 000000000..69c367def--- /dev/null+++ b/apps/docs/src/app/sandbox/api/reconnect/page.mdx@@ -0,0 +1,38 @@+# Connect to running sandbox++Disconnect and reconnect to the same sandbox {{ className: 'lead' }}++## Description++The sandboxes are by default kept alive only when connected to them. When you disconnect from a sandbox, it will be destroyed.++If you want to keep the sandbox alive even after disconnecting from it, you can explicitly say for how long you want to keep it alive. You can then disconnect from the sandbox and reconnect to it later. This can be useful for example in a serverless environment or chatbot application.++The duration limit is 1 hour. If you need more, feel free to [reach out to us](/getting-help) with your use.++## Keep sandbox alive++The example below shows how to disconnect from a running sandbox and reconnect to it again while keeping the sandbox alive.++<CodeGroupAutoload+ path="reconnect/reconnect_1"+ isRunnable={false}+/>++Call the `keep_alive` method on the sandbox instance to keep it alive. You can specify a red duration, as a multiple of a default time unit, which is+- 1 ms in JS+- 1 s in Python.++You then disconnect from the sandbox.++<CodeGroupAutoload+ path="reconnect/reconnect_2"+ isRunnable={false}+/>++You can then reconnect to the sandbox from anywhere.++<CodeGroupAutoload+ path="reconnect/reconnect_3"+ isRunnable={false}+/>diff --git a/apps/docs/src/code/js/reconnect/reconnect_1.js b/apps/docs/src/code/js/reconnect/reconnect_1.jsnew file mode 100644index 000000000..bf8d68406--- /dev/null+++ b/apps/docs/src/code/js/reconnect/reconnect_1.js@@ -0,0 +1,7 @@+import { Sandbox } from '@e2b/sdk'++async function wait(ms) {+ return new Promise(resolve => setTimeout(resolve, ms))+}++const sandbox = await Sandbox.create({id: 'base'})\ No newline at end of filediff --git a/apps/docs/src/code/js/reconnect/reconnect_2.js b/apps/docs/src/code/js/reconnect/reconnect_2.jsnew file mode 100644index 000000000..1952de7c7--- /dev/null+++ b/apps/docs/src/code/js/reconnect/reconnect_2.js@@ -0,0 +1,14 @@+// Do something in the sandbox+await sandbox.filesystem.write('hello.txt', 'Hello World!')++// Get the sandbox ID, we'll need it later+const sandboxID = sandbox.id++// Keep alive the sandbox for 1 hour+await sandbox.keepAlive(60 * 60 * 1000) // 1 hour $HighlightLine++// Close the sandbox+await sandbox.close()++// Do something else...+await wait(60 * 1000)\ No newline at end of filediff --git a/apps/docs/src/code/js/reconnect/reconnect_3.js b/apps/docs/src/code/js/reconnect/reconnect_3.jsnew file mode 100644index 000000000..ebfb55198--- /dev/null+++ b/apps/docs/src/code/js/reconnect/reconnect_3.js@@ -0,0 +1,9 @@+// Reconnect to the sandbox+const sandbox2 = await Sandbox.reconnect(sandboxID) // $HighlightLine++// Continue in using the sandbox+const content = await sandbox2.filesystem.read('hello.txt')+console.log(content)++// Close the sandbox+await sandbox2.close()diff --git a/apps/docs/src/code/python/reconnect/reconnect_1.py b/apps/docs/src/code/python/reconnect/reconnect_1.pynew file mode 100644index 000000000..7be57cc63--- /dev/null+++ b/apps/docs/src/code/python/reconnect/reconnect_1.py@@ -0,0 +1,5 @@+import time+from e2b import Sandbox+++sandbox = Sandbox('base')\ No newline at end of filediff --git a/apps/docs/src/code/python/reconnect/reconnect_2.py b/apps/docs/src/code/python/reconnect/reconnect_2.pynew file mode 100644index 000000000..b130926ce--- /dev/null+++ b/apps/docs/src/code/python/reconnect/reconnect_2.py@@ -0,0 +1,14 @@+# Do something in the sandbox+sandbox.filesystem.write('hello.txt', 'Hello World!')++# Get the sandbox ID, we'll need it later+sandboxID = sandbox.id++# Keep the sandbox alive for 1 hour+sandbox.keep_alive(60 * 60) # $HighlightLine++# Close the sandbox+sandbox.close()++# Do something else...+time.sleep(60)\ No newline at end of filediff --git a/apps/docs/src/code/python/reconnect/reconnect_3.py b/apps/docs/src/code/python/reconnect/reconnect_3.pynew file mode 100644index 000000000..0a98f1ca6--- /dev/null+++ b/apps/docs/src/code/python/reconnect/reconnect_3.py@@ -0,0 +1,9 @@+# Reconnect to the sandbox+sandbox2 = Sandbox.reconnect(sandboxID) # $HighlightLine++# Continue in using the sandbox+content = sandbox2.filesystem.read('hello.txt')+print(content)++# Close the sandbox+sandbox2.close()