Skip to content

Commit

Permalink
add SDK docs (#273)
Browse files Browse the repository at this point in the history
* Create sdk page

* basic structure

* updates

* lots of context

* update

* add download support

* Updates from Christian

* small fixes

* small url fix

* update links

* fix faq

---------

Co-authored-by: Danilo Woznica <danilowoz@gmail.com>
Co-authored-by: Ives van Hoorne <Ives.v.h@gmail.com>
Co-authored-by: Christian Alfoni <christianalfoni@gmail.com>
Co-authored-by: Necoline <necoline@codesandbox.io>
  • Loading branch information
5 people authored Dec 12, 2024
1 parent 04f1cf2 commit 5e9cf3a
Show file tree
Hide file tree
Showing 31 changed files with 1,726 additions and 30 deletions.
5 changes: 5 additions & 0 deletions packages/projects-docs/next-env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/// <reference types="next" />
/// <reference types="next/image-types/global" />

// NOTE: This file should not be edited
// see https://nextjs.org/docs/basic-features/typescript for more information.
4 changes: 3 additions & 1 deletion packages/projects-docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@
},
"devDependencies": {
"autoprefixer": "^10.4.16",
"markdown-table": "^3.0.4",
"postcss": "^8.4.31",
"tailwindcss": "^3.3.5"
"tailwindcss": "^3.3.5",
"typescript": "5.7.2"
},
"prettier": {
"embeddedLanguageFormatting": "off",
Expand Down
12 changes: 9 additions & 3 deletions packages/projects-docs/pages/_meta.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
{
"learn": {
"title": "Documentation",
"title": "Editor",
"type": "page"
},
"sdk": {
"title": "SDK",
"type": "page"
},
"tutorial": {
"title": "Tutorial",
"type": "page"
"type": "page",
"display": "hidden"
},
"faq": {
"title": "FAQ",
"type": "page"
"type": "page",
"display": "hidden"
}
}
14 changes: 13 additions & 1 deletion packages/projects-docs/pages/learn/_meta.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
"index": "Introduction",
"guides": "Getting Started",
"editors": "Editors",
"sdk": {
"title": "SDK",
"href": "/sdk/"
},
"-- cloud-development": {
"type": "separator",
"title": "Cloud Development"
Expand Down Expand Up @@ -29,5 +33,13 @@
"title": "More"
},
"ci": "CodeSandbox CI",
"explore": "Discover Page"
"explore": "Discover Page",
"tutorials": {
"title": "Tutorials",
"href": "/tutorial/getting-started-with-dev-containers"
},
"faq": {
"title": "FAQ",
"href": "/faq"
}
}
24 changes: 9 additions & 15 deletions packages/projects-docs/pages/learn/access/organizations.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -17,48 +17,42 @@ Additionally, organizations make it possible to connect all the employees at a c

## Creating an organization

You can create an organization from any active workspace that is not already part of an organzation.
In the _Workspace settings_, open the [Organization tab](https://codesandbox.io/t/org) and enter a name for the organization.

You can create an organization from any active workspace that is not already part of an organzation.
In the _Workspace settings_, open the [Organization tab](https://codesandbox.io/t/org) and enter a name for the organization.

![create org](../images/org-create.png)

To prevent confusion, we recommend that you name your organization so it directly relates to your business’ name.


![name org](../images/org-name.png)
![name organization](../images/org-name.png)

Once you have created an organization, it will automatically be linked to the workspace you created it from. However, you can choose to add additional existing workspaces to that organization.
You can also manage the organization admins.
You can also manage the organization admins.


![edit org](../images/org-edit.png)
![edit org](../images/org-edit.png)

The [Organization settings](https://codesandbox.io/t/org) will always be discoverable through the _Workspace settings_.

## Setting an email domain

In the _Organizations settings_, open the `Domain` tab and add your email domain.
The domain must be verified before being approved. This is an automated process that requires no action from you.

In the _Organizations settings_, open the `Domain` tab and add your email domain.
The domain must be verified before being approved. This is an automated process that requires no action from you.

![add domain](../images/org-domain-add.png)

Once the domain is approved, users who have CodeSandbox accounts connected to emails with a matching domain will be given the option to join the workspace.
Users may encounter the suggestion to join an eligible workspace when they are attempting to access projects in the workspace or when they log into CodeSandbox for the first time after the email domain has been approved.
Users may encounter the suggestion to join an eligible workspace when they are attempting to access projects in the workspace or when they log into CodeSandbox for the first time after the email domain has been approved.

Below is an example of the message a user with a matching email domain will see when opening a private Sandbox from a workspace they are not yet part of.


![join ws](../images/org-domain-join-ws.png)


## Accounts and domains

CodeSandbox accounts are tied to individual users. They grant access to shared workspaces and organizations, enabling collaboration with other CodeSandbox users.

With a single CodeSandbox account, users can create or join multiple workspaces and organizations to collaborate with various groups of CodeSandbox users; these workspaces and organizations are separate entities, each with a unique set of members and projects.

Since each workspace and organization are their own separate entities, members cannot see the other workspaces or organizations a user belongs to.


![graph](../images/org-account-graph.png)
3 changes: 1 addition & 2 deletions packages/projects-docs/pages/learn/devboxes/upload.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ You can check your current Sandbox storage usage from your [Dashboard](https://c

For Devboxes and repositories, the limits are:

- 20 GB total storage per Devbox or repository branch for workspaces on a Free plan.
- 50 GB total storage per Devbox or repository branch for workspaces on a Pro plan.
- 20 GB total storage per Devbox or repository branch for all workspaces.

You can check your Devbox storage usage by clicking the CodeSandbox icon at the top left of the editor and then "Virtual machine". This will open a new DevTool displaying the current VM usage, including storage.

Expand Down
2 changes: 1 addition & 1 deletion packages/projects-docs/pages/learn/environment/vm.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ VM resources consist of vCPU, RAM and storage. The vCPU and RAM specs are groupe
| Subscription | Storage |
| ------------ | ------- |
| Free | 20 GB |
| Pro | 50 GB |
| Pro | 20 GB |

If you require storage that goes beyond our Pro plan defaults, please select 'Pro Subscriptions' on our support form and [get in touch](https://codesandbox.io/support). Our team can adjust your limits to suit your project.

Expand Down
33 changes: 33 additions & 0 deletions packages/projects-docs/pages/sdk/_meta.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"index": "Introduction",
"use-cases": "Use Cases",
"pricing": "Pricing",
"contact": {
"title": "Contact Us",
"href": "https://codesandbox.io/support#form",
"newWindow": true
},

"-- api-reference": {
"type": "separator",
"title": "API Reference"
},
"sandboxes": "Sandboxes",
"filesystem": "File System",
"shells": "Shells",
"ports": "Ports & Previews",
"tasks": "Tasks & Setup",
"specs": "VM Specs",

"-- resources": {
"type": "separator",
"title": "Resources"
},
"snapshot-library": "Snapshot Library",
"snapshot-builder": "Snapshot Builder",
"environment": "Environment",
"docker": "Docker & Docker Compose",
"persistence": "Persistence",
"fork": "Fork",
"faq": "FAQ"
}
148 changes: 148 additions & 0 deletions packages/projects-docs/pages/sdk/docker.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
---
title: Docker & Dev Containers
description: Learn how to configure your sandbox environment using Dev Containers.
---

import { Callout } from 'nextra-theme-docs'

# Dev Containers

CodeSandbox natively supports the [Dev Containers specification](https://containers.dev/), allowing you to customize your sandbox, install system-level dependencies, and run additional services.

## Configuration

To configure your sandbox environment, create a `.devcontainer/devcontainer.json` file inside the root of the sandbox:

```json
{
"name": "Node.js",
"image": "mcr.microsoft.com/devcontainers/javascript-node:18",
"features": {
"ghcr.io/devcontainers/features/python:1": {}
}
}
```

In this example, we're installing Node v18 as base, with Python on top using Dev Container Features.

Alternatively, you can use a `Dockerfile` to build the Docker image when the sandbox boots:

```json
{
"name": "Node.js",
"build": {
"dockerfile": "./Dockerfile"
}
}
```

### Using Dev Containers in the SDK

When creating a sandbox, all shells will automatically run inside the Docker container specified in the Dev Container configuration.

```ts
const sandbox = await sdk.sandbox.create({
template: "node" // Template with Dev Container configuration
});

await sandbox.shells.run("node --version");
```

Since we use memory snapshots, the Docker container will already be running when you run your shell.

## Docker Compose

You can run additional services using Docker Compose by adding a `docker-compose.yml` configuration to your Dev Container:

```json
{
"name": "Full Stack App",
"dockerComposeFile": "docker-compose.yml",
"service": "app",
"workspaceFolder": "/workspace"
}
```

With a corresponding `docker-compose.yml`:

```yaml
services:
app:
image: mcr.microsoft.com/devcontainers/javascript-node:18
command: sleep infinity

db:
image: postgres:14
ports:
- 5432:5432
environment:
POSTGRES_PASSWORD: password
```
### Using Docker Compose in the SDK
The SDK will automatically start all services defined in your Docker Compose configuration:
```ts
const sandbox = await sdk.sandbox.create({
template: "fullstack" // Template with Dev Container configuration
});

// Wait for all services to be ready
await sandbox.setup.waitForFinish();

// You can now connect to the services
const portInfo = await sandbox.ports.waitForPort(5432);
console.log(`Database available at: ${portInfo.hostname}:${portInfo.port}`);
```
## Examples
### Full-Stack Development Environment
Here's an example of setting up a full-stack development environment with Node.js and PostgreSQL:
```ts
const sandbox = await sdk.sandbox.create({
template: "fullstack"
});

// Wait for environment setup
const progress = await sandbox.setup.waitForFinish();
if (progress.state !== "FINISHED") {
throw new Error("Environment setup failed");
}

// Start the development server
const devTask = await sandbox.tasks.runTask("dev");

// Wait for both the app and database to be ready
const [appPort, dbPort] = await Promise.all([
sandbox.ports.waitForPort(3000),
sandbox.ports.waitForPort(5432)
]);

console.log(`
App running at: ${appPort.getPreviewUrl()}
Database available at: ${dbPort.hostname}:${dbPort.port}
`);
```

### Custom Environment with System Dependencies

Example of a sandbox that needs specific system packages:

```ts
const sandbox = await sdk.sandbox.create({
template: "custom-env"
});
// The Dev Container will install required packages during setup
await sandbox.setup.waitForFinish();
// Run a command that uses the installed packages
const result = await sandbox.shells.run("ffmpeg -version");
console.log(result.output);
```

For more information about Dev Container configuration options and features, visit the [Dev Container specification](https://containers.dev/). Also, take a look at our [snapshot builder](/sdk/snapshot-builder.mdx) to learn how to create efficient snapshots with preloaded Docker images.
22 changes: 22 additions & 0 deletions packages/projects-docs/pages/sdk/environment.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
title: Environment
description: Learn how the CodeSandbox SDK's environment works.
---

import { Callout } from 'nextra-theme-docs'

# Environment

Sandboxes on CodeSandbox are backed by a [Firecracker](https://firecracker-microvm.github.io/) microVM, within the VM we run a rootless Docker container based on a [Dev Container](https://containers.dev/) configuration (specified in the `.devcontainer/devcontainer.json` file).

## Booting a Sandbox

Whenever we boot a sandbox from scratch, we'll:

1. Start the Firecracker VM
2. Create a default user (called `pitcher-host`)
3. (optional) Build the Docker image specified in the `.devcontainer/devcontainer.json` file
4. Start the Docker container
5. Mount the `/project/sandbox` directory as a volume inside the Docker container

We run an agent inside the VM that the SDK connects to. Via an RPC protocol you can then interact with the sandbox.
42 changes: 42 additions & 0 deletions packages/projects-docs/pages/sdk/faq.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
---
title: FAQs
description: Find answers to common questions about the CodeSandbox SDK.
---

import { Callout } from 'nextra-theme-docs'

# FAQ

## Do I need a CodeSandbox workspace to be able to use the SDK?

Yes. You need a CodeSandbox API key to use CodeSandbox SDK, which you can get by [creating a CodeSandbox account](https://codesandbox.io/signin).

## How can I revoke my API key?

To revoke an API key, go to the "Permissions" tab of your [Workspace Settings](https://codesandbox.io/t/permissions) and click the respective token under "API Access". Then, click "Remove Token" from the bottom of that modal.

## I have hit a rate limit - what should I do?

Please subscribe to a CodeSandbox plan that includes the most suitable rate limit for you. In case you're on our Builder plan already, please [contact us](https://webforms.pipedrive.com/f/72gS7iXoP8qjL8Ku9HZQcN5UKpUpZkgKRCjhbqREjCOYyBgzrCKCr7Mys5AyczOHBN) to discuss an Enterprise plan.

## Can I change the specs of the VMs?

Currently, we only allow changing the default specs for all VMs created with the SDK. You can change

We are also SOC 2 Type II compliant.

## Is it possible to self-host CodeSandbox SDK?

No, for now we don't provide a self-host option.

## Are there any SDK rate limits?

Yes. The SDK has rate limits on concurrent VMs, number of requests per hour, and number of sandboxes created per hour. These limits vary depending on the CodeSandbox plan, as explained on our [Pricing page](https://codesandbox.io/pricing).

## Can I use the same CodeSandbox plan for SDK and non-SDK usage?

Yes. Your CodeSandbox plan will allow you to use both, so you can leverage the SDK for programmatic sandbox creation, while still allowing your team to use CodeSandbox for their development.

## Does the CodeSandbox SDK use CodeSandbox Sandboxes or Devboxes?

While the SDK code only mentions "sandbox", the actual environments that it uses are officially called "Devboxes" (which use VMs). So, if you need more details about these VMs, please always refer to "[Devbox](/learn/devboxes/overview)" section of the CodeSandbox documentation and pricing.
Loading

0 comments on commit 5e9cf3a

Please sign in to comment.