# Managing sandboxes

The SDK can create, retrieve, fork, stop, restart, and delete a sandbox. These operations act on the sandbox as a whole.

## Create a sandbox

```ts
import { PHPSandbox } from '@phpsandbox/sdk';

const client = PHPSandbox.realtime(process.env.PHPSANDBOX_TOKEN!);

const sandbox = await client.notebook.create('laravel', {
  title: 'Inventory',
  visibility: 'private',
});
```

The current SDK method is named `client.notebook.create()`, but the resource it creates is a sandbox.

## Retrieve an existing sandbox

```ts
const sandbox = await client.notebook.get(sandboxId);
```

Call `ready()` when the next step requires the sandbox to be fully initialized, such as opening its preview:

```ts
const ready = await sandbox.ready();
console.log(ready.data.previewUrl);
```

Runtime operations such as filesystem and command calls wait for initialization when necessary.

## Fork a sandbox

Forking creates another sandbox from the current state:

```ts
const fork = await sandbox.fork();
```

Changes in the fork do not alter the source sandbox.

## Stop and restart

```ts
await sandbox.stop();
await sandbox.restart();
```

Stopping releases the sandbox's active compute without deleting its files and configuration. Restarting starts its compute again.

## Delete a sandbox

```ts
await sandbox.destroy();
```

`destroy()` permanently deletes the remote sandbox. Use it only when the sandbox and its retained work are no longer needed.
