ยฉ 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
Cloudflare DevelopersCD
Cloudflare Developersโ€ข2y agoโ€ข
10 replies
michelle

Env Vars in Worker

Hi, I am having an issue using an environment variable in my Worker code. I have set the environment variable in my Worker's dashboard, and am trying to access it in my Worker JS code, but it won't work. When I console log the env var it either shows up as 'Null' or 'Cannot read properties of undefined'.

I have tried to simplify my code to see if I can pinpoint the issue, but am not sure what I'm doing wrong. Is it necessary to use a Wrangler file, or am I implementing it incorrectly?

addEventListener('fetch', event => {
    event.respondWith(handleRequest(event));
  });
  
  async function handleRequest(event) {
    const { env } = event;
    
    // Access the environment variable
    const myEnvVar = env.MAILCHIMP_API_KEY;
  
    // Log the environment variable
    console.log(
addEventListener('fetch', event => {
    event.respondWith(handleRequest(event));
  });
  
  async function handleRequest(event) {
    const { env } = event;
    
    // Access the environment variable
    const myEnvVar = env.MAILCHIMP_API_KEY;
  
    // Log the environment variable
    console.log(
MY_ENV_VAR: ${myEnvVar}
);
  
    // Respond with a simple message
    return new Response(
);
  
    // Respond with a simple message
    return new Response(
Environment variable MAILCHIMP_API_KEY: ${myEnvVar}
, {
      headers: { 'Content-Type': 'text/plain' },
    });
  }
, {
      headers: { 'Content-Type': 'text/plain' },
    });
  }
Cloudflare Developers banner
Cloudflare DevelopersJoin
Welcome to the official Cloudflare Developers server. Here you can ask for help and stay updated with the latest news
85,042Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

hiding open source worker env vars
Cloudflare DevelopersCDCloudflare Developers / workers-and-pages-help
3y ago
wrangler deployment overwrites env vars in the worker
Cloudflare DevelopersCDCloudflare Developers / workers-and-pages-help
12mo ago
Can't access env vars in Workers build
Cloudflare DevelopersCDCloudflare Developers / workers-and-pages-help
6mo ago
how can I use the env vars in .env when deploying a worker via wrangler?
Cloudflare DevelopersCDCloudflare Developers / workers-and-pages-help
4mo ago