Skip to content
QwryAI
Documentation
Pages

Advanced Integration

Webhook API Guide

Get a signed request on your server when customers submit leads and forms, phone calls end, tickets open or close, or a teammate takes over

Webhook API Guide

A webhook sends a POST request to your endpoint each time an event happens in your agent, so your own systems can react right away.

Create a webhook

You create a webhook where its event comes from. For lead submissions, use the Collect leads Action:

  1. In your agent, open Actions and open your Collect leads Action.
  2. Go to its Destinations step.
  3. Enter the URL that should receive the POST request, then select Create webhook.

Collect data Actions have the same Destinations step. A custom form has its own Webhooks step, under Send form data to webhook (optional).

For phone call summaries, add the webhook on the Phone channel:

  1. Open the Phone channel of an agent that has a phone number.
  2. In its Connections, find Call summary webhook, enter the URL that should receive the POST request and select Create webhook.

Helpdesk events

Ticket and takeover events are set up in your agent's settings:

  1. Open Settings › Webhooks

    In your agent, open Settings and select the Webhooks tab. The Webhooks table at the top lists every webhook your Actions send to, with a link to each Action.

    Settings › Webhooks
  2. Add the endpoint

    Under Helpdesk events, select New webhook. Enter your Endpoint URL, which must start with https://, and tick the Events you want:

    • A ticket is opened sends ticket.opened.
    • A ticket is closed sends ticket.closed.
    • A teammate takes over a conversation sends conversation.taken_over.

    Select Create webhook.

    New webhook: choose the endpoint and events.
  3. Copy the signing secret

    In the row of your new webhook, select the copy icon (Copy signing secret) and store the secret on your server. You'll use it to check the signature.

Payload

Every request has the same JSON body:

KeyTypeDescription
eventTypestringThe event type
chatbotIdstringThe id of the agent that sent it
payloadobjectThe event's data. Learn more

The request also carries these headers:

HeaderDescription
Content-Typeapplication/json
x-qwryai-signatureThe HMAC-SHA1 of the body, in hex, made with the webhook's secret
Idempotency-KeyThe same value on every retry of one event; use it to skip duplicates

Event types

These are the events webhooks can send:

  • leads.submit: a customer submits their name, email, phone and any custom fields of a Collect leads Action.
  • {action name}_collect_data.submit: a customer submits the fields a Collect data Action asks for. {action name} is that Action's name.
  • {action name}_custom_form.submit: a customer submits a custom form. {action name} is that Action's name.
  • phone_call.summary: a phone call to your agent ends. Sent once per call, shortly after the caller hangs up, with a summary of the call.
  • ticket.opened: a ticket is opened.
  • ticket.closed: a ticket is closed.
  • conversation.taken_over: a teammate takes over a conversation from the AI.
  • helpdesk.trigger and helpdesk.automation: a helpdesk trigger or automation ran a Send webhook action.

Event payload

What each event sends in payload:

  • leads.submit:
JSON
{
  conversationId: string,
  customerEmail: string,
  customerName: string,
  customerPhone: string,
  customFields: object // only when the Collect leads Action has custom fields
}

customFields is keyed by the names of the fields you added to the Action, for example { "Company name": "Acme", "Team size": "12" }.

  • {action name}_collect_data.submit:
JSON
{
  conversationId: string,
  customFields: object // keyed by each collected field
}
  • {action name}_custom_form.submit:
JSON
{
  conversationId: string,
  data: object, // the submitted fields, keyed by field name
  error: string | null // set when an image upload failed
}
  • phone_call.summary:
JSON
{
  conversationId: string,
  callSummary: string, // summary of the call
  duration: number, // call length in seconds
  callerNumber: string, // the caller's number as the carrier sent it
  calledNumber: string // your agent's number that was dialed
}
  • ticket.opened and ticket.closed:
JSON
{
  ticketId: string, // such as "TKT-42"
  conversationId: string,
  subject: string,
  status: string,
  priority: string,
  requester: { name: string | null, email: string | null },
  at: string // ISO 8601 time of the event
}
  • conversation.taken_over:
JSON
{
  conversationId: string,
  agent: { id: string, name: string }, // the teammate who took over
  at: string
}
  • helpdesk.trigger and helpdesk.automation:
JSON
{
  ticketId: string,
  conversationId: string, // when the ticket has a conversation
  rule: { id: string, name: string },
  ticket: object // subject, status, priority, channel, tags, requester, url and more
}

Receiving the request

Read the JSON body like any other request. To make sure a request really came from QwryAI, check the x-qwryai-signature header before you trust it.

Make an HMAC-SHA1 of the body with your webhook's secret and compare it, in hex, with x-qwryai-signature. If they match, the request is from QwryAI.

TypeScript
import crypto from 'crypto'
import { NextApiRequest, NextApiResponse } from 'next'
import getRawBody from 'raw-body'

// The raw body is needed to check the signature.
export const config = {
  api: {
    bodyParser: false,
  },
}

async function webhookHandler(req: NextApiRequest, res: NextApiResponse) {
  if (req.method !== 'POST') {
    res.setHeader('Allow', 'POST')
    return res.status(405).end('Method Not Allowed')
  }

  const { QWRYAI_WEBHOOK_SECRET } = process.env
  if (typeof QWRYAI_WEBHOOK_SECRET !== 'string') {
    throw new Error('No webhook secret found')
  }

  const rawBody = await getRawBody(req)
  const signature = sha1(rawBody, QWRYAI_WEBHOOK_SECRET)

  if (signature !== req.headers['x-qwryai-signature']) {
    return res.status(400).json({ message: "Signature didn't match" })
  }

  const event = JSON.parse(rawBody.toString())
  console.log('Received:', event)

  /*
  Body for a leads.submit event:
  {
    eventType: 'leads.submit',
    chatbotId: '<agentId>',
    payload: {
      conversationId: '<conversationId>',
      customerEmail: 'jane@example.com',
      customerName: 'Jane',
      customerPhone: '+14155550123',
      customFields: { 'Company name': 'Acme' }
    }
  }
  */

  res.status(200).end('OK')
}

function sha1(data: Buffer, secret: string): string {
  return crypto.createHmac('sha1', secret).update(data).digest('hex')
}

export default webhookHandler

Explore the documentation