# Change Contacts for Notifications with API

**URL:** <https://community.icinga.com/t/change-contacts-for-notifications-with-api/14963>\
**Category:** Icinga 2\
**Tags:** notifications, api, icinga2\
**Created:** [June 27, 2025, 5:19am UTC](https://community.icinga.com/t/change-contacts-for-notifications-with-api/14963 "2025-06-27T05:19:15Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![dcz01](https://community.icinga.com/letter_avatar_proxy/v4/letter/d/9f8e36/32.png) [@dcz01](https://community.icinga.com/u/dcz01)\
**Post date:** [June 27, 2025, 5:19am UTC](https://community.icinga.com/t/change-contacts-for-notifications-with-api/14963/1 "2025-06-27T05:19:15Z")

</div>

Hi there,

I’m trying to change or add an contact to an host for notification but i’m not able to do so.  
Maybe my API call is wrong or it isn’t possible at all?

Here i already changed those Parameters via API successfully

 ![image](https://community.icinga.com/uploads/default/original/2X/5/5c67f6ce63239765909e54ab703bb89b39be4491.png)  
via

```auto
curl -k -s -S -i -u user:password -H 'Accept: application/json' \
 -X POST 'https://localhost:5665/v1/objects/hosts' \
 -d '{ "attrs": { "vars.ping_wrta" : "200", "vars.ping_wpl" : "10", "vars.ping_crta" : "300", "vars.ping_cpl" : "20" }, "pretty": true }'

curl -k -s -S -i -u user:password -H 'Accept: application/json' \
 -X POST 'https://localhost:5665/v1/objects/hosts' \
 -d '{ "templates": ["generic-host"], "attrs": { "vars.ping_wrta" : "200", "vars.ping_wpl" : "10", "vars.ping_crta" : "300", "vars.ping_cpl" : "20" }, "pretty": true }'

```

But now trying to add here additional OpsGenie

 ![image](https://community.icinga.com/uploads/default/original/2X/d/d45aea5c710712552cec05a1a849007e13d8e734.png)  
via

```auto
curl -k -s -S -i -u user:password -H 'Accept: application/json' \
 -X POST 'https://localhost:5665/v1/objects/hosts/hostname' \
 -d '{ "attrs": {
		"contacts" : ["datacenter", "opsgenie", "sound"],
		"contact_groups": ["datacenter"]
	},
	"pretty": true }'

curl -k -s -S -i -u user:password -H 'Accept: application/json' \
 -X POST 'https://localhost:5665/v1/objects/hosts/hostname' \
 -d '{ "templates": ["generic-host"], "attrs": { "vars.notifications_opsgenie": "true" }, "pretty": true }'

curl -k -s -S -i -u user:password -H 'Accept: application/json' \
 -X POST 'https://localhost:5665/v1/objects/hosts/hostname' \
 -d '{ "attrs": { "vars.notifications_opsgenie" : "true" }, "pretty": true }'

```

Here are the Variables used and we’re using the three Notification Variables to manage the same notifications on the services of the host.

 ![image](https://community.icinga.com/uploads/default/original/2X/2/21a5084a3650c560f30da4656f379e4c5a9eaa4b.png)

Regards  
dcz01

---

<div class="post-metadata">

**Author:** ![rivad](https://community.icinga.com/user_avatar/community.icinga.com/rivad/32/5852_2.png) [@rivad](https://community.icinga.com/u/rivad)\
**Post date:** [June 27, 2025, 9:03am UTC](https://community.icinga.com/t/change-contacts-for-notifications-with-api/14963/2 "2025-06-27T09:03:37Z")

</div>

I guess, you try to manipulate the wrong object.

[Object Types - Host](https://icinga.com/docs/icinga-2/latest/doc/09-object-types/#host)  
vs  
[Object Types - Notification](https://icinga.com/docs/icinga-2/latest/doc/09-object-types/#notification)

---

<div class="post-metadata">

**Author:** ![jeanm](https://community.icinga.com/letter_avatar_proxy/v4/letter/j/6a8cbe/32.png) [@jeanm](https://community.icinga.com/u/jeanm)\
**Post date:** [June 27, 2025, 9:26am UTC](https://community.icinga.com/t/change-contacts-for-notifications-with-api/14963/3 "2025-06-27T09:26:48Z")

</div>

> [@dcz01](#):
>
> `"attrs": { "vars.notifications_opsgenie" : "true" }`

I believe the `true` value ought to be specified without the surrounding quotes, if `notifications_opsgenie` is defined as a boolean variable.

Could you share the full output of the failing `curl` command, even if the above solves the issue? It will help future searches for the same issue 😉

---

<div class="post-metadata">

**Author:** ![dcz01](https://community.icinga.com/letter_avatar_proxy/v4/letter/d/9f8e36/32.png) [@dcz01](https://community.icinga.com/u/dcz01)\
**Post date:** [June 27, 2025, 9:36am UTC](https://community.icinga.com/t/change-contacts-for-notifications-with-api/14963/4 "2025-06-27T09:36:51Z")

</div>

I tried it without the quotes but then i’ll get the same like with:

```auto
HTTP/1.1 200 OK
Server: Icinga/r2.14.3-1
Content-Type: application/json
Content-Length: 188

{
    "results": [
        {
            "code": 200,
            "name": "hostname",
            "status": "Attributes updated.",
            "type": "Host"
        }
    ]
}

```

and it doesn’t solve the problem ☹

But here are the applys for the Notifications (if they help):

```auto
apply Notification "notify-opsgenie-host" to Host {
  command = "opsgenie-host-notification"
  users = ["opsgenie"]
  states = [Down, Up]
  types = [Problem, Recovery, Acknowledgement]

  # assign rules
  assign where host
  assign where host.vars.notifications_opsgenie == "true"
  ignore where host.vars.notifications_opsgenie == "false"

apply Notification "notify-opsgenie-service" to Service {
  command = "opsgenie-service-notification"
  users = ["opsgenie"]
  states = [Critical, OK]
  types = [Problem, Recovery, Acknowledgement]

  # assign rules
  assign where service.vars.notifications_opsgenie == "true" && service.enable_notifications == true
  ignore where host.vars.notifications_opsgenie == "false"

```

---

<div class="post-metadata">

**Author:** ![jeanm](https://community.icinga.com/letter_avatar_proxy/v4/letter/j/6a8cbe/32.png) [@jeanm](https://community.icinga.com/u/jeanm)\
**Post date:** [June 27, 2025, 10:15am UTC](https://community.icinga.com/t/change-contacts-for-notifications-with-api/14963/5 "2025-06-27T10:15:29Z")

</div>

So I understand the API call _is_ working 🙂

> [@dcz01](#):
>
> `where service.vars.notifications_opsgenie == "true" && service.enable_notifications == true`

This should read `where service.vars.notifications_opsgenie && service.enable_notifications`

If using Director, use the operator `is true (or set)` instead of the operator `==`.

---

<div class="post-metadata">

**Author:** ![dcz01](https://community.icinga.com/letter_avatar_proxy/v4/letter/d/9f8e36/32.png) [@dcz01](https://community.icinga.com/u/dcz01)\
**Post date:** [June 27, 2025, 10:37am UTC](https://community.icinga.com/t/change-contacts-for-notifications-with-api/14963/6 "2025-06-27T10:37:32Z")

</div>

Thanks for that clearification.  
But we’re not using the Director and only managing all files directly via DSL.  
We create new hosts via Ansible over the API and then we’ll using these variables exactly like

```auto
notifications_opsgenie == "true"

```

---

<div class="post-metadata">

**Author:** ![apenning](https://community.icinga.com/user_avatar/community.icinga.com/apenning/32/7669_2.png) [@apenning](https://community.icinga.com/u/apenning)\
**Post date:** [June 30, 2025, 7:33am UTC](https://community.icinga.com/t/change-contacts-for-notifications-with-api/14963/7 "2025-06-30T07:33:52Z")

</div>

Thanks for posting. While your API query looks valid, the issue is the missing evaluation of the `apply` rule when modifying objects through the API. The [documentation](https://icinga.com/docs/icinga-2/latest/doc/12-icinga2-api/#modifying-objects) states:

> Modified attributes do not trigger a re-evaluation of existing static [apply rules](https://icinga.com/docs/icinga-2/latest/doc/03-monitoring-basics/#using-apply) and [group assignments](https://icinga.com/docs/icinga-2/latest/doc/03-monitoring-basics/#group-assign-intro). Delete and re-create the objects if you require such changes or consider funding [this feature request](https://github.com/Icinga/icinga2/issues/4084).

Thus, please consider re-creating the object.

---

<div class="post-metadata">

**Author:** ![rivad](https://community.icinga.com/user_avatar/community.icinga.com/rivad/32/5852_2.png) [@rivad](https://community.icinga.com/u/rivad)\
**Post date:** [June 30, 2025, 8:14am UTC](https://community.icinga.com/t/change-contacts-for-notifications-with-api/14963/8 "2025-06-30T08:14:30Z")

</div>

Re-evaluation would be such a force multiplier.  
I was thinking about automatically detecting stuff like network dependencies and discussing with @moreamazingnick about it and just got reminded by reading about it in your link. [Proposal to add the feature of gathering facts from an agent [auto discovery] · Issue #9204 · Icinga/icinga2 · GitHub](https://github.com/Icinga/icinga2/issues/9204)
