Apps are containers for Modal resources like Functions, Classes, and Sandboxes. Use the Apps API to reference deployed apps.
Access the Apps API
import { ModalClient } from "modal";
const modal = new ModalClient();
const app = await modal.apps.fromName("my-app");
Methods
modal.apps.fromName(name, params?)
Reference a deployed App by name, or create it if it does not exist.
Name of the App to reference.
Optional parameters
Environment name to use. Defaults to the workspace default environment.
If true, creates the App if it doesn’t exist. Defaults to false.
The referenced App object.
// Reference an existing app
const app = await modal.apps.fromName("my-app");
// Create if missing
const app = await modal.apps.fromName("my-app", {
createIfMissing: true,
});
// Use a specific environment
const app = await modal.apps.fromName("my-app", {
environment: "staging",
});
Throws NotFoundError if the app doesn’t exist and createIfMissing is false.
App object
The App object represents a deployed Modal application.
Properties
Unique identifier for the App.
Name of the App, if it was looked up by name.
Example: Create a sandbox in an app
import { ModalClient } from "modal";
const modal = new ModalClient();
const app = await modal.apps.fromName("my-app", {
createIfMissing: true,
});
const image = modal.images.fromRegistry("alpine:3.21");
const sandbox = await modal.sandboxes.create(app, image, {
command: ["echo", "Hello from sandbox!"],
});
const output = await sandbox.stdout.readText();
console.log(output);
await sandbox.terminate();