Repository navigation
How to use environment variables in Next.js on Vercel? #95017
SummaryI have a Next.js app deployed on Vercel. It works locally, but after deployment my API key or environment variable is showing as undefined. I already added the variable in my .env.local file, but it is not working on Vercel. What is the correct way to use environment variables in a Next.js project deployed on Vercel? Additional informationNo response ExampleNo response |
Replies: 2 comments 1 reply
|
bro BTW this is very common In Next.js if you search on google and youtube you can get it easily but not a problem .env.local only works on your local machine. For Vercel deployment, you need to add the same environment variables inside the Vercel dashboard. Go to: Vercel Dashboard → Project → Settings → Environment Variables
click on add environment variable
Add your variable there, then redeploy the project. Example local file: NEXT_PUBLIC_APP_URL=https://your-domain.vercel.app Use public variables in the browser with NEXT_PUBLIC_: const appUrl = process.env.NEXT_PUBLIC_APP_URL; Use private variables only on the server: export async function GET() { return Response.json({ |




bro BTW this is very common In Next.js if you search on google and youtube you can get it easily but not a problem .env.local only works on your local machine. For Vercel deployment, you need to add the same environment variables inside the Vercel dashboard.
Go to:
Vercel Dashboard → Project → Settings → Environment Variables
click on add environment variable
Add your variable there, then redeploy the project.
Example local file:
NEXT_PUBLIC_APP_URL=https://your-domain.vercel.app
DATABASE_URL=your_database_url
API_SECRET_KEY=your_secret_key
Use public variables in the browser with NEXT_PUBLIC_:
const appUrl = process.env.NEXT_PUBLIC_APP_URL;
console.log(appUrl);
Use private variables o…