> ## Documentation Index
> Fetch the complete documentation index at: https://www.truefoundry.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Environment Variables

> Use environment variables in your TrueFoundry deployments to configure model paths, endpoints, and other settings.

An environment variable is a value that affects the way the code runs and is dependent on the environment on which it is running.

For example, you have written a ML model API service that,

1. Downloads the model from somewhere.
2. Loads the model from disk.
3. Serves a `/infer` route, which calls the model's inference function.

Now, your service code may not change if you re-train and update the model. In this case, we can pass the model path via an environment variable.

**`main.py`**

<CodeGroup>
  ```python Python lines theme={"dark"}
  import os

  from fastapi import FastAPI
  import mlfoundry as mlf
  client = mlf.get_client()

  # NOTE: `MODEL_FQN` variable will now contain the value
  # of environment variable `MODEL_FQN`
  MODEL_FQN = os.getenv("MODEL_FQN")
  MODEL = client.get_model(MODEL_FQN).load()

  S3_BUCKET = os.getenv("S3_BUCKET_NAME")

  app = FastAPI()


  @app.get("/infer")
  def infer(...):
      ...
  ```
</CodeGroup>

You can then run this service and inject the environment variable value like below,

<CodeGroup>
  ```shell Shell lines theme={"dark"}
  MODEL_FQN="YOUR MODEL FQN" S3_BUCKET_NAME="my-s3-bucket" uvicorn main:app --port 8000 --host 0.0.0.0
  ```
</CodeGroup>

You can also use a `.env` file on your local dev environment and use [python-dotenv](https://pypi.org/project/python-dotenv/).

**`.env`**

```shell lines theme={"dark"}
MODEL_FQN="YOUR MODEL FQN FOR LOCAL RUN"
S3_BUCKET_NAME="S3 bucket for local development"
```

## How to inject environment variables in TrueFoundry

In this guide we will learn how can we inject environment variables in our deployments in TrueFoundry.

<CodeGroup>
  ```python Python lines theme={"dark"}
  """
  Both `Service ` and `Job` classes have an argument `env` where you can pass a dictionary. The dictionary keys will be assumed as environment variable names and the values will be the environment variable values.
  """

  import logging

  from servicefoundry import Build, Service, DockerFileBuild

  logging.basicConfig(level=logging.INFO)
  service = Service(
      name="my-service",
      image=Build(build_spec=DockerFileBuild()),
      ports=[{"port": 8501}],
      env={
        "MODEL_FQN": "YOUR MODEL FQN",
        "S3_BUCKET_NAME": "my-s3-bucket"
      },
  )
  service.deploy(workspace_fqn="YOUR_WORKSPACE_FQN")
  ```

  ```python YAML lines theme={"dark"}
  # You can add your environment variables under `env:` section as key value pairs.

  name: my-service
  components:
    - name: my-service
      type: service
      image:
        type: build
        build_source:
          type: local
        build_spec:
          type: dockerfile
      ports:
       - port: 8501
      env:
        MODEL_FQN: "YOUR MODEL FQN"
        S3_BUCKET_NAME: my-s3-bucket
  ```
</CodeGroup>

The variables `MODEL_FQN` and `S3_BUCKET_NAME` should be available in your environment on deployment.
