# Webhook action in watcher to use environment variables

**URL:** <https://discuss.elastic.co/t/webhook-action-in-watcher-to-use-environment-variables/148790>\
**Category:** Elasticsearch\
**Tags:** elastic-stack-alerting\
**Created:** [September 17, 2018, 8:50am UTC](https://discuss.elastic.co/t/webhook-action-in-watcher-to-use-environment-variables/148790 "2018-09-17T08:50:18Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![vinodkannan](https://avatars.discourse-cdn.com/v4/letter/v/839c29/32.png) [@vinodkannan](https://discuss.elastic.co/u/vinodkannan)\
**Post date:** [September 17, 2018, 8:50am UTC](https://discuss.elastic.co/t/webhook-action-in-watcher-to-use-environment-variables/148790/1 "2018-09-17T08:50:18Z")

</div>

Hi,

This is the webhook action I am using:

"actions" : {  
"web-post" : {  
"webhook": {  
"method" : "POST",  
"host": "x.x.x.x",  
"port": 8090,  
"path": ":/notification",  
"headers": {  
"Content-Type": "application/json"  
},  
"body": "{{#toJson}}ctx.payload.hits{{/toJson}}"  
}  
}  
}

While the same code needs to be used in different environments, how can I make the host and port parameterized and use environment variables. something like " host =\> [${HOST}] " in logstash.

Thanks  
Vinod

---

<div class="post-metadata">

**Author:** ![elastock](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/elastock/32/35672_2.png) [@elastock](https://discuss.elastic.co/u/elastock)\
**Post date:** [September 19, 2018, 8:28am UTC](https://discuss.elastic.co/t/webhook-action-in-watcher-to-use-environment-variables/148790/2 "2018-09-19T08:28:47Z")

</div>

If you want to re-use the same code in many differents watch you have to use stored sripts fields

```
    POST _scripts/dat_super_script
        {
          "script": {
            "lang": "painless",
            "code": """def my_data = ["DATA1","DATA2","DATA3"]; ctx.vars.my_data=my_data;
     --- do whatever you want there---
        """
            }
        }

```

This script will create an array and add this array to the execution context and you can use it as `condition` validator or `transform` or just logging action , to just add data to the execution context

```
  "email_administrator": {
  "condition": {
    "script": {
      "stored": "dat_super_script"
    }
  },

```

you can access to this data by this variable : `ctx.vars.my_data`  
for example :

```
"actions": {
    "web-post": {
        "webhook": {
            "method": "POST",
            "host": {{ctx.vars.my_data.0}},
            "port": 8090,
            "path": ":/notification",
            "headers": {
                "Content-Type": "application/json"
            },
            "body": "{{#toJson}}ctx.payload.hits{{/toJson}}"
        }
    }
}
```

---

<div class="post-metadata">

**Author:** ![vinodkannan](https://avatars.discourse-cdn.com/v4/letter/v/839c29/32.png) [@vinodkannan](https://discuss.elastic.co/u/vinodkannan)\
**Post date:** [September 20, 2018, 12:31am UTC](https://discuss.elastic.co/t/webhook-action-in-watcher-to-use-environment-variables/148790/3 "2018-09-20T00:31:46Z")

</div>

Thank you Guillaume, Let me try your suggestion.

Thanks  
VInod

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [September 20, 2018, 11:38am UTC](https://discuss.elastic.co/t/webhook-action-in-watcher-to-use-environment-variables/148790/4 "2018-09-20T11:38:52Z")

</div>

Hey,

another possibility is to reuse the same watches (same input, same action ,same condition), but change the `metadata` field and access those. This way you could have scripts creating the watches on your deployment mechanism.

--Alex

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [October 18, 2018, 11:38am UTC](https://discuss.elastic.co/t/webhook-action-in-watcher-to-use-environment-variables/148790/5 "2018-10-18T11:38:56Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
