Store model credentials in .env files
Learn how to store model identifier credentials in a .env file instead of using inline credentials. This topic is relevant for model developers who want to follow best practices for security when running notebooks.
Why is this recommended?
Storing credentials in a .env file is considered a best practice for security. Embedding credentials directly within the code makes them more susceptible to accidental exposure when sharing code or collaborating on models.
Keeing model credentials in a separate file also allows for precise access control and ensures that sensitive credentials are not publically accessible.
Prerequisites
Steps
Create a new file in the same folder as your notebook and name it
.env.This is a hidden file, so you may need to change your settings to view it.
Locate the code snippet for your model:
- In the left sidebar, click Inventory.
- Select a model by clicking on it or find your model by applying a filter or searching for it.3
- In the left sidebar that appears for your model, click Getting Started.
- Locate the code snippet and click Copy snippet to clipboard.
Paste the credentials into your
.envfile in the following format:VM_API_HOST=<api_host> VM_API_KEY=<api_key> VM_API_SECRET=<api_secret> VM_API_MODEL=<model>
For example, if your credentials look like this:
import validmind as vm
vm.init(
api_host = "https://api.prod.validmind.ai/api/v1/tracking",
api_key = "key123",
api_secret = "secret456",
model = "model789"
)Then your .env file should look like this:
VM_API_HOST=https://api.prod.validmind.ai/api/v1/tracking
VM_API_KEY=key123
VM_API_SECRET=secret456
VM_API_MODEL=model789Insert this code snippet above your model identifier credentials:
%load_ext dotenv %dotenv dev.envRemove the inline credentials from
vm.init().[^4]The updated code cell should now look like this:
%load_ext dotenv %dotenv .env import validmind as vm vm.init( )
To enable monitoring when you are storing credentials in an .env file:4
%load_ext dotenv
%dotenv .env
import validmind as vm
vm.init(
monitoring=True
)- Run the cell. Instead of using inline credentials, this cell will now load your model credentials from a
.envfile.