How to use .env in ICP

I wanted some of the variable defined in ICP , how to use .env in ICP ?

1 Like

This worked for me (in a backend Rust canister):

  1. Create a .env file in the root of the project repository. For example, let’s say your .env file looks like this:
MAIL_API_KEY='dddddddddd'
POSTMAN_API_KEY='aaaaaaaaaa'
  1. You must then export the entire .env file by running these commands:
set -a
source .env
set +a
  1. When you need to call the environment variable in your Rust canister, in this format: option_env!("name_of_the_environment_variable").

Using the .env file above as an example:

option_env!("MAIL_API_KEY")

4 Likes

@jennifertran how is that secure? Are the env vars exposed?

The canister state is private to a certain degree (for example, a user who is not a canister controller cannot inspect a private function). However, there is technically a risk of malicious subnet nodes inspecting the canister state.