Getting to environment variables from my api.js code

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?

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:

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

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

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

1 Like

Thanks again, you again saved the day!

Apparently I used the wrong instance…

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'
//}

...

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).