MELLEPRISE MELLEPRISE AI MP Blog
← All articles

replit secrets work in preview, but the published app cannot see them

Pastel comic of a key traveling from a project editor safe toward a published app safe through a pipe with one closed valve

Your replit secrets work in Preview. Then the published app throws an undefined environment variable, cannot call the API, cannot log users in, or writes to the wrong database.

The confusing part is that the key exists. You can see its name in the Secrets tool. The same app runs when you press Run. Production still behaves as if the value never arrived.

What is actually failing

Preview and publishing are not the same runtime. The Project Editor reads the workspace configuration. A published app reads the values attached to that deployment.

Replit made this less brittle in April 2025: deployment secrets can sync with workspace secrets when you publish or republish. That does not mean every production value is always identical. A value can be unsynced or overridden in the deployment settings, and older documentation still describes the two lists as separate.

So the correct question is not “did I create the secret?” It is “which exact value did this running production deployment receive?”

Why AI-built apps miss it

Replit Agent usually proves the happy path inside the Project Editor. It adds a key, reads it with process.env.NAME or os.environ["NAME"], calls the API once, and reports success.

That test does not prove the published runtime. It also does not prove the correct database, because both environments can use a variable called DATABASE_URL with different values. The editor points at development data; the published app should receive the production connection.

The Agent may also put a public configuration value and a private credential in the same mental bucket. Replit separates them: secrets are for tokens, passwords, and connection strings; configurations are for non-sensitive settings such as feature flags, log levels, or public service URLs.

replit secrets production click path

  1. Capture the exact production error. Open the Publishing logs and copy the first missing variable, failed API call, auth error, database error, or startup crash. Do not print secret values.
  2. Confirm the deployment type. Secrets are not available for Static Deployments. A static build cannot hide server credentials; anything bundled into browser JavaScript is public.
  3. Name the runtime that failed. Preview, Autoscale, Reserved VM, Scheduled, or a custom domain all need separate evidence. REPLIT_DEPLOYMENT=1 means the code is running in a published project.
  4. Open the Project Editor Secrets tool. Use Tools → Secrets. Check exact key names, spelling, case, underscores, and whether the value is a secret or only a configuration.
  5. Open Publishing → Adjust settings → Production app secrets. This is the production checklist. Compare names, not values. Look for missing rows, unsynced rows, or manual overrides.
  6. Understand sync before editing. Since the April 2025 change, workspace secrets can sync into deployment secrets at publish or republish. An explicitly unsynced production value can intentionally differ. Do not “fix” a production override until you know why it exists.
  7. Check account and project scope. Account secrets are reusable across projects. When the same key exists in both places, the project-level value wins. A correct account value can still be shadowed by a stale project value.
  8. Check database variables separately. A development DATABASE_URL and a production DATABASE_URL can share the same name while pointing at different databases. Compare only the host or endpoint ID yourself; never paste the full connection string into a chat.
  9. Rotate and republish deliberately. After changing a production secret, republish. Replit documents synchronization at publish or republish, not proof that a running process hot-reloaded the new value.
  10. Verify one real path. Trigger exactly one login, API call, webhook, or database write in production. Confirm the log line and the external system result before changing anything else.

The failures that look identical to the user

The key name is almost right. OPENAI_API_KEY, OPEN_AI_KEY, and OPENAI_KEY are three different variables. Your code reads only one of them.

The secret exists only as configuration. Both secrets and configurations surface as environment variables, but sensitive credentials belong in Secrets. Configurations are for values you may safely expose to the environment.

A production override is stale. Sync can be correct for most keys while one unsynced production row keeps an old token. Rotating the workspace secret does not repair that row.

The app is static. A static deployment cannot protect a server-side secret. Moving the key into frontend code is not a fix; it publishes the credential.

The value was pasted with invisible damage. A trailing newline, leading space, surrounding quotes, or copied Unicode dash can break authentication while the variable technically exists.

The code reads the wrong database variable. Replit’s managed database flow uses DATABASE_URL, but your app may also contain SUPABASE_DATABASE_URL, NEON_DATABASE_URL, or a hard-coded fallback. The presence of one correct variable does not prove the code uses it.

Safe debugging without leaking the secret

Never debug by printing the token. Do not log process.env, run a public endpoint that dumps the environment, or paste a database connection string into Agent, a screenshot, a support ticket, or a git issue.

Print only names and safe metadata: which variable the code attempted to read, whether it was present, its length class, and the first error returned by the external service. If you must compare database hosts, compare the host or endpoint identifier yourself and keep passwords hidden.

Remember that visibility rules are not a security boundary. Replit warns that some organization members who cannot view secret values can still access them by printing environment variables. Treat every person with code execution access as able to read the runtime environment.

Secrets that should never be in the browser

Service-role keys, database passwords, SMTP passwords, payment webhook secrets, and private API tokens belong on the server only. If the browser needs to call a service, use a backend route that holds the credential and exposes only the narrow operation the user is allowed to perform.

This is the same production boundary as vercel env vars: a variable existing in one environment is not evidence that the deployed bundle received the right value, or that the value belongs in that bundle at all.

Production checklist

  • The exact failing runtime is named: Preview, published app, Scheduled job, or custom domain.
  • The deployment type supports Secrets; Static is not being used for server-side credentials.
  • Every key required in production exists under Publishing → Adjust settings → Production app secrets.
  • Names match the code exactly, including case and underscores.
  • Workspace sync state and unsynced overrides have been reviewed before editing.
  • Project-level secrets do not accidentally shadow correct account secrets.
  • Sensitive credentials are Secrets, not public configurations.
  • Values have no accidental spaces, quotes, or pasted newlines.
  • DATABASE_URL in the published app points at the intended production database.
  • No secret is printed in logs, screenshots, frontend JavaScript, git, or chat.
  • After a production secret change, the app was republished and one real flow was verified.
  • The wider launch path still passes the lovable deploy production checklist.

Once you separate editor secrets, deployment secrets, sync state, overrides, deployment type, and database targets, the failure stops looking random. Fix the layer that actually owns the wrong value. If the broken secret controls data access, continue with the replit database production checks; if publishing itself never became stable, use the replit deploy path first.

FAQ

Why do replit secrets work in Preview but not in the published app?

Preview and publishing are separate runtimes. Since April 2025 Replit can sync workspace secrets into deployment secrets, but individual production values can be unsynced or overridden. Static Deployments do not support Secrets, and a changed value is only guaranteed at publish or republish. Check Publishing → Adjust settings → Production app secrets instead of trusting the editor pane.

Do I need to republish after changing Replit secrets?

Treat republishing as required when a production secret changes. Replit documents synchronization when you publish or republish, not as proof that a running process hot-reloaded the value. Republish, then verify one production request in logs without printing the secret.

Is DATABASE_URL the same in Replit Preview and production?

No. Both environments can use a variable named DATABASE_URL, but the editor points to the development database while the published app should receive the production value. Compare only hosts or endpoint IDs yourself; never paste the full connection string into chat or screenshots.