> ## Documentation Index
> Fetch the complete documentation index at: https://docs.corsair.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Modal plugin

> Modal's serverless cloud through its JavaScript SDK: sandboxes (create from any image, exec, files, tunnels, snapshots, logs), image builds, calls to deployed functions and classes, function calls, volumes, secrets, queues, dicts, apps and environments (95 operations).

Use **Modal** through Corsair: one client, typed API calls.

Connect a Modal account with a token pair from modal.com, Settings, API
Tokens: the token secret (`as-...`) is the API key and the token id
(`ak-...`) is stored as `token_id`. Calls run in the account's default
environment unless an operation gets `environment`, or the plugin is set up
with `modal({ environment: 'dev' })`.

* **Modal has no REST API.** Everything goes over gRPC through Modal's
  official JavaScript SDK, so the plugin runs on Node (HTTP/2), not in edge
  or browser runtimes. The SDK loads the first time an operation runs; each
  call makes its own client from the account's tokens and closes it after.
  The host's `MODAL_TOKEN_ID`, `MODAL_TOKEN_SECRET`, `MODAL_ENVIRONMENT` and
  `~/.modal.toml` are never used for an account.
* **Money.** Sandboxes, image builds and function calls are billed per
  second of CPU, memory and GPU. `sandboxes.create`, `sandboxes.exec`,
  `sandboxes.resume`, `sandboxes.restoreSnapshot`, `images.build`,
  `functions.call`, `functions.spawn`, `classes.*` and
  `functions.updateAutoscaler` are `destructive`, as is every delete, stop,
  clear and cancel. A sandbox ends at its `timeoutMs` (Modal's default is 5
  minutes) or with `sandboxes.terminate`.
* **Sandboxes** belong to an App; `sandboxes.create` uses
  `corsair-sandboxes` (created if missing) unless `appName` is given, and
  the registry image `python:3.13-slim` unless `image` is given. Ports in
  `encryptedPorts`, `h2Ports` or `unencryptedPorts` come back as tunnel URLs.
  `sandboxes.exec` waits for the command and returns its exit code, stdout
  and stderr (each capped at `maxOutputBytes`, 1 MB by default).
* **Functions and classes** have to be deployed with Modal's Python CLI
  first (`modal deploy`); the plugin looks them up by App and name.
  Arguments go CBOR-encoded, which Modal 1.2+ deployments accept.
  `functions.call` waits up to `waitMs` and returns status `pending` with a
  `functionCallId` if the call is still running; `functionCalls.get` picks
  the result up later.
* **Queues and dicts** store Python pickles, so Python code on Modal reads
  what the plugin writes. Dict keys are strings, numbers, booleans or null,
  encoded byte for byte as Python does, so the same key matches from both
  sides. Integers keep their full value, Python tuples and sets come back as
  arrays, bytes as `{ "$bytes": "<base64>" }` (send the same shape to store
  bytes), and objects JSON has no form for (datetimes, class instances) as
  `{ "$pickle": "<base64>" }`.
* **Lists** of volumes, secrets, queues and dicts page newest first: pass the
  returned `nextCreatedBefore` as `createdBefore`.
* Not wrapped: deploying Apps (Python CLI only), web endpoint URLs you call
  yourself, the SDK's experimental sidecar containers and outbound header
  policies, and reading or uploading Volume files (mount the Volume in a
  sandbox and use `files.*`).

**What you get:**

* 95 typed API operations

## Setup

**1. Install**

```bash npm theme={null}
npm install corsair @corsair-dev/modal better-sqlite3
npm install --save-dev @types/better-sqlite3
```

```bash yarn theme={null}
yarn add corsair @corsair-dev/modal better-sqlite3
yarn add --dev @types/better-sqlite3
```

```bash pnpm theme={null}
pnpm install corsair @corsair-dev/modal better-sqlite3
pnpm install --save-dev @types/better-sqlite3
```

```bash bun theme={null}
bun add corsair @corsair-dev/modal better-sqlite3
bun add --dev @types/better-sqlite3
```

**2. Add the plugin**

```ts corsair.ts theme={null}
import Database from 'better-sqlite3';
import { createCorsair } from 'corsair';
import { modal } from '@corsair-dev/modal';

export const corsair = createCorsair({
	plugins: [
		modal(),
	],
	database: new Database('corsair.db'),
	kek: process.env.CORSAIR_KEK!,
	multiTenancy: true,
	hub: {
		projectApiKey: process.env.CORSAIR_API_KEY!,
		signingSecret: process.env.CORSAIR_SIGNING_SECRET!,
	},
});
```

With `multiTenancy: true`, scope calls with `corsair.withTenant(id)`. See [Quick start](https://docs.corsair.dev/quick-start.md) for KEK + Hub keys, and [Multi-tenancy](https://docs.corsair.dev/concepts/multi-tenancy.md) for account isolation.

**3. Choose authentication**

> [tab] API Key (Recommended)
>
> Store the Modal API key before the first call: `pnpm corsair setup --plugin=modal --tenant=acme api_key=<your-key>` (leave out `--tenant` without `multiTenancy`), or pass it as `key` in the plugin options. With Hub, each tenant enters their own key on the connect page. See [API key auth](https://docs.corsair.dev/concepts/api-key).
>
> ```ts theme={null}
> modal()
> ```
>
> More: [API Key](https://docs.corsair.dev/concepts/api-key)

**4. Connect a tenant**

Mint a connect link and send the tenant to it. Hub hosts the page and delivers the result to your app — see [Connect / OAuth](https://docs.corsair.dev/management/connect).

```ts theme={null}
const { connectUrl } = await corsair.manage.connect.createLink({
	plugin: 'modal',
	tenantId: 'acme',
});
// redirect the user's browser to connectUrl
```

## Example API calls

**List running sandboxes**

```ts theme={null}
const tenant = corsair.withTenant('acme');
await tenant.modal.api.sandboxes.list({ appName: 'corsair-sandboxes' });
```

**Run a command in a sandbox**

```ts theme={null}
const tenant = corsair.withTenant('acme');
await tenant.modal.api.sandboxes.exec({
	sandboxId: 'sb-a1b2c3',
	command: ['python', '-c', 'print(2 + 2)'],
});
```

See the full list on the [API](https://docs.corsair.dev/plugins/modal/api.md) page.

## What's next

> [card] [API reference](https://docs.corsair.dev/plugins/modal/api.md)
>
> Every `modal.api.*` operation with input and output types.

> [card] [Connect / OAuth](https://docs.corsair.dev/management/connect)
>
> createLink, Hub delivery, and tenant connect flows.

> [card] [Use with agents](https://docs.corsair.dev/mcp-adapters/mcp-adapters)
>
> Expose this plugin's operations as MCP tools.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.