# Getting to environment variables from my api.js code

**URL:** <https://community.homey.app/t/getting-to-environment-variables-from-my-api-js-code/71791>\
**Category:** Developers\
**Created:** [November 6, 2022, 5:07pm UTC](https://community.homey.app/t/getting-to-environment-variables-from-my-api-js-code/71791 "2022-11-06T17:07:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![LvanderRee](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/lvanderree/32/1853_2.png) [@LvanderRee](https://community.homey.app/u/LvanderRee)\
**Post date:** [November 6, 2022, 5:07pm UTC](https://community.homey.app/t/getting-to-environment-variables-from-my-api-js-code/71791/1 "2022-11-06T17:07:07Z")

</div>

I am working on a new application (to integrate Casambi devices), for which I want to store the username and password to an external service via the settings-screen, so you don’t have to login for every device and can change the password on its gateway afterwards.

In order to access the external service I have to provide an API-key/secret as well, which I have placed in a env.json file, which is provided to Homey on install, and can be seen in my app.js code via Homey.env.API\_KEY. However this env is not available via my api.js code, in which Homey.env = {} .

My setttings form saves the username and password, after I tested via an API-call to my app on Homey (with the secret), that the username and password combination on the service or correct, but without secret this is of course not possible.

Is this done on purpose? Or is there a workaround for this?

The alternative is that I ask for a username and password when adding a device, but when you than change the password on the service, you have to reinstall every device, or is there an alternative for that?

---

<div class="post-metadata">

**Author:** ![robertklep](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/robertklep/32/160628_2.png) [@robertklep](https://community.homey.app/u/robertklep)\
**Post date:** [November 6, 2022, 6:34pm UTC](https://community.homey.app/t/getting-to-environment-variables-from-my-api-js-code/71791/2 "2022-11-06T18:34:58Z")

</div>

> [@LvanderRee](#):
>
> Is this done on purpose? Or is there a workaround for this?

Are you sure you’re using the correct `Homey`, and are you sure the `env.json` file is being uploaded? I can’t reproduce the issue:

```auto
// api.js
const Homey = require('homey');

console.log('ENV', Homey.env);

```

---

<div class="post-metadata">

**Author:** ![Adrian\_Rockall](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/adrian_rockall/32/13209_2.png) [@Adrian\_Rockall](https://community.homey.app/u/Adrian_Rockall)\
**Post date:** [November 6, 2022, 7:32pm UTC](https://community.homey.app/t/getting-to-environment-variables-from-my-api-js-code/71791/3 "2022-11-06T19:32:06Z")

</div>

> [@LvanderRee](#):
>
> The alternative is that I ask for a username and password when adding a device, but when you than change the password on the service, you have to reinstall every device, or is there an alternative for that?

The alternative is to provide a repair option to prompt for the new password.

---

<div class="post-metadata">

**Author:** ![LvanderRee](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/lvanderree/32/1853_2.png) [@LvanderRee](https://community.homey.app/u/LvanderRee)\
**Post date:** [November 6, 2022, 7:32pm UTC](https://community.homey.app/t/getting-to-environment-variables-from-my-api-js-code/71791/4 "2022-11-06T19:32:28Z")

</div>

Thanks again, you again saved the day!

Apparently I used the wrong instance…

```auto
const Homey = require('homey');

module.exports = {
    async testLogin({ homey, body }) {

        homey.log('env', homey.env);
// => env {}

        console.log('ENV', Homey.env);
//ENV {
// API_KEY: 'very secret key'
//}

...

```

---

<div class="post-metadata">

**Author:** ![robertklep](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/robertklep/32/160628_2.png) [@robertklep](https://community.homey.app/u/robertklep)\
**Post date:** [November 7, 2022, 6:30am UTC](https://community.homey.app/t/getting-to-environment-variables-from-my-api-js-code/71791/5 "2022-11-07T06:30:43Z")

</div>

> [@LvanderRee](#):
>
> Apparently I used the wrong instance…

Yes, it’s a bit confusing that `env` isn’t (also) part of the Homey instance that gets passed around (and is available as `this.homey` in the app/driver/device instances).
