Access environmental variable inside nextjs edge api


Just to be specific. I have two questions.

  1. when deploying nextjs on netilfy, will the api route using edge runtime becomes edge functions in netlify environment as well ?

  2. how to access environmental variables if treated as edge function. will process.env still work or I have to use the netlify.env global variable. Is there any good example showing how ? Thank you ,
    The below is one example i’m trying to use but failed to find Netlify.env under “netlify dev” testing .

export const config = {
    runtime: 'experimental-edge'

export default () =>
    new Response(JSON.stringify(Netlify.env.toObject(), null, 2));

Hi @weidezhang :wave:t6: ,

Welcome to the community and thanks for reaching out!

  1. When deploying Next.js on Netlify, the API routes using the Next.js edge runtime will indeed become edge functions in the Netlify environment.
  2. You should use the process.env approach rather than Netlify.env. The Netlify.env variable is specific to Netlify Dev, which is used for local development + testing. In the actual production environment on Netlify, you can access envr variables using process.env. You can read more about this in the Next.js documentation.