npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@codesandbox/sdk

v0.0.0-alpha.10

Published

The CodeSandbox SDK

Downloads

652

Readme

CodeSandbox SDK

The power of CodeSandbox in a library

CodeSandbox SDK enables you to programmatically spin up development environments and run untrusted code. It provides a programmatic API to create and run sandboxes quickly and securely.

Under the hood, the SDK uses the microVM infrastructure of CodeSandbox to spin up sandboxes. It supports:

  • Starting fresh VMs within 4 seconds
  • Snapshotting/restoring VMs (checkpointing) at any point in time
    • With snapshot restore times of less than 2 seconds
  • Cloning VMs within 3 seconds
  • Source control (git, GitHub, CodeSandbox SCM)
  • Running any Dockerfile

Getting Started

To get started, install the SDK:

npm install @codesandbox/sdk

Then, create an API token by going to https://codesandbox.io/t/permissions, and clicking on the "Create API Token" button. You can then use this token to authenticate with the SDK:

import { CodeSandbox } from "@codesandbox/sdk";

// Create the client with your token
const sdk = new CodeSandbox(token);

// This creates a new sandbox by forking our default template sandbox.
// You can also pass in other template ids, or create your own template to fork from.
const sandbox = await sdk.sandbox.create();

// You can run JS code directly
await sandbox.shells.js.run("console.log(1+1)");
// Or Python code (if it's installed in the template)
await sandbox.shells.python.run("print(1+1)");

// Or anything else
await sandbox.shells.run("echo 'Hello, world!'");

// We have a FS API to interact with the filesystem
await sandbox.fs.writeTextFile("./hello.txt", "world");

// And you can clone sandboxes! This does not only clone the filesystem, processes that are running in the original sandbox will also be cloned!
const sandbox2 = await sandbox.fork();

// Check that the file is still there
await sandbox2.fs.readTextFile("./hello.txt");

// You can also get the opened ports, with the URL to access those
console.log(sandbox2.ports.getOpenedPorts());

// Getting metrics...
const metrics = await sandbox2.getMetrics();
console.log(
  `Memory: ${metrics.memory.usedKiB} KiB / ${metrics.memory.totalKiB} KiB`,
);
console.log(`CPU: ${(metrics.cpu.used / metrics.cpu.cores) * 100}%`);

// Finally, you can hibernate a sandbox. This will snapshot the sandbox and stop it. Next time you start the sandbox, it will continue where it left off, as we created a memory snapshot.
await sandbox.hibernate();
await sandbox2.hibernate();

// Open the sandbox again
const resumedSandbox = await sdk.sandbox.open(sandbox.id);

CodeSandbox Integration

This SDK uses the API token from your workspace in CodeSandbox to authenticate and create sandboxes. Because of this, the sandboxes will be created inside your workspace, and the resources will be billed to your workspace.

You could, for example, create a private template in your workspace that has all the dependencies you need (even running servers), and then use that template to fork sandboxes from. This way, you can control the environment that the sandboxes run in.

Example Use Cases

These are some example use cases that you could use this library for:

Code interpretation: Run code in a sandbox to interpret it. This way, you can run untrusted code without worrying about it affecting your system.

Development environments: Create a sandbox for each developer, and run their code in the sandbox. This way, you can run multiple development environments in parallel without them interfering with each other.

AI Agents: Create a sandbox for each AI agent, and run the agent in the sandbox. This way, you can run multiple agents in parallel without them interfering with each other. Using the forking mechanism, you can also A/B test different agents.

CI/CD: Run tests inside a sandbox, and hibernate the sandbox when the tests are done. This way, you can quickly start the sandbox again when you need to run the tests again or evaluate the results.