# How to use .env in ICP

**URL:** <https://forum.dfinity.org/t/how-to-use-env-in-icp/35200>\
**Category:** Rust\
**Created:** [September 16, 2024, 11:37am UTC](https://forum.dfinity.org/t/how-to-use-env-in-icp/35200 "2024-09-16T11:37:26Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rachit2323](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/rachit2323/32/19417_2.png) [@Rachit2323](https://forum.dfinity.org/u/Rachit2323)\
**Post date:** [September 16, 2024, 11:37am UTC](https://forum.dfinity.org/t/how-to-use-env-in-icp/35200/1 "2024-09-16T11:37:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jennifertran](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/jennifertran/32/11558_2.png) [@jennifertran](https://forum.dfinity.org/u/jennifertran)\
**Post date:** [September 18, 2024, 1:19am UTC](https://forum.dfinity.org/t/how-to-use-env-in-icp/35200/2 "2024-09-18T01:19:14Z")

</div>

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:

```auto
MAIL_API_KEY='dddddddddd'
POSTMAN_API_KEY='aaaaaaaaaa'

```

1. You must then export the entire `.env` file by running these commands:

```auto
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")`

---

<div class="post-metadata">

**Author:** ![xpluscal](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/xpluscal/32/23915_2.png) [@xpluscal](https://forum.dfinity.org/u/xpluscal)\
**Post date:** [January 24, 2025, 11:09pm UTC](https://forum.dfinity.org/t/how-to-use-env-in-icp/35200/3 "2025-01-24T23:09:35Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jennifertran](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/jennifertran/32/11558_2.png) [@jennifertran](https://forum.dfinity.org/u/jennifertran)\
**Post date:** [January 25, 2025, 1:13am UTC](https://forum.dfinity.org/t/how-to-use-env-in-icp/35200/4 "2025-01-25T01:13:13Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![AliSci](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/alisci/32/33799_2.png) [@AliSci](https://forum.dfinity.org/u/AliSci)\
**Post date:** [August 25, 2025, 10:10am UTC](https://forum.dfinity.org/t/how-to-use-env-in-icp/35200/5 "2025-08-25T10:10:49Z")

</div>

when i run `dfx deploy backend –network ic` the env will be uploaded as well, or should I run the another command again for ic?

---

<div class="post-metadata">

**Author:** ![jasonzhu](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/jasonzhu/32/12251_2.png) [@jasonzhu](https://forum.dfinity.org/u/jasonzhu)\
**Post date:** [August 28, 2025, 6:26pm UTC](https://forum.dfinity.org/t/how-to-use-env-in-icp/35200/6 "2025-08-28T18:26:50Z")

</div>

> [@AliSci](#):
>
> the env will be uploaded as wel

What exactly do you mean by “upload”? As far as I can tell, the `option_env!` should make it so that the env variable value is part of the WASM, and WASM is more or less private as stated above.

---

<div class="post-metadata">

**Author:** ![aespieux](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/aespieux/32/22748_2.png) [@aespieux](https://forum.dfinity.org/u/aespieux)\
**Post date:** [September 3, 2025, 10:53am UTC](https://forum.dfinity.org/t/how-to-use-env-in-icp/35200/7 "2025-09-03T10:53:17Z")

</div>

If you want to store API keys in a secure way look into vetkeys, it’s a new feature

---

<div class="post-metadata">

**Author:** ![peterparker](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@peterparker](https://forum.dfinity.org/u/peterparker)\
**Post date:** [September 3, 2025, 12:06pm UTC](https://forum.dfinity.org/t/how-to-use-env-in-icp/35200/8 "2025-09-03T12:06:48Z")

</div>

I’m not an expert in VetKeys, but I understand they don’t address the use case of “saving API keys in/for your backend”. If they do, I’d be really curious to learn how because that sounds interesting?

---

<div class="post-metadata">

**Author:** ![georgiod9](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/georgiod9/32/21654_2.png) [@georgiod9](https://forum.dfinity.org/u/georgiod9)\
**Post date:** [September 13, 2025, 6:17am UTC](https://forum.dfinity.org/t/how-to-use-env-in-icp/35200/9 "2025-09-13T06:17:21Z")

</div>

Vetkeys do not solve the problem in this case. As far as I know, with vetkeys a canister cannot encrypt and decrypt keys.

They are designed to be used by external principals like identities. The encryption/decryption is actually done on the client side once you have a ecdsa public key
