Documentation
Sandboxes
CLI & API

Create Sandboxes with the CLI & API

This guide will show you how to create sandboxes using the CLI & API.

CodeSandbox CLI

You can import a local project into CodeSandbox by using our CLI (opens in a new tab).

To install our CLI, run npm install -g codesandbox. Then import a project by running codesandbox {directory}.

Example usage

$ npm install -g codesandbox
$ codesandbox ./

Define API

We offer an API that allows you to programmatically create a sandbox. This is useful for documentation, enabling you to generate a sandbox on the fly from code examples. You can call the endpoint https://codesandbox.io/api/v1/sandboxes/define both with a GET and with a POST request.

Supported Parameters

We currently support three extra parameters. The query accepts the same options as the embed options.

Query ParameterDescriptionExample Input
parametersParameters used to define how the sandbox should be created.Example below
queryThe query that will be used in the redirect URL.view=preview&runonclick=1
embedDefines whether we should redirect to the embed instead of the editor.1
jsonInstead of redirecting we will send a JSON response with {"sandbox_id": sandboxId}.1

How it works

The API only needs one argument: files. This argument contains the files that will be contained in the sandbox.

An example body would be:

{
  "files": {
    "index.js": {
      "content": "console.log('hello!')",
      "isBinary": false
    },
    "package.json": {
      "content": {
        "dependencies": {}
      }
    }
  }
}

Binary Files

You can import binary files by setting isBinary to true and content as a URL to the file hosted externally. For example:

{
  "isBinary": true,
  "content": "https://..."
}

Folders

You can create folders by naming the file with a / in its name, allowing you to structure your application as you prefer:

{
  "files": {
    "src/index.js": {
      "content": "console.log('hello!')",
      "isBinary": false
    },
    "package.json": {
      "content": {
        "dependencies": {}
      }
    }
  }
}

This will create a file called index.js in your src folder.

GET Request

It's quite difficult to send the JSON parameters with a GET request. There is a chance of unescaped characters and the URL hits its limit of ~2000 characters quickly. That's why we first compress the files to a compressed lz-string. We offer a utility function in the codesandbox dependency for this. The implementation looks like this:

import { getParameters } from 'codesandbox/lib/api/define';
 
const parameters = getParameters({
  files: {
    'index.js': {
      content: "console.log('hello')",
    },
    'package.json': {
      content: { dependencies: {} },
    },
  },
});
 
const url = `https://codesandbox.io/api/v1/sandboxes/define?parameters=${parameters}`;

POST Form

You can do the exact same steps for a POST request, but instead of a URL, you'd show a form. With a POST request, you can create bigger sandboxes.


Define without render

If you want to define a new sandbox without getting it rendered, you can add ?json=1 to the request. For instance, https://codesandbox.io/api/v1/sandboxes/define?json=1. Instead of the render, the result will be JSON data providing you with the sandbox_id of the new sandbox.

This is useful, for instance, if you need to create a new sandbox programmatically, so you can then embed it on your site (see Embed documentation).

Both GET and POST requests are supported.

XHR Request

You can also create a sandbox using an XHR request, like using fetch. An example sandbox is:


Import Single Components

You can import a local component into CodeSandbox by using our other CLI (opens in a new tab).

You can install our CLI by running npm install -g codesandboxer-fs. Then you can export a project by running codesandboxer {filePath}.

$ npm install -g codesandboxer-fs
$ codesandboxer docs/examples/my-single-component.js

This will print out the ID of a sandbox that does nothing but render the targeted component, along with a link to that sandbox. This will also bundle in other local files used by the component to ensure render.

Import Using Codesandboxer

Codesandboxer (opens in a new tab) imports a single file from a git repository, along with supplemental files and dependencies.

Using this creates an easy way to upload an example instead of an entire git repository. This enables you to easily share examples with others, or to link to editable versions of examples from a documentation website. React-codesandboxer is the main version, but there are also versions for VS Code, Atom, and BitBucket.

How it works

Below the surface, react-codesandboxer fetches the files it needs from GitHub or BitBucket, using a single file that will be rendered as the 'example' as an entry point, then uses the Define API to upload the necessary files into a new create-react-app sandbox.

Check out the codesandboxer docs (opens in a new tab) for information on how to implement it.

import React, { Component } from 'react';
import CodeSandboxer from 'react-codesandboxer';
 
export default () => (
  <CodeSandboxer
    examplePath="examples/file.js"
    gitInfo={{
      account: 'noviny',
      repository: 'react-codesandboxer',
      host: 'github',
    }}
  >
    {() => <button type="submit">Upload to CodeSandbox</button>}
  </CodeSandboxer>
);

Import Using Remark-codesandbox

Remark-codesandbox (opens in a new tab) is a remark plugin for creating sandboxes directly from code blocks in documentation. Developed by CodeSandbox community member Kai Hao, it supports popular platforms including MDX, Gatsby, Storybook Docs, docz etc. Learn more about it in their documentation (opens in a new tab).

Sandbox information inference

When importing, we infer sandbox settings based on several files in a repository.

Sandbox SettingInferred from
Titlename field in package.json
Descriptiondescription field in package.json
Tagskeywords field in package.json
TemplateBased on this (opens in a new tab) logic

If you want to override any of these settings, you can create a template.json in the .codesandbox folder. You can find more info about this setting here (opens in a new tab).