# Configure pyodk via environment variables, as well as config file

**URL:** <https://forum.getodk.org/t/configure-pyodk-via-environment-variables-as-well-as-config-file/53558>\
**Category:** Development\
**Tags:** pyodk\
**Created:** [February 11, 2025, 4:18pm UTC](https://forum.getodk.org/t/configure-pyodk-via-environment-variables-as-well-as-config-file/53558 "2025-02-11T16:18:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![spwoodcock](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/spwoodcock/32/33091_2.png) [@spwoodcock](https://forum.getodk.org/u/spwoodcock)\
**Post date:** [February 11, 2025, 4:18pm UTC](https://forum.getodk.org/t/configure-pyodk-via-environment-variables-as-well-as-config-file/53558/1 "2025-02-11T16:18:13Z")

</div>

## Issue

- pyodk currently has the option for configuration by a TOML file on the filesystem.
- This is fine for scripting.
- For integrating into applications, it would be much nicer to configure via [environment variables](https://12factor.net/config).

## Solution

- Add code to pick up environment variables related to pyodk.

- A config file should generally take precedence over these environment variables, if both are set.

- The variables could be:

## Question

- As requested, I didn't raise a PR for this, instead asking the community first.
- Would it be worthwhile to add a small handler for environment variables in pyodk?
- Note that I created a [branch/PR](https://github.com/getodk/pyodk/pull/107) on my fork with these changes already, with tests and docs. If this is approved on the forums, I could make the PR against the official repo.

---

<div class="post-metadata">

**Author:** ![yanokwa](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/yanokwa/32/1695_2.png) [@yanokwa](https://forum.getodk.org/u/yanokwa)\
**Post date:** [February 11, 2025, 8:09pm UTC](https://forum.getodk.org/t/configure-pyodk-via-environment-variables-as-well-as-config-file/53558/2 "2025-02-11T20:09:25Z")

</div>

I welcome this change.

I'd probably name the variables to more closely match the config file, but I could be convinced that these need `_CENTRAL_`.

```auto
PYODK_USERNAME=https://example.com
PYODK_USER=user@example.com
PYODK_PASSWORD=yourpassword
PYODK_DEFAULT_PROJECT_ID=1

```

I think it's more typical that env vars override config files, so I'd prefer that behavior.

What do you mean by add a small handler?

I'm not sure the wisdom of this, but could we also store the token in an env var?

---

<div class="post-metadata">

**Author:** ![spwoodcock](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/spwoodcock/32/33091_2.png) [@spwoodcock](https://forum.getodk.org/u/spwoodcock)\
**Post date:** [February 11, 2025, 9:48pm UTC](https://forum.getodk.org/t/configure-pyodk-via-environment-variables-as-well-as-config-file/53558/3 "2025-02-11T21:48:10Z")

</div>

Good point - thanks for the feedback!

To match the config file, would these vars work?

```dotenv
# the toml file variable is base_url
PYODK_BASE_URL=https://example.com
PYODK_USERNAME=user@example.com
PYODK_PASSWORD=yourpassword
PYODK_DEFAULT_PROJECT_ID=1

```

By small handler I basically just mean a little bit of code to handle the env vars - what I implemented in the PR 😄

Interesting idea for the token too - the cache\_path variable is in a lot of code though, so this would be quite a large refactor, perhaps best for another issue / v2?

Although doing that might have some complications. It's probably unlikely, but say someone is running pyodk in a service that has multiple replicas. It's easy to share the cache file between replicas and use the same token, but if this was set dynamically in the environment of a single replica, each replica would have a unique environment and wouldn't benefit from the shared session token. Not sure if this is a big deal though!

---

<div class="post-metadata">

**Author:** ![spwoodcock](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/spwoodcock/32/33091_2.png) [@spwoodcock](https://forum.getodk.org/u/spwoodcock)\
**Post date:** [February 12, 2025, 1:22am UTC](https://forum.getodk.org/t/configure-pyodk-via-environment-variables-as-well-as-config-file/53558/4 "2025-02-12T01:22:22Z")

</div>

Something that would be very useful is passing the config directly to the `Client` class too.

Changing:

```python
    def __init__ (
        self,
        config_path: str | None = None,
        cache_path: str | None = None,
        project_id: int | None = None,
        session: Session | None = None,
        api_version: str | None = "v1",
    ) -> None:
        self.config: config.Config = config.read_config(config_path=config_path)

```

to

```python
    def __init__ (
        self,
        config_path: str | config.CentralConfig | None = None,
        cache_path: str | None = None,
        project_id: int | None = None,
        session: Session | None = None,
        api_version: str | None = "v1",
    ) -> None:
        if isinstance(config_path, config.CentralConfig):
            self.config: config.Config = config.Config(central=config_path)
        else:
            self.config: config.Config = config.read_config(config_path=config_path)

```

This way we could also do:

```python
new_config = config.CentralConfig(
    base_url="https://obj.config.com",
    username="user@obj.config.com",
    password="ConfigPassword",
)
with Client(config_path=new_config) as client:
    return client.submissions.create(
        project_id=xxx,
        form_id=xxx,
        xml=xxx,
    )

```

The loading order would be:

1. Directly passed CentralConfig object.
2. Env vars.
3. User specified config\_path.
4. Default config\_path.
5. Error.

The reason I mention this is because I am using pyodk in an app where there are potentially multiple different ODK Central servers to contend with, so I need to easily swap between them. Doing this via env var or config file isn't ideal
