Next.js 15.5.12 Cannot Read OPENAI_API_KEY Service Variable
beebond
HOBBYOP

25 days ago

This is similar to issue "My backend service cannot read the DATABASE_URL environment variable" (10 days ago).

Problem: Service Variables are not accessible at runtime in Next.js 15.5.12.

What works:

- Railway system variables (RAILWAY_PUBLIC_DOMAIN, etc.) are readable

- Application builds successfully

- NODE_ENV shows as "production"

What doesn't work:

- Custom Service Variables (OPENAI_API_KEY) are completely missing

- process.env.OPENAI_API_KEY returns undefined

Diagnostic output shows all available env vars, but OPENAI_API_KEY is not listed.

Already tried:

1. Removed output: 'standalone' from next.config.js

2. Recreated Service Variables multiple times

3. Verified case sensitivity (OPENAI_API_KEY)

4. Confirmed I'm in correct project and service

I need this resolved for production.

$10 Bounty

2 Replies

Are you using Docker?


darseen
HOBBYTop 5% Contributor

24 days ago

Are you trying to use that API key on the client? Because env variables that are not prefixed with NEXT_PUBLIC_ are not available on the client, only on the server (server actions, API routes, and server components). Since this is a variable that is meant to be used on the server. I recommend that you check where you're using it.


Loading...