Skip to main content

Environment variables

This reference describes the generated Factory Server. Set local values in its ignored .env and deployed values in the environment's secret settings. Restart the server after changes. For provider setup, see Auth, Storage, and Sandboxes. See the API reference.

Server and storage

VariableDefault or conditionPurpose
PORTThe development server normally starts at 4111.Listening port. Keep the local public URL aligned with it.
MASTRACODE_PUBLIC_URLDefaults to http://localhost:4111 when unset.Browser-facing origin used for authentication and integration callbacks. Set explicitly for a different port or deployed server.
MASTRACODE_ALLOWED_ORIGINSEmptyComma-separated UI origins allowed for credentialed cross-origin requests when the UI uses another origin.
DATABASE_URLRequired outside development and tests by the generated entry.Postgres connection string. The installer provisions this database connection.
APP_DATABASE_URLDeprecated fallbackUse DATABASE_URL for new configuration.
MASTRACODE_DISPATCH_MAX_IN_FLIGHTDispatcher default when unset or invalidPositive integer limiting concurrent background dispatches per replica, not across all replicas.

Without a database URL, the generated entry uses local LibSQL only in development or tests. Configure persistent storage for a deployed server.

Stored credential encryption

VariableFormatPurpose
FACTORY_CREDENTIAL_ENCRYPTION_KEYBase64-encoded 32-byte keyPrimary encryption key for persisted provider and integration credentials. Configure before saving credentials.
FACTORY_CREDENTIAL_ENCRYPTION_KEY_IDString, default v1Identifier stored with newly encrypted values.
FACTORY_CREDENTIAL_ENCRYPTION_PREVIOUS_KEYSJSON object of key IDs to Base64 keysPrevious keys retained during rotation.

Configure encryption before users save credentials, following Get started. Preserve the same key with the database across restarts and replicas. Restart after configuration and confirm the missing-encryption warning is gone.

The generated entry defaults the key identifier to v1. This identifier is metadata, not another encryption key. Missing encryption triggers plaintext compatibility warnings in the current runtime. For a custom server, pass secretEncryption to MastraFactory.

Rotate encryption keys

Before rotation, take a database backup and preserve the active key. Coordinate the change across every replica sharing that database.

  1. Generate a new 32-byte key and give it a new identifier, such as v2.
  2. Set the new primary key and identifier on the server.
  3. Set FACTORY_CREDENTIAL_ENCRYPTION_PREVIOUS_KEYS to a JSON object mapping the old identifier to the old Base64 key.
  4. Restart every replica with the same primary and previous-key configuration.
  5. Verify existing provider and integration connections, then verify new credentials can be saved and used.

The previous keys allow existing ciphertext to be read while records are re-encrypted with the primary key. Keep them until migration is verified for all credential storage and retained backups have an appropriate recovery plan.

For unreadable credentials, see Troubleshooting.

Platform authentication and integrations

VariablePurpose
MASTRA_SHARED_API_URLPlatform API root. When set, the generated server prioritizes Platform authentication over self-managed providers.
MASTRA_ORGANIZATION_IDOptional organization restriction on the Platform authentication path.
MASTRA_COOKIE_DOMAINOptional cookie-domain override for Platform authentication. Leave unset unless your deployment layout requires it.
MASTRA_PLATFORM_ACCESS_TOKENPlatform integration and sandbox credential, checked before the scaffolding secret.
MASTRA_PLATFORM_SECRET_KEYPlatform integration and sandbox credential written by scaffolding. Used when the access token is absent.

Factory uses Mastra platform authentication by default. To use another provider, pass it to the auth constructor option. See Auth for provider configuration.

Sandbox selection

VariableCondition
FACTORY_SANDBOX_PROVIDERSet to local to override Platform and E2B selection. Otherwise, Factory selects a provider from the credentials below.
MASTRA_PLATFORM_ACCESS_TOKEN or MASTRA_PLATFORM_SECRET_KEYEither credential is required with both IDs below to select Platform.
MASTRA_PROJECT_IDMastra platform project ID.
MASTRA_ENVIRONMENT_IDEnvironment ID within that project.
MASTRA_WORKSPACE_PROXY_URLOptional Platform workspace proxy override. Normally use the provider default.
E2B_API_KEYSelects direct E2B when the complete Platform configuration is absent.
MASTRACODE_LOCAL_SANDBOX_ROOTLocal fallback root. Defaults to ~/.mastracode/web/sandboxes.

Read Sandboxes for provider precedence and how to verify a started session. Partial remote configuration can leave the local fallback active.

Slack

VariablePurpose
SLACK_APP_SIGNING_SECRETEnables Slack in the generated entry and verifies request signatures. Also provides the fallback state-signing secret.
SLACK_APP_BOT_TOKENPosts replies through the installed app.
SLACK_APP_CLIENT_IDClient ID for linking a user's Slack identity.
SLACK_APP_CLIENT_SECRETClient secret for account linking.
MASTRACODE_CHANNELS_PUBLIC_URLPublic HTTPS origin for Slack callbacks and links. Falls back to the public URL when unset. Use a tunnel origin for local Slack testing.

For setup instructions, see Slack.

Custom integration credentials

Complete explicit integration configurations take precedence over the corresponding automatic Platform integration.

IntegrationVariablesCallback configuration
GitHub AppGITHUB_APP_ID, GITHUB_APP_PRIVATE_KEY, GITHUB_APP_CLIENT_ID, GITHUB_APP_CLIENT_SECRET, GITHUB_APP_SLUGPublic origin plus /auth/github/callback for the App's callback and setup URL.
Linear OAuth appLINEAR_CLIENT_ID, LINEAR_CLIENT_SECRETPublic origin plus /auth/linear/callback.

The generated entry derives its integration state-signing secret from GITHUB_APP_WEBHOOK_SECRET, then WORKOS_COOKIE_PASSWORD, then SLACK_APP_SIGNING_SECRET. Integrations that require stable state signing reject a temporary per-process signer. Configure a stable secret and preserve it across replicas and restarts. A custom server can pass stateSecret directly.

To use your own authentication provider, see Auth.

Event delivery

VariableDefaultPurpose
MASTRA_PLATFORM_GITHUB_POLLING_ENABLEDEnabledPlatform GitHub event polling.
MASTRA_PLATFORM_GITHUB_POLLING_INTERVAL_MS20000Positive polling interval in milliseconds.
MASTRA_PLATFORM_GITHUB_RECONCILE_ENABLEDEnabledState reconciliation for missed GitHub events.
MASTRACODE_GITHUB_RECONCILE_ENABLEDEnabledReconciliation for a self-managed GitHub integration.
MASTRACODE_GITHUB_RECONCILE_INTERVAL_MS3600000Self-managed GitHub reconciliation interval in milliseconds, unless overridden by the issue- or PR-specific interval.

Personal issue subscriptions and Linear routing remain necessary even when workers are enabled. Use the generated environment schema for additional server-specific overrides.