Support Forums

Save token to a file using Netlify function

Hi. I’m wanting to either cache a token used for a third-party API or write it to a file, using a Netlify function. Is this possible? I don’t think it’s possible to update an env variable via a function, is it?

I think it is:

If you expand the build_settings object, there’s an env object which you can programmatically update.

Ah, thank you. I wasn’t aware of this. I’ll check it out.

But, hold on, this would mean I’d need to keep rebuilding the app to update the token, right?

I suppose that depends on the purpose of the env variable. If it’s used by your front-end: yes. If it’s only used by your functions: not so much.

Thanks for this. It works with a few caveats:

  • You have to re-save all of your env variables or you lose them.
  • When using netlify dev in the CLI, you have to quit and restart netlify dev for the new env variable to take effect.

That’s quite useful information, thanks for reporting back!

1 Like