I set up .env file and gatsby-config.js as below.
// .env.development GATSBY_API_URL=https://example.com/api
Maybe worth noting that it's easy to misname the file if you are used to writing .dev or .develop.
.dev
.develop
Gatsby requires that the file is named exactly: .env.development
.env.development