Railway environment variables: frontend vs backend secrets

By Tracklayer · Updated 2026-11-21

Short answer: add configuration to the Railway service that needs it, use service references for database connections and keep privileged credentials in backend code. Frontend variables such as VITE_ and NEXT_PUBLIC_ values are public configuration. Storing them in a hosting dashboard does not make the browser bundle private.

Decide who is allowed to read each value

Value Where it usually belongs
Public API base URL Frontend configuration
Public client key intended by a provider for browsers Frontend, with the provider’s access controls configured
Model provider secret key Backend only
Database connection string with password Backend only
Webhook signing secret Receiving backend only
Administrator or service-role credential Privileged backend only

The distinction is capability, not whether a value contains the word “key.” Read what the provider permits that credential to do. A publishable key and an administrator key cannot be interchanged because both appear in the same dashboard.

Set variables on the correct Railway service

  1. Identify the reader. Find the code that accesses the variable and determine whether it runs on the server or in a browser.
  2. Select the project environment. Keep production and test destinations intentional.
  3. Open the service’s Variables tab. Add only what that process needs.
  4. Use references for other services. If the database service is named Postgres, an app variable can reference its connection value as shown below.
  5. Apply the configuration and deploy. Verify behavior through the app without dumping secret values into logs.
DATABASE_URL=${{Postgres.DATABASE_URL}}

The service name and exported variable must match your actual project. This example uses Railway’s reference syntax, documented in Using Variables. A reference avoids manually copying a password into every dependent service; it does not change which code receives the value.

Understand build time and runtime

A server can read a runtime variable when it starts or handles a request, depending on its code. A static frontend has already been turned into files. Changing a dashboard value cannot rewrite JavaScript that was built yesterday.

Vite documents that VITE_ values are exposed in the client bundle. Next.js documents the public NEXT_PUBLIC_ prefix and build-time inlining. Read the rules for your framework instead of assuming that all environment variables remain on the host.

For an API base URL, rebuild after the change and inspect the destination of an actual browser request. Confirm the page is loading the new deployment, not an old preview link. For a backend secret, test the server operation that uses it and log only whether configuration is present, never the secret itself.

Keep the browser behind an API boundary

Suppose an AI-built app calls a model provider. The browser should send its request to your backend; the backend authenticates the user, applies the application’s usage rules and calls the provider with the secret key. Avoid creating an unauthenticated proxy that anyone can use to spend your API budget.

The same pattern applies to database credentials. A frontend needs an application interface, not a database superuser password. If a backend provider explicitly supports direct browser access, configure its authorization rules and use the intended public credential rather than substituting a privileged one.

For a Python implementation, start with deploying FastAPI. For database components, read backend choices for an AI-built app.

Use private connections in the right place

A Railway backend can use an internal database connection when both services share the project environment. A visitor’s browser cannot resolve that private service path, and an externally hosted backend is outside the network too. Railway explains the scope in its private domain documentation.

If the frontend is hosted elsewhere, expose the API over HTTPS and keep the database connection behind it. Configure authentication and the allowed browser origins on that API. A network boundary alone does not decide which signed-in user may read a record.

Recover from a leaked key and keep configuration reviewable

If a privileged key reached a public bundle or repository, rotate it at the issuing provider. Removing a line from the current source does not invalidate a value that somebody already copied. Redeploy with the replacement and review how the old value became public.

Keep an .env.example with names and placeholders, and document which variables require a rebuild. Before sharing an AI-generated diagnostic transcript, check for connection strings and authorization headers. For teams needing a separate secrets-management product, our Infisical guide covers that option; the frontend/backend distinction still applies.

FAQ

Where do I add environment variables in Railway?

Select the service and open Variables. Add values there, or reference another service's variable, then apply the change with a deployment. Check that you are editing the intended project environment.

Is an API key private because I put it in Railway Variables?

Only if your application keeps it server-side. Variables exposed through frontend prefixes or returned in API responses can reach the browser. Hosting configuration does not override what the application publishes.

Can I put a database URL in VITE_DATABASE_URL?

No. Vite exposes VITE_ variables to client code. Keep database credentials in backend variables and have the browser call an authenticated API instead.

Why does a changed frontend variable still have the old value?

Public frontend variables are commonly embedded during the build. Rebuild and deploy the application after changing them, then verify the correct deployment and cached assets. Runtime-only changes do not rewrite an existing JavaScript bundle.