Webhook for Approval task status changes?

Hello! I’m new to the Asana API, and fairly new to manually creating webhooks…
I am creating approval tasks and hope to trigger a webhook whenever the approval task status is changed (approved, rejected, change requested) in my project (“IAM Tasks”). I would like the webhook to send the gid and due information. (Ideally, it would also send the name/description, but I’d settle for the gid)

Here is the body of my webhook creation request
{
“data”: {
“filters”: [
{
“action”: “changed”,
“fields”: [
“gid”,
“name”,
“due_on”

    ],
    "resource_subtype": "approval",
    "resource_type": "task"
  }
],
"resource": "XXXXXX",
"target": "XXXXX"

}
}

and the response:

{
“response”: {
“status_code”: 201,
“headers”: {
“server”: “nginx”,
“date”: “Fri, 21 May 2021 13:21:38 GMT”,
“content-type”: “application/json; charset=UTF-8”,
“content-length”: “478”,
“connection”: “close”,
“location”: “/api/1.0/webhooks/XXXXX”,
“cache-control”: “no-store”,
“pragma”: “no-cache”,
“set-cookie”: [
“TooBusyRedirectCount=0”
],
“x-frame-options”: “DENY”,
“x-xss-protection”: “1; mode=block”,
“x-content-type-options”: “nosniff”,
“content-security-policy”: “report-uri https://app.asana.com/-/csp_report?report_only=false;default-src ‘none’;frame-src ‘none’;frame-ancestors ‘none’”,
“x-asana-api-version”: “1.0”,
“asana-change”: “name=new_user_task_lists;info=Update on our planned API changes to user task lists (a.k.a. My Tasks)”,
“x-robots-tag”: “none”,
“strict-transport-security”: “max-age=31536000; includeSubDomains”,
“datacenter-time-end”: “1621603298.133”,
“x-loadbalancer”: “prod-lb013.ec2”
},
“body”: {
“data”: {
“gid”: “XXXX”,
“resource_type”: “webhook”,
“resource”: {
“gid”: “XXXX”,
“resource_type”: “project”,
“name”: “IAM Tasks”
},
“target”: “XXXXXXX”,
“active”: true,
“filters”: [
{
“resource_type”: “task”,
“resource_subtype”: “approval”,
“action”: “changed”,
“fields”: [
“gid”,
“name”,
“due_on”
]
}
],
“created_at”: “2021-05-21T13:21:35.534Z”,
“last_failure_at”: null,
“last_failure_content”: “”,
“last_success_at”: “2021-05-21T13:21:37.794Z”
}
}
}
}

The changes only trigger the webhook intermittently (I’ve received one notification out of 3-4 changes to the status of a task in the project), and when I do receive the notifications, they are essentially blank, as seen below:

{
“method”: “post”,
“path”: “/”,
“body”: {
“events”:
},
“query”: {},
“headers”: {
“accept-encoding”: “identity”,
“content-length”: “13”,
“content-type”: “application/json”,
“host”: XXXXX
“user-agent”: “Asana”,
“x-amzn-trace-id”: “Root=1-60a7b4d8-402a26017c38a95100243f8d”,
“x-asana-request-signature”: “XXXXXXX”,
“x-forwarded-for”: “3.239.117.148”,
“x-forwarded-port”: “443”,
“x-forwarded-proto”: “https”,
“x-hook-signature”: “XXXXX”
}
}

Any suggestions would be appreciated!

Hi @Daniel_Wynn and welcome to the forum!

A couple of things to note here:

First… when you have an active webhook, Asana pings it periodically to make sure it’s still alive. It expects to receive a 100 response from these pings, if it doesn’t, it assumes the webhook is dead and it deactivates it (I don’t think that happen after just one failed ping; it takes multiple failures to have it deactivated).

When it sends these pings, they contain no actual events. So when you see periodic webhook messages with no events; i.e.

“body”: {
“events”: []
},

you can figure that’s a ping, and you can ignore it.

So that explains why you see those periodic blank messages. Next question is, why aren’t you getting any actual webhooks?

The answer lies here:

“fields”: [
“gid”,
“name”,
“due_on”
],

It looks like you’re specifying those fields to try and get that data back. Unfortunately that’s not how it works: the fields element within a webhook filter is used to further refine the filter to say “only send back a webhook when these specific fields change value”. That’s why you won’t get any webhooks when you change the approval status - it’s not among your filter fields. So you’ll want to remove those from your filter. You could probably replace them with just the field approval_status so you’ll only get webhooks when that particular field changes.

Next question, I know, is… “so how DO I get those fields’ values back in my webhook?”

Answer: you don’t. Asana’s webhooks (and this is pretty much an industry standard) are lightweight messages that are designed only to tell you that something changed; they don’t tell you what changed. To get that info, you need to do another API call to query the resource that changed and return its data. In your case, you’ll query the task and that’s how you’ll determine what the new value of approval_status is.

Thanks for the help @Phil_Seeman ! I’ll see if I can delete/re-create the webhook accordingly!

Regarding the “You don’t…”. So, to verify, I should EXPECT to see essentially a blank message… there is no way to give any indication of what task was modified.

If that’s the case, the logic will be: I receive a blank trigger, then query the project for all tasks, and then use whatever method necessary on my end to differentiate prior and current state for each task.

Bummer! Are there any other ways around this leveraging some element of the Asana API that might be more effecient? (This project is expected to scale to hundreds of tasks)

No, sorry for confusing you @Daniel_Wynn!

The blank webhooks are just the periodic “ping” ones and you should ignore those completely.

Once you have a valid webhook created that’s returning actual events, you’ll see that it won’t be blank - it will tell you the ID of the task that changed status, and have some other metadata in it. It won’t be empty! When you get these non-empty messages, these are the ones where you’ll then query the specific task whose ID you were told has just changed to get its data.