E2B icon indicating copy to clipboard operation
E2B copied to clipboard

Experimental stateless JavaScript SDK

Open jakubno opened this issue 1 year ago • 1 comments

Experimental stateless JavaScript SDK for E2B

The stateless version of our SDK allows you to spin up a sandbox from a client without managing the sandbox lifecycle on the client. The sandbox stays stateful though.

Installation

npm i [email protected]

Usage

import * as e2b from 'e2b'

const apiKey = '...'
const lifetime = 60_000 * 5 // 5 minutes

console.log('> Creating sandbox...')
const sandboxID = await e2b.experimental_stateless.create({ apiKey }, {
  keepAliveFor: lifetime,
})
console.log('...sandbox created, sandbox id -', sandboxID)

const cmd = 'echo hello world'
console.log(`\n> Executing command "${cmd}"...`)
// This will stream command's stdout and stderr
await e2b.experimental_stateless.exec({
  apiKey,
  sandboxID
}, {
  cmd,
  onStdout: (data) => {
    console.log(data)
  },
  onStderr: (data) => {
    console.error(data)
  },
})
// Or you can wait and get the full output in the `result` object after the command has finished running:
// const result = await e2b.experimental_stateless.exec(...)
console.log('...command finished')

console.log('\n> Uploading file...')
await e2b.experimental_stateless.uploadFile({
  apiKey,
  sandboxID,
}, {
  path: '/tmp/hello.txt',
  content: new TextEncoder().encode('hello world'),
})
console.log('...file uploaded')

console.log('\n> Downloading file...')
const content = await e2b.experimental_stateless.downloadFile({
  apiKey,
  sandboxID,
}, {
  path: '/tmp/hello.txt'
})
console.log('...downloaded file:\n', content.toString())

console.log(`\n> Killing sandbox "${sandboxID}"...`)
await e2b.experimental_stateless.kill({
  apiKey,
  sandboxID,
})
console.log('...sandbox killed')

There are three methods on stateless module:

  • create - creates a sandbox for a specified time in ms, returns id of the sandbox, you can specify template or the sandbox will be created with the base template
  • exec - executes the command in specified sandbox
  • downloadFile - downloads a file from the sandbox as a byte array
  • uploadFile - uploads a file to a sandbox
  • kill - destroys the sandbox immediately

jakubno avatar Feb 16 '24 18:02 jakubno

⚠️ No Changeset found

Latest commit: 5719c9efbd2c9e226ecb66e9b6a140a311d4f49e

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

changeset-bot[bot] avatar Feb 16 '24 18:02 changeset-bot[bot]

We closed this PR because our new SDK that is in beta incorporates most stateless ideas.

ValentaTomas avatar Jul 16 '24 17:07 ValentaTomas