How to use Netlify envornment variable in template

I am using Hugo-universal-theme, github, and netlify. Everything works great, except I cannot use my Google Maps API key in the config.toml file because then it will be exposed to the public via github. Netlify has the ability to create environment variables, so I created one called GOOGLEMAPS_API_KEY with my key in it. How do I use that in the config.toml. If I simply try

googleMapsApiKey = GOOGLEMAPS_API_KEY

I get an error relating to the params.googleMapsApiKey.

Thanks, Roger

Hi @rogerjbos. You’re close. You’ll wanna get your env var from inside the template you need it in. See this function:

Thanks for the suggestion, but maybe I am not interpreting the docs correctly. Trying the following in my config.toml file

googleMapsApiKey = {{ getenv "GOOGLEMAPS_API_KEY" }}

gives an error that bare keys cannot contain “{”. I need a newbie explanation.

Let me clarify. Don’t use that function in your config file. Use it in your template. Aka layouts/_default/baseof.html or something like that

1 Like