Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Experimental stateless JavaScript SDK #313

Draft
wants to merge 20 commits into
base: main
Choose a base branch
from
Draft

Experimental stateless JavaScript SDK #313

wants to merge 20 commits into from

Conversation

jakubno
Copy link
Member

@jakubno jakubno commented Feb 16, 2024

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

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 jakubno added the js-rc JS SDK - Release candidate label Feb 16, 2024
Copy link

changeset-bot bot commented Feb 16, 2024

⚠️ No Changeset found

Latest commit: 5719c9e

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

@jakubno jakubno added js-rc JS SDK - Release candidate and removed js-rc JS SDK - Release candidate labels Feb 16, 2024
@mlejva mlejva changed the title Stateless sdk Experimental stateless SDK Feb 16, 2024
@mlejva mlejva changed the title Experimental stateless SDK Experimental stateless JavaScript SDK Feb 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
js-rc JS SDK - Release candidate
Projects
Status: 🤔 Exploring
Development

Successfully merging this pull request may close these issues.

None yet

3 participants