Workers API ReferenceError

I’m getting the following error from my Workers API:

  "exceptions": [
    {
      "name": "ReferenceError",
      "message": "API_KEY is not defined",
      "timestamp": 1673733383244
    }
  ],

Why is that? I’ve already set the value via the Cloudflare Workers settings:

  1. Go back to the your worker’s dashboard by clicking on ‘Save & Deploy’ and then exit out of the editor.
  2. Under the Settings panel, click on Variables. Here you can set the environment variable for the worker.
  3. Click on ‘Add variable’ and input the variable name as API_KEY and the value as the bot’s API token and “save settings & deploy”.

And I’ve tried it several times, but still, I’m getting the above “API_KEY is not defined” error. Why & how to fix the problem?

Can you share your full Worker code?

Variables are only globals with the older Service Worker syntax. They’re available on env in newer Module Workers. Migrating to module Workers · Cloudflare Workers docs

Oh, it was just directly accessing ${API_KEY}

Yep. working now. THX!!!

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.