I set up .env file and gatsby-config.js as below.
// .env.development GATSBY_API_URL=https://example.com/api
Make sure you've included
require("dotenv").config({ path: .env.${process.env.NODE_ENV}, })
.env.${process.env.NODE_ENV}
in your gatsby-config.js file before you start using your ENV variables.