You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
For multi-tenant apps, keep a map of sandbox IDs by user. On each request, connect to the user's existing sandbox (which auto-resumes if paused) or create a new one.
159
+
160
+
<CodeGroup>
161
+
```js JavaScript & TypeScript
162
+
import { Sandbox } from'e2b'
163
+
164
+
constuserSandboxes=newMap() // userId → Sandbox
165
+
166
+
asyncfunctiongetSandbox(userId) {
167
+
let sandbox =userSandboxes.get(userId)
168
+
169
+
if (!sandbox) {
170
+
sandbox =awaitSandbox.create({
171
+
timeoutMs:5*60*1000,
172
+
lifecycle: {
173
+
onTimeout:'pause',
174
+
resumeOn:'any',
175
+
},
176
+
})
177
+
userSandboxes.set(userId, sandbox)
178
+
}
179
+
180
+
return sandbox
181
+
}
182
+
183
+
// On each user request — auto-resumes if paused
184
+
constsandbox=awaitgetSandbox('user-123')
185
+
constresult=awaitsandbox.commands.run('echo "Hello from your sandbox"')
0 commit comments