# Setting environment variables dynamically

**URL:** <https://discourse.jupyter.org/t/setting-environment-variables-dynamically/9982>\
**Category:** JupyterHub\
**Tags:** help-wanted\
**Created:** [July 16, 2021, 12:11am UTC](https://discourse.jupyter.org/t/setting-environment-variables-dynamically/9982 "2021-07-16T00:11:37Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![hsghori](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.jupyter.org/hsghori/32/4766_2.png) [@hsghori](https://discourse.jupyter.org/u/hsghori)\
**Post date:** [July 16, 2021, 12:11am UTC](https://discourse.jupyter.org/t/setting-environment-variables-dynamically/9982/1 "2021-07-16T00:11:37Z")

</div>

Hey,

Is there a way to initialize either a Spawner or a Notebook with environment variables computed “dynamically” (eg per user)? I have a web app which uses the API to interact with JupyterHub (and the single user servers), but each user has unique credentials which they would use to authenticate with the database and I’d like for those credentials to be available in the user environment as opposed to users having to paste them in or having them otherwise exposed in the notebook.

I’m using JupyterHub 1.4.1 with the default DockerSpawner. I’ve seen [this GH issue](https://github.com/jupyterhub/dockerspawner/issues/352) which implies that while it may not be possible with the default maybe a different spawner will let me do what i’m trying to do?

---

<div class="post-metadata">

**Author:** ![hsghori](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.jupyter.org/hsghori/32/4766_2.png) [@hsghori](https://discourse.jupyter.org/u/hsghori)\
**Post date:** [July 16, 2021, 12:34am UTC](https://discourse.jupyter.org/t/setting-environment-variables-dynamically/9982/2 "2021-07-16T00:34:37Z")

</div>

Gonna answer my own question 'cause I figured it out literally right after posting.

In `jupyterhub_config.py` I did:

```auto
from dockerspawner import DockerSpawner

class EnvDockerSpawner(DockerSpawner):
    def get_env(self):
        env = super().get_env()
        user_options_env = self.user_options.get("env", {})
        env.update(user_options_env)
        return env

c.JupyterHub.spawner_class = EnvDockerSpawner

```

Then in my api I do:

```auto
rv = requests.post(f"{URL}/users/{username}/server", json={"env": {"DATA": user.json_data}}, headers=_get_notebook_server_header(user))

```
