Passing env vars to already running notebook

I have a setup where user info(some API tokens) are passed to jupyternotebook as env variables by the spawnner using pre_spawn_start function in a custom Authenticator. I have some magic functions which access this API token from environment variable to make an API call. My problem is, if the API token expires after the notebook starts, I am no longer able to use the magic functions. Is there a way to either update this API token env var or pass the API token some other way to the note book?

You can’t modify an environment variable after the server is started, so you’ll have to fetch the token using an API call and pass it some other way.

One way is to hook into the refresh_user call, for example see
https://oauthenticator.readthedocs.io/en/latest/how-to/refresh.html#refreshing-tokens-from-user-sessions

The alternative is to use a separate service that authenticates the user using the JupyterHub API token, and returns the other API tokens: