[SOLVED] Moralis SDK API Error: [A0003] apiKey is not set

While running the tutorial “simple-nextjs-demo” I get the error (see below) but as I can tell it is correctly set in the evn.local file.
What am I missing?

error - MoralisError [Moralis SDK API Error]: [A0003] apiKey is not set
at EndpointResolver.createHeaders (J:_a\simple-nextjs-demo\node_modules@moralisweb3\api-utils\lib\resolvers\EndpointResolver.js:158:19)
at EndpointResolver. (J:_a\simple-nextjs-demo\node_modules@moralisweb3\api-utils\lib\resolvers\EndpointResolver.js:64:47)
at step (J:_a\simple-nextjs-demo\node_modules@moralisweb3\api-utils\lib\resolvers\EndpointResolver.js:33:23)
at Object.next (J:_a\simple-nextjs-demo\node_modules@moralisweb3\api-utils\lib\resolvers\EndpointResolver.js:14:53)
at J:_a\simple-nextjs-demo\node_modules@moralisweb3\api-utils\lib\resolvers\EndpointResolver.js:8:71
at new Promise ()
at __awaiter (J:_a\simple-nextjs-demo\node_modules@moralisweb3\api-utils\lib\resolvers\EndpointResolver.js:4:12)
at EndpointResolver.get (J:_a\simple-nextjs-demo\node_modules@moralisweb3\api-utils\lib\resolvers\EndpointResolver.js:55:47)
at Object.EndpointResolver.fetch [as getNativeBalance] (J:_a\simple-nextjs-demo\node_modules@moralisweb3\api-utils\lib\resolvers\EndpointResolver.js:115:34)
at getServerSideProps (webpack-internal:///./pages/native.jsx:48:95) {
isMoralisError: true,
code: ‘A0003’,
details: undefined,
page: ‘/native’,
[cause]: undefined
}
warn - Fast Refresh had to perform a full reload. Read more: https://nextjs.org/docs/basic-features/fast-refresh#how-it-works
error - MoralisError [Moralis SDK API Error]: [A0003] apiKey is not set
at EndpointResolver.createHeaders (J:_a\simple-nextjs-demo\node_modules@moralisweb3\api-utils\lib\resolvers\EndpointResolver.js:158:19)
at EndpointResolver. (J:_a\simple-nextjs-demo\node_modules@moralisweb3\api-utils\lib\resolvers\EndpointResolver.js:64:47)
at step (J:_a\simple-nextjs-demo\node_modules@moralisweb3\api-utils\lib\resolvers\EndpointResolver.js:33:23)
at Object.next (J:_a\simple-nextjs-demo\node_modules@moralisweb3\api-utils\lib\resolvers\EndpointResolver.js:14:53)
at J:_a\simple-nextjs-demo\node_modules@moralisweb3\api-utils\lib\resolvers\EndpointResolver.js:8:71
at new Promise ()
at __awaiter (J:_a\simple-nextjs-demo\node_modules@moralisweb3\api-utils\lib\resolvers\EndpointResolver.js:4:12)
at EndpointResolver.get (J:_a\simple-nextjs-demo\node_modules@moralisweb3\api-utils\lib\resolvers\EndpointResolver.js:55:47)
at Object.EndpointResolver.fetch [as getNativeBalance] (J:_a\simple-nextjs-demo\node_modules@moralisweb3\api-utils\lib\resolvers\EndpointResolver.js:115:34)
at getServerSideProps (webpack-internal:///./pages/native.jsx:48:95) {
isMoralisError: true,
code: ‘A0003’,
details: undefined,
page: ‘/native’,
[cause]: undefined

Hi can you read this on how to post code.

How have you set the environment variable for the API key in your Moralis code (process.env..)? Make sure it’s named correctly in your .env.local file in the root of your project directory.

You can test if the environment variable is the issue by hardcoding your API key.

Thank you

1 - Yes, I did read how to paste code, but this is not really “code”, merely an output from the terminal
2 - I am not setting anything in “process.env…” as you asked, but I have identical spelling in my .env.local file and in the tutorial, they are both
MORALIS_API_KEY

I am not setting anything in “process.env…” as you asked

You should be using something like process.env.MORALIS_API_KEY where in your .env.local file, you have MORALIS_API_KEY=keyhere. Can you post your code? Did you try hardcoding?

Yes, I did read how to paste code, but this is not really “code”, merely an output from the terminal

It makes it easier to read still.

No, I did not try to hard-code anything until now, but looks like it works.

Here is what I changed (from the tutorial)
//Commented following line out:
//await Moralis.start({ apiKey: process.env.MORALIS_API_KEY });

//and hard-coded the key:
await Moralis.start({ apiKey: ‘my hardcoded API key here…O4cels18FnA’});

And this seems to be working.
Just wondering if this is a bug, or typo, or something?
Thank you

That looks fine, there’s an issue somewhere else where it’s not picking up your MORALIS_API_KEY variable. Can you post a screenshot of your folder structure and your .env.local file (just hide/swap the API key value).

Also, make sure you restart your app server if you changed your .env.local file after starting it.

Yes, indeed it is not picking it up.
Here is the folder structure:
image
And the the env.local file has only one line:

const MORALIS_API_KEY = 'xO4c8..blah, blah......3o9FEXRi'

Remove const from your .env.local file, it needs to be just MORALIS_API_KEY=keyhere.

GREAT catch, Alex, much appreciated.

was having the same issues today had to change mine to

NEXT_PUBLIC_API_KEY= keyjbaksadhbashd