# How to get environment variables?

**URL:** <https://discuss.circleci.com/t/how-to-get-environment-variables/12013>\
**Category:** Build Environment\
**Tags:** circle-yml\
**Created:** [April 17, 2017, 11:00pm UTC](https://discuss.circleci.com/t/how-to-get-environment-variables/12013 "2017-04-17T23:00:22Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![aderhg](https://avatars.discourse-cdn.com/v4/letter/a/76d3ee/32.png) [@aderhg](https://discuss.circleci.com/u/aderhg)\
**Post date:** [April 17, 2017, 11:00pm UTC](https://discuss.circleci.com/t/how-to-get-environment-variables/12013/1 "2017-04-17T23:00:22Z")

</div>

I am storing certain sensitive data in environment variables in CircleCI’s project settings. How do I add them to my circle.yml file? I tried the following, but it doesn’t work:

```
machine:
  python:
    version: 3.5.1
  environment:
    CLIENT_ID: $CLIENT_ID
    CLIENT_SECRET: $CLIENT_SECRET

```

Is this how I’m supposed to define environment variables so that CircleCI gets them from the project settings?

---

<div class="post-metadata">

**Author:** ![FelicianoTech](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/felicianotech/32/21348_2.png) [@FelicianoTech](https://discuss.circleci.com/u/FelicianoTech)\
**Post date:** [April 17, 2017, 11:54pm UTC](https://discuss.circleci.com/t/how-to-get-environment-variables/12013/2 "2017-04-17T23:54:07Z")

</div>

Those last two lines aren’t needed here. You’d just use `$CLIENT_ID` or `${CLIENT_ID}` in `circle.yml` where it’s needed. The’d also be available in scripts called via `circle.yml`.

This is assuming that `$CLIENT_ID` and `$CLIENT_SECRET` were configured as Environment Variables via CircleCI’s UI or API.

---

<div class="post-metadata">

**Author:** ![aderhg](https://avatars.discourse-cdn.com/v4/letter/a/76d3ee/32.png) [@aderhg](https://discuss.circleci.com/u/aderhg)\
**Post date:** [April 18, 2017, 2:21am UTC](https://discuss.circleci.com/t/how-to-get-environment-variables/12013/3 "2017-04-18T02:21:39Z")

</div>

These variables are needed to run my tests. I have defined them in our project settings through CircleCI’s UI. I tried defining those variables in my circle.yml file because my tests cannot find those variables. Circle CI’s build page says that it is exporting those variables:

```
Exporting CLIENT_ID
Exporting CLIENT_SECRET

```

Why can’t my tests find them?

---

<div class="post-metadata">

**Author:** ![holdonowgo](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/holdonowgo/32/10638_2.png) [@holdonowgo](https://discuss.circleci.com/u/holdonowgo)\
**Post date:** [July 9, 2017, 5:27am UTC](https://discuss.circleci.com/t/how-to-get-environment-variables/12013/4 "2017-07-09T05:27:17Z")

</div>

Hey aderhg, Did you ever solve this issue? I think I am running into the same thing. My Mocha tests can’t read ENV variables I have set through the Circle CI UI.

---

<div class="post-metadata">

**Author:** ![FelicianoTech](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/felicianotech/32/21348_2.png) [@FelicianoTech](https://discuss.circleci.com/u/FelicianoTech)\
**Post date:** [July 10, 2017, 5:49pm UTC](https://discuss.circleci.com/t/how-to-get-environment-variables/12013/5 "2017-07-10T17:49:07Z")

</div>

Some questions:

Mocha is running as the same user correct?  
Is this running within Docker?  
This is about CircleCI 1.0?  
Are you using the right capitalization for the variables?  
Are you storing non-ASCII characters in the variables?  
Also, maybe this? [https://stackoverflow.com/questions/44598333/referencing-environment-variables-for-mocha-tests](https://stackoverflow.com/questions/44598333/referencing-environment-variables-for-mocha-tests)

---

<div class="post-metadata">

**Author:** ![holdonowgo](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/holdonowgo/32/10638_2.png) [@holdonowgo](https://discuss.circleci.com/u/holdonowgo)\
**Post date:** [July 27, 2017, 5:59am UTC](https://discuss.circleci.com/t/how-to-get-environment-variables/12013/6 "2017-07-27T05:59:42Z")

</div>

Hi!

Thanks for the reply.

- I believe Mocha is running as the same user. How can I check this? I have done nothing that would lead me to believe this is not the case
- This is not running within Docker
- I think this is CircleCI 2.0
- I am using the correct capitalization for the variables
- I am not storing non-ASCII characters in the variables

I checked the link you provided, but am still confused.

In my app.js I have this:

```
if (process.env.NODE_ENV !== 'production') {
  require('dotenv').config();
}

```

I guess my question is why when in my test:

```
const authToken = process.env.AUTH_TOKEN

```

process.env.AUTH\_TOKEN is not found when run by CircleCI?

Do these env variables need to be imported via the circle.yml file first and then retrieved from there?  
Would I need to do something like this in my circle.yml file:

```
machine:
  environment:
    AUTH_TOKEN: $AUTH_TOKEN
    TEST_USER_NAME: $TEST_USER_NAME
    TEST_PASSWORD: $TEST_PASSWORD

```

Please forgive my confusion and thank you again.

---

<div class="post-metadata">

**Author:** ![red2678](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/red2678/32/26364_2.png) [@red2678](https://discuss.circleci.com/u/red2678)\
**Post date:** [January 27, 2018, 9:40pm UTC](https://discuss.circleci.com/t/how-to-get-environment-variables/12013/7 "2018-01-27T21:40:09Z")

</div>

Hi, did you ever fingure this out?

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/circleci/original/3X/5/1/511608acda8e85906e15909b57bd0a46c1dbe291.png) [@system](https://discuss.circleci.com/u/system)\
**Post date:** [June 18, 2018, 11:39am UTC](https://discuss.circleci.com/t/how-to-get-environment-variables/12013/8 "2018-06-18T11:39:24Z")

</div>


