Variables: Can they be defined by users?

I’m documenting some API use cases. Many of our REST calls include options like an “API Token”

My hope: find a way to use Hugo to set up variables.

Ideally, a reader would enter a value for an API_Token, and Hugo would populate that same value to all REST calls with that variable.

I’ve looked through what I could with User-Defined variables, and I think "no, that doesn’t help.


In your site configuration you can do something like:

api_token = 'abc123'

And access that value in your templates with:

{{ site.Params.api_token }}


Are you looking to use environment variables, or does what jmooring suggested do what you need?

Thanks for the response. I apologize for the delay in my follow-up.

In this particular case, I’d rather not include the API Token in something like config.toml, as API Tokens are Personally Idenifying Information.

@cshoredaniel Would environment variables limit access to specific users?

First, here is info on setting params via environment variables:

e.g. HUGO_PARAMS_API_TOKEN=sometoken hugo (don’t do this; sometoken would end up in your history; this is just for illustration)

would build the site with (IIRC) site.Params.api_token set to sometoken (param names get lower cased, but the value is maintained)

Obviously if only certain users have HUGO_PARAMS_API_TOKEN set to the correct API token in their environment then only they will get the right resulits.

With services like GitHub there are ways to setup CI pipelines that allow users to use the token without being able to find out what the token are, but that’s a service specific kind of thing, and off-topic for this forum.

I’m assuming you are not putting the API token in the site HTML or other public file of course.

And re-reading I’m wondering if what you are looking for is more dynamic rather than static site.

I was initially thinking you were using the API key to grab something to include in the with e.g. resources.GetRemote, but now I’m thinking I misunderstood.