Webhook triggering a function to run multiple times

I have a route that receives a webhook from this Open Table like service called Tripleseat. Every time one of our team members creates an event there, I create a task on Asana. If they update the event there I update the task.To know if the task already exists or not, I created a table on supabase that stores the tripleseat ID and the Asana task ID. So when the webhook fires up, I check if a task already exists associated with that Tripleseat event ID or not. That determines if it is an update or a create on the Asana side of things.

I found this on Tripleseat’s documentation:

What to do after receiving a Webhook POST
Important: Return a status 200 after successfully receiving a POST from our Webhook service. Failing to do this will result in our system re-sending the same POST several more times (it assumes it has failed without a 200 status response). After enough successive failures, our system will deactivate the Webhook altogether!*

Is there a way I can send the response right away using Next JS?

Thank you in advance!

1 Like

Hey Gus

You can return the response right away and move the processing logic into Next.js’s after() function (which replaces waitUntil(), which you may see suggested elsewhere online)

export async function POST(request) {
  const body = await request.json()

  after(processEvent(body))

  return NextResponse.json({ message: 'Webhook received' }, { status: 200 })
}

This will confirm to Tripleseat that you have received the webhook, and keep the event processing alive until after you finish processing it.

If you don’t use after(), and just avoid awaiting the processEvent function, it may work sometimes, but Vercel’s functions will begin shutting down after they’ve sent their responses, and this may interrupt your async task.

Using after() communicates to Vercel that you want to keep this function alive until it’s finished processing the event

1 Like

Thank you for this, Jacob. I wasn’t aware of the this after() api. Very helpful! I will give it a shot!

1 Like

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