API Token must be provided

Hi I am using Gatsby Cloud integrated with DatoCMS. I met with an issue when I cloned the github repo to my local. Firstly I added .env as per instructions and ran npm run develop. It threw an error “API Token must be provided!”. I replaced .env with .env.development and renamed DATO_API_TOKEN to GATSBY_DATO_API_TOKEN but error persists. However if I replace apiToken directly with the key, it works:

Repo here

gatsby-config.js

.env.development pic
.env

Hi @zxlvera

when using your gatsby project locally, your token must be saved inside .env file. And the variable name must match with the one you use in gatsby-config.js.

So, based on the screenshots you posted here, you should have a .env file with this content:

GATSBY_DATO_API_TOKEN=yourTokenHere

Does it work now?

1 Like

Hi @fabrizio ! Thanks for helping out.

I created an .env file as described but it is not working.

I’m sorry I did not noticed you configured dotenv differently from our demo.

I’m looking at this file now: https://github.com/zxlvera/portfolio/blob/main/gatsby-config.js which is different from your screenshot.

So, unless you specify a NODE_ENV you need to create a .env.development file inside your project root directory with DATO_API_TOKEN.

If this still does not work, try to simplify the code. Use require('dotenv').config() and place the token in the .env file instead.

The reason I changed to .env.development is because .env with require('dotenv').config() was not working for me haha, I changed it back and it is still throwing back errors. .env cannot be detected no matter what I do.

An update! I am not sure what happened but I clone the repo to Linux/Pop_OS local the project is not throwing errors about API Token anymore. Previously I cloned to repo to Windows 10. My next issue is when I opened http://localhost:8000/ the website keeps flicking? Ctrl+Shift+I

hi @zxlvera oh I see. Then I think dotenv package has some problems running on Windows. I suggest you to search for windows related issues on their repo https://github.com/motdotla/dotenv

Thank you so much for the support! :smiley:
In case anyone has the same flickering issue in Firefox, the workaround is to disable 'Delete cookies and site data when Firefox is closed" in Preference. https://github.com/gatsbyjs/gatsby/issues/16076

1 Like