Webhook with Nuxt

Hi, I am having some trouble with the Webhook. I would like to know when an ordered has been completed and create a function when this has happened. Anyone could please help me? I am using Nuxt and Snipcart. Thanks

app.get('/hook/', async(req, res) =>  {
    if (req.body.eventName === 'order.completed') {
        res.send("SUCCESS")
    }else{
        res.send("ERROR")
    }
})

Hi @Nerea, thanks for reaching out!

Could you share with us a bit more of your setup and what you tried?

Have you deployed this code? Do you get any request in the setup webhook URL after an order is completed, or nothing at all? Can you fake a request with a tool like Postman?

Thanks.

Hi @nelitow,

I am working with Nuxt and my idea is to do something (like send email or change Sanity Backend ) when an ordered is completed.

I have written in my Snipcart Webhooks the URL of the site, https://5963-77-225-46-176.ngrok.io/hook/. But I get always a 404. I am using this code. Something I am missing?
Nuxt.config.js

 serverMiddleware: {
    '/api': '~/api'
 }

Index.js

app.get('/hook/', async(req, res) =>  {
    
    if (req.body.eventName === 'order.completed') {
        res.send("SUCCESS")
    }else{
        res.send("ERROR")
    }
})

export default {
  path: '/api',
  ha

ndler: app
}
index.vue

async fetch() {
    this.hook = await this.$axios.get('/api/hook/', {}).then(res => console.log('e',res))
 }

Hi @Nerea, thank you for the details.

Is your webhook endpoint running on a server, like in a backend or a serverless function?

The webhook endpoint is used by Snipcart to POST the order payload that you can then handle to send an email or update Sanity data, for example.

This endpoint should not be used to GET data by fetching from the client side, like you seem to be doing in your index.vue file.

Could you give us more details of what you’re trying to achieve with your index.vue?

Also, which version of Nuxt you are using?

Thanks.

Hi,

My Webhook endpoint is running on a server.
My question is how can I receive the Post from the Order payload in my Nuxt.js and then handle to send an email? Do you have an Example in JS?

I think i don’t need the index.vue to use the webhook endpoint.

My Nuxt version is “version”: “1.0.0”.

Thanks in advance.