# Using synthetics monitor CLI with .env

**URL:** <https://discuss.elastic.co/t/using-synthetics-monitor-cli-with-env/369682>\
**Category:** Synthetics\
**Created:** [October 28, 2024, 3:03pm UTC](https://discuss.elastic.co/t/using-synthetics-monitor-cli-with-env/369682 "2024-10-28T15:03:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cristian\_Pereyra](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cristian_pereyra/32/126779_2.png) [@Cristian\_Pereyra](https://discuss.elastic.co/u/Cristian_Pereyra)\
**Post date:** [October 28, 2024, 3:03pm UTC](https://discuss.elastic.co/t/using-synthetics-monitor-cli-with-env/369682/1 "2024-10-28T15:03:50Z")

</div>

- ECK version: 8.14.0
- FLEET version: 8.14.0
- Synthetics Monitor agent version: 8.14.1
- @elastic/synthetics CLI version: 1.12.1
- Arch: on premise Kubernetes

Hello everyone!  
I currently have a synthetic monitor running with CLI. The stages are working fine for me:

- Create project
- Develop the monitor (in .js and .ts)
- Publish in Kibana

I wanted to set security criteria using a .env file to keep sensitive information

```auto
------
.env
-----
PRIVATE_LOCATIONS=oke-svc-prod
SCHEDULE=5
GOTO=https://www.elastic.co/es/import dotenv from 'dotenv';

------
elastic-env.journey.js
-----
import dotenv from 'dotenv';
dotenv.config({path: path.resolve(__dirname, './.env') });

```

and I encountered two scenarios:

**Scenario 1:**  
Inside monitor.user, I can reference the .env file, and the variables load correctly. I validated this by publishing the changes to Kibana, and the variables loaded fine. The variables I used were PRIVATE\_LOCATIONS and SCHEDULE.  
However, when I tried to use the GOTO variable in `page.goto(process.env.GOTO)` , it failed. Sometimes it loads as empty, and other times it literally takes 'process.env.GOTO' as the value.

```auto
monitor.use({
    id: 'elastic-env',
    name : 'elastic-env',
    tags : ['DOC-DEVOPS'],
    privateLocations: [process.env.PRIVATE_LOCATIONS],
    schedule: parseInt(process.env.SCHEDULE), // Convertir a número
    screenshot: 'on',
    enabled: true
  });
step('Go to https://www.elastic.co/es/', async () => {
    const gotoPage = process.env.GOTO;
    console.log('pageGoto: ', gotoPage);
    await page.goto(gotoPage);
  });

```

**Scenario 2:**  
Due to the error when using GOTO in Scenario 1, I updated my synthetic agent deployment by loading the GOTO environment variable into the pod (I'm using K8s). With this change, the monitor worked.

```auto
elastic-agent@syntetics-agent-test-b95d84546-f7nqg:~$ env | grep GOTO
GOTO=https://www.elastic.co/es/
elastic-agent@syntetics-agent-test-b95d84546-f7nqg:~$

```

![image](https://us1.discourse-cdn.com/elastic/original/3X/4/4/446e329c2d9d525107631407ca4c85e47d97e3eb.png)

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/e/6/e6879617aeb7c66449376e857089b37e6556de4f.png)  
 ![image](https://us1.discourse-cdn.com/elastic/original/3X/2/e/2edb8b8b666ec57c62fe9aeb3ad91d4fb2f77581.png)

_ **My question is:** _  
Is there a way to use an environment variable from a .env file that can be recognized and used when running `npx @elastic/synthetics push`?

---

<div class="post-metadata">

**Author:** ![vigneshshanmugam](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vigneshshanmugam/32/40324_2.png) [@vigneshshanmugam](https://discuss.elastic.co/u/vigneshshanmugam)\
**Post date:** [October 29, 2024, 7:35pm UTC](https://discuss.elastic.co/t/using-synthetics-monitor-cli-with-env/369682/2 "2024-10-29T19:35:46Z")

</div>

Hi @Cristian_Pereyra,

You would need to pass in the ENV values as `params` which can then be consumed in journeys [Configure Synthetics projects | Elastic Observability [8.15] | Elastic](https://www.elastic.co/guide/en/observability/current/synthetics-configuration.html).

```ts
// synthetics.config.ts
export {
   params: {
      GOTO: "https://example.com"
   }
}

// journey.ts
journey('test', ( page, params ) => {
  step('Go to https://www.elastic.co/es/', async () => {
    console.log('pageGoto: ', params.GOTO);
    await page.goto(params.GOTO);
  });
});

```

The reason for this is because the local environment is different from the remote locations where we run these monitors and these env values will not be defined.

Thanks,  
Vignesh

---

<div class="post-metadata">

**Author:** ![Cristian\_Pereyra](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cristian_pereyra/32/126779_2.png) [@Cristian\_Pereyra](https://discuss.elastic.co/u/Cristian_Pereyra)\
**Post date:** [October 29, 2024, 10:53pm UTC](https://discuss.elastic.co/t/using-synthetics-monitor-cli-with-env/369682/3 "2024-10-29T22:53:49Z")

</div>

thanks gor you intressing

i solved that using global.js file. In this solutions i have two advantages

1. The sensitive information cannot seen in Synthetics Kibana
2. i became independent from the environment because the env going to npm push

```auto
----
globals.js
----
const API_URL = 'https://www.elastic.co/es/';
const PRIVATE_LOCATIONS='oke-svc-prod'
const SCHEDULE=10

module.exports = {
  API_URL,
  PRIVATE_LOCATIONS,
  SCHEDULE
};

```

```auto
----
elastic-env.journey.js
----
const globals = require('./globals');
[..]
  console.log ("url: ", globals.API_URL)
[...]
  monitor.use({
    id: 'elastic-env',
    name : 'elastic-env',
    tags : ['DOC-DEVOPS'],
    privateLocations: [globals.PRIVATE_LOCATIONS],
    schedule: parseInt(globals.SCHEDULE), // Convertir a número
    screenshot: 'on',
    enabled: true
  });
[..]

  step('Go to https://www.elastic.co/es/', async () => {
    await page.goto(globals.API_URL);
  });

```

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/c/3/c39258ebd0d7ad9e675fdfbcc46cfe717186db2b.png)
