Deployment
Deploy the Factory Server to Mastra platform or self-host it in your own environment. Both options use the Factory Server project created in Get started.
Before you begin
- Complete the setup and investigation steps in Get started.
- For deployment to Mastra platform, have access to the intended project and environment.
- Preserve the database's credential encryption key.
- Choose the execution provider and verify its required configuration.
Choose a setup
| Setup | Factory Server | Supporting services |
|---|---|---|
| Full platform (default) | Mastra platform | Mastra platform auth, PostgreSQL, and sandboxes |
| Local server with platform resources | Your computer | Mastra platform auth, PostgreSQL, and sandboxes |
| Fully self-hosted | Your infrastructure | Your auth provider, database, and sandbox provider |
Continue with Deploy for the full-platform setup or Self-host when the Factory Server runs in your environment.
Deploy
Use the generated project's deploy script to deploy to Mastra platform. The CLI builds and uploads the application to a platform environment and reports its public URLs.
Check the server
From the Factory Server directory, run:
- npm
- pnpm
- Yarn
- Bun
npm run check npm run buildpnpm run check pnpm run buildyarn run check yarn buildbun run check bun run buildReview local environment values so a localhost callback origin isn't used by the deployed server.
Prepare the environment
In Platform, choose the target project and review its environment configuration. Confirm the attached database, encryption key, provider access, integration credentials, and public origin.
The public origin must point to the deployed Factory endpoint. Configure allowed origins only when serving the UI from a separate origin. A deployed server requires persistent database storage.
See Environment variables for variable names and the distinction between Platform integration and sandbox credentials.
Run the deployment
Inside your Factory project directory, run the deployment command:
- npm
- pnpm
- Yarn
- Bun
npm run deploypnpm run deployyarn deploybun run deployReview the CLI's organization, project, and environment selection. The default environment is
production. You can pass flags to thedeploycommand to customize its behavior.Deployment flags
To target another named environment, pass it explicitly:
- npm
- pnpm
- Yarn
- Bun
npm run deploy -- --env stagingpnpm run deploy --env stagingyarn deploy --env stagingbun run deploy --env stagingThe CLI can create an environment if it doesn't exist. Review configuration and secret changes when prompted. Use the deployed Factory URL and credentials for the target environment.
To configure a EU region for a new environment, pass
--region eu:- npm
- pnpm
- Yarn
- Bun
npm run deploy -- --env staging --region eupnpm run deploy --env staging --region euyarn deploy --env staging --region eubun run deploy --env staging --region euIt doesn't relocate an existing environment. Confirm the chosen environment and attached database region in Platform before deploying.
For a Factory project, the CLI derives a Factory URL and a Studio URL from the environment slug. Other project types use a server URL. Use the actual URLs returned for your project instead of constructing addresses from a project name.
Verify the deployed Factory
Open the Factory URL reported by the CLI. Confirm sign-in returns to this server, repository access works, and existing provider credentials remain usable. Create a new issue in a subscribed source, investigate it, and verify the session's actual sandbox provider.
Inspect the Platform deployment logs if a step fails.
Self-host
Run Factory on a virtual machine, in a container, or on a hosting service that supports a persistent Node.js process. The build includes Factory's browser UI and API, so you can serve both from the same domain.
A self-hosted Factory Server can use Mastra platform services or your own providers. Configure Auth, Storage, and Sandboxes independently, and connect your preferred model providers.
For a fully self-hosted setup, create your project with the --no-platform flag:
- npm
- pnpm
- Yarn
- Bun
npm create factory@latest my-factory -- --no-platformpnpm create factory my-factory --no-platformyarn create factory my-factory --no-platformbunx create-factory my-factory --no-platformThe installer copies .env.example to .env without adding platform credentials, so configure your authentication, database, and sandbox providers before deployment.
Requirements
- Runtime: Node.js matching the
engines.noderequirement in your Factory project'spackage.json. Keep the server running so Factory can process intake and agent work in the background. - Storage: A persistent database. The generated server uses PostgreSQL with pgvector through
DATABASE_URL. For another provider, follow Storage configuration. - Public endpoint: An HTTPS URL that supports streaming responses for live sessions. Set
MASTRACODE_PUBLIC_URLto this origin and use it for authentication and integration callbacks. - Secrets: Provider and integration credentials in your deployment's environment settings. Keep the credential encryption key and state-signing secrets stable across deployments.
- Sandbox environment: Network access to your chosen cloud sandbox provider. For local execution, install Git and your repository's build tools on the server, and use persistent disk space for session directories.
Deployment outline
-
Connect your Factory Server repository to your hosting service, or copy it to the machine where it will run. Configure your chosen providers in the project.
-
Add the project's environment variables to the deployment environment. Set
NODE_ENV=productionand configurePORTfor your hosting service. Update callback URLs for any auth or integration apps you manage. -
Install dependencies and build from the Factory Server directory:
- npm
- pnpm
- Yarn
- Bun
npm ci --include=dev npm run check npm run buildnpm ci --include=dev # couldn't auto-convert command pnpm run check pnpm run buildnpm ci --include=dev # couldn't auto-convert command yarn run check yarn buildnpm ci --include dev # couldn't auto-convert command bun run check bun run build -
Use
npm run startas the service's start command. This uses the generated project's Mastra CLI andvarlockdependencies, so retain them in the runtime environment. Configure automatic restarts and route HTTPS traffic to the server's listening port. -
Open the deployed Factory URL and sign in. Import an issue from your connected repository and run an investigation to confirm the agent can use its sandbox.
The build writes the server and Factory UI to .mastra/output. You can also package this output for a container deployment. See the Mastra server deployment guide for running the build directly with Node.js.