Upsert Document which comes via Webhook to an API

Hi all,

I’m using webhook event handler in my sync gateway. Whenever a particular doc syncs, it is sent as request to an API. Now I tried to upsert the Doc with some changes in the Doc property. My issue is the doc gets updated, but the same document is synced and is sent as an request to an API where the upsert is done. The cycle go infinite times until I stop my API server.

My aim is to update the document that comes as an API request via webhook, and then store it in the main DB server.

Let me know if there is a way to do it.

This is my webhook config.

"event_handlers":{
                    "document_changed":[
                    {       "handler":"webhook",
                            "url":"http://localhost:5000/checkInvoiceSequence",
                            "filter":` function(doc) {
                                    if(doc.channels){
                                        if(doc.type == "invoice"){
                                            return true;
                                        }
                                    }
                                    return false;
                            }`
                    }
                    ]
            }

I’m trying to upsert the same doc in the API to the main DB server.

TIA.

Are you saying that in response to the webhook call, your external system triggers a subsequent update to the document via Sync Gateway (which then results in a subsequent webhook event)?

From Sync Gateway’s perspective, it’s expected that any update (from whatever source) will trigger a webhook event. To avoid the echo, you’ll need to identify these kind of echos in your application logic. I think that could happen in a couple of ways:

  1. Use the webhook filter to prevent the update being sent (e.g. have your upsert set a property on the document that can be included in your webhook filter), or
  2. Identify the duplicates in the system doing the updates.