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:
- In your agent, open Actions and open your Collect leads Action.
- Go to its Destinations step.
- Enter the URL that should receive the
POSTrequest, 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:
- Open the Phone channel of an agent that has a phone number.
- In its Connections, find Call summary webhook, enter the URL that should receive the
POSTrequest and select Create webhook.
Helpdesk events
Ticket and takeover events are set up in your agent's settings:
- 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 - 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. - A ticket is opened sends
- 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:
| Key | Type | Description |
|---|---|---|
| eventType | string | The event type |
| chatbotId | string | The id of the agent that sent it |
| payload | object | The event's data. Learn more |
The request also carries these headers:
| Header | Description |
|---|---|
Content-Type | application/json |
x-qwryai-signature | The HMAC-SHA1 of the body, in hex, made with the webhook's secret |
Idempotency-Key | The 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.triggerandhelpdesk.automation: a helpdesk trigger or automation ran a Send webhook action.
Event payload
What each event sends in payload:
leads.submit:
{
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:
{
conversationId: string,
customFields: object // keyed by each collected field
}{action name}_custom_form.submit:
{
conversationId: string,
data: object, // the submitted fields, keyed by field name
error: string | null // set when an image upload failed
}phone_call.summary:
{
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.openedandticket.closed:
{
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:
{
conversationId: string,
agent: { id: string, name: string }, // the teammate who took over
at: string
}helpdesk.triggerandhelpdesk.automation:
{
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.
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 webhookHandlerimport crypto from 'crypto'
import { Request, Response } from 'express'
// This example uses express.json(), so the body arrives parsed.
// QwryAI signs JSON.stringify(body), so signing the parsed body again gives the same result.
export async function webhookHandler(req: Request, res: Response) {
const { QWRYAI_WEBHOOK_SECRET } = process.env
if (typeof QWRYAI_WEBHOOK_SECRET !== 'string') {
throw new Error('No webhook secret found')
}
const rawBody = Buffer.from(JSON.stringify(req.body))
const signature = sha1(rawBody, QWRYAI_WEBHOOK_SECRET)
if (signature !== req.headers['x-qwryai-signature']) {
return res.status(400).json({ message: "Signature didn't match" })
}
console.log('Received:', req.body)
res.status(200).end('OK')
}
function sha1(data: Buffer, secret: string): string {
return crypto.createHmac('sha1', secret).update(data).digest('hex')
}