# 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](/docs/user-guides/chatbot/actions/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](/docs/user-guides/chatbot/actions/collect-data) Actions have the same **Destinations** step. A [custom form](/docs/developer-guides/website-integration/client-side-custom-forms) 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. ![The agent's Settings page on the Webhooks tab, with the Webhooks table of Action, Event and URL, and the Helpdesk events section with the New webhook button.](annotated/01-webhooks-settings.webp "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**. ![The New webhook dialog with the Endpoint URL field and the three event checkboxes: A ticket is opened, A ticket is closed and A teammate takes over a conversation.](annotated/02-new-webhook.webp "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. > [!TIP] > > Helpdesk rules can also call these webhooks. In a rule, add the **Send webhook** action and pick the endpoint; QwryAI then sends `helpdesk.trigger` or `helpdesk.automation`. See [Triggers](/docs/user-guides/chatbot/help-desk/triggers) and [Automations](/docs/user-guides/chatbot/help-desk/automations). ## Payload Every request has the same JSON body: | Key | Type | Description | | :- | :- | :- | | **eventType** | string | The [event type](#event-types) | | **chatbotId** | string | The id of the agent that sent it | | **payload** | object | The event's data. [Learn more](#event-payload) | 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](/docs/developer-guides/website-integration/client-side-custom-forms). `{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. > [!NOTE] > > Your endpoint should answer with a `2xx` status within 10 seconds. Any other answer, or no answer, counts as a failure: QwryAI tries again up to 5 times in all, waiting longer each time. Retries carry the same `Idempotency-Key`. > [tab] Next.js > > ```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: '', > payload: { > 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 > ``` > [tab] Node.js > > ```typescript > import 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') > } > ```