Building Hugo site with Netlify environment variables fails

I’m building a hugo site and want to use an API key at build, but being in a public repository wanted to store them as environment variables.

In Netlify, under build and deploy, the environment variable is set.
It states:
APIKEY

I’m able to pass the variable to hugo locally with the command:
env HUGOxPARAMSxAPIxKEY='Bearer SuperSecretKey' hugo --gc --minify

And my netlify build for deploy previews uses from within the netlify.toml:
[context.deploy-preview] command = "env HUGOxPARAMSxAPIxKEY='Bearer $APIKEY' hugo --gc --minify -b $DEPLOY_PRIME_URL"

I’m wondering if i’m missing something here, as the command to build with the deploy url is passed through.
The build command fails with still showing $APIKEY in the build command, and of course failing the build.

I’ve read through https://answers.netlify.com/t/support-guide-using-environment-variables-on-netlify-correctly/267 however find myself stuck.

Is it because the variables aren’t accessible in the build?
Can I somehow inject the variables into the build command, or do I need to try to create a file that will inject the variables so it is readable at build like npm run build && echo $APIKEY > config/production/params.toml?

Found the issue:

Netlify doesn’t inject environment variables if it’s within a quotation.

Changing my build command to: "env HUGOxPARAMSxAPIxKEY='Bearer '$APIKEY hugo --gc --minify -b $DEPLOY_PRIME_URL" worked!
(moving the $APIKEY out of the 'Bearer ’ area)

Hey there, @AdamXweb :wave:

Thanks so much for coming back and sharing your solutions with the Forums. This will definitely be beneficial for folks who encounter the same obstacle.

Happy building :rocket: