Square sending multiple webhook Create transactions when item is duplicated

When duplicating an item in the web dashboard, webhook is sending a create transaction microseconds apart. I placed the HTTP 200 response at the top of my code but the response is not fast enough to prevent the second transaction in some cases. See the example below.
Event ID1 : eed530ec-99ed-3074-8796-e9c67ae83d9a
Event ID2 : 4894ae81-ecfa-3271-a285-4b6555c95d0d

:waving_hand: I’m Square Dev AI, here to help answer your question!
Based on the information you’ve provided, the following documentation will help answer your question:

Additional Documentation

Handle Inventory Event Notifications
Use Webhooks to Integrate with a Payroll System
Use Customer Webhooks

If this documentation helped you find a solution, please check the box indicating that this reply solves the problem.
Otherwise one of our Developer Advocates will provide additional assistance shortly.

Hey @DavidE, the catalog.version.updated webhook fires any time any updates are made to the catalog. This can sometimes result in two webhook events being dispatched in close succession, especially when it’s the result of asynchronous server-side operations. In this case, it doesn’t look like these are truly duplicate webhooks. In the payload, I see that the catalog_version.updated_at is a different timestamp, meaning these webhooks are notifications of different updates to the catalog.

It appears that the Create event is first followed shortly after (microseconds later) with an update. When my code calls the Catalog API using the updated_at timestamp from the webhook event, both API calls (from each event) return the same Square item with the same updated_at timestamp. The results is I have two handler processes running on my server processing the same Square record microseconds apart.

My webhook handler is a means to keep my external database synchronized with the items in Square. The problem I am having then is this behavior is creating duplicate records in my database because the events are too close together when checking for existence before creating records.

This behavior in Square has now become my problem I guess. I either need to serialize the event processing through queuing or catch a duplicate record error when inserting the record in the database.

David

To resolve this issue, I created a table in my database that stores the Square Item ID and updated_at timestamp. When the webhook event is received, a record is created in the database. If the INSERT is successful, then my program processes. If it fails because of existing record in the table, the program doesn’t process it.

Thanks for your help.

Great, thanks for sharing your solution @DavidE!