[SOLVED] Moralis error A0003 ApiKey not set

Hello. I could use some help finding out why my ApiKey is not working.

import Moralis from 'moralis';

import { EvmChain } from '@moralisweb3/evm-utils';

function Native({ nativeBalance, address }) {

    return (

        <div>

            <h3>Wallet: {address}</h3>

            <h3>Native Balance: {nativeBalance} ETH</h3>

        </div>

    );

}

export async function getServerSideProps(context) {

    await Moralis.start({ apiKey: process.env.MORALIS_API_KEY });

    const address = '0x...';

    const nativeBalance = await Moralis.EvmApi.balance.getNativeBalance({

        chain: EvmChain.ETHEREUM,

        address,

    });

    return {

        props: { address, nativeBalance: nativeBalance.result.balance.ether },

    };

}

export default Native;

For my env.local file

APP_CHAIN_ID=0x1
APP_DOMAIN=ethereum.boilerplate 
MORALIS_API_KEY=mySuperSecretWeb3ApiKey
NEXTAUTH_SECRET= # Linux: `openssl rand -hex 32` or go to https://generate-secret.now.sh/64
NEXTAUTH_URL=http://localhost:3000

I have tried hard coding my ApiKey in the process.env location and it worked perfectly. Any help on what I am missing would be wonderful.

Thanks much
Galant

Do you mean you are not able to access from env.local file?

Maybe try restarting the localhost and refresh the browser window. Usually, it is not required to restart the localhost in nextjs but in this case it seems like it is not loaded from env.

Correct. My app is not pulling the Key from the env.local file. But if I hard code my Key in the process.env location then it works.

//await Moralis.start({ apiKey: process.env.MORALIS_API_KEY });// Not working.

//await Moralis.start({ apiKey: β€˜24q53MyApiKey865873’ });// Does work.

Clues?

Thanks much
Galant

I figured it out. I had β€œenv.local”. It is supposed to be β€œ.env.local”. The period in front of env matters!

1 Like