# Custom webhook

Send call recordings or transcripts to APX from any phone system or middleware, signed with HMAC-SHA256.

Use a custom webhook when your phone system isn't Twilio, Telnyx or Aircall, or when you already process calls in your own server or a tool like Zapier, Make or n8n. Your system sends each call to a URL APX gives you, as JSON, signed with a secret only you and APX know.

## Create the webhook

1. Go to **Integrations** and select **Add integration**.
2. Pick **Custom webhook** and select **Next**.
3. Under **How will you send call data?**, pick one:
   - **Send transcripts** if your system already has the transcript. APX scores it directly.
   - **Send recordings** if your system has the audio. APX transcribes it, then scores it.
4. Enter a **Name** and select **Create integration**.
5. On **Save these secrets now**, copy the **Webhook URL** and the **Webhook secret (shown once)**. Store the secret in your secret manager.

> **Warning:** APX shows the webhook URL and the secret on this step only. Copy both before you select **Continue**. If you lose the secret, you can make a new one (see [Replace the secret](#replace-the-secret)).

## Send a call

Send one `POST` request per call to your webhook URL, with a JSON body and these headers:

| Header | Value |
|---|---|
| `Content-Type` | `application/json` |
| `X-APX-Timestamp` | The current time in ISO 8601 UTC, like `2026-10-29T15:04:05Z`. Fractional seconds and a `+hh:mm` offset are fine. Requests more than 5 minutes off APX's clock are rejected. |
| `X-APX-Signature` | The HMAC-SHA256 of the timestamp, a dot, then the raw body, keyed with your webhook secret, as a hex string. |

An example body for a **Send transcripts** integration:

```json
{
  "external_call_id": "call-0001",
  "direction": "inbound",
  "from_number": "+15550100001",
  "to_number": "+15550100002",
  "started_at": "2026-10-29T15:00:00Z",
  "ended_at": "2026-10-29T15:05:12Z",
  "duration_seconds": 312,
  "transcript": "Agent: Thanks for calling. Customer: Hi, I have a question about my order.",
  "metadata": { "crm_contact_id": "12345" }
}
```

For a **Send recordings** integration, send `recording_url` instead of `transcript`.

## Payload fields

| Field | Required | Type | Notes |
|---|---|---|---|
| `external_call_id` | Yes | string | Your ID for the call, unique per call. Sending the same ID again doesn't create a second call. |
| `transcript` | One of `transcript` or `recording_url` | string | The full transcript. Speaker labels like `Agent:` are fine. APX keeps the first 50,000 characters. |
| `recording_url` | One of `transcript` or `recording_url` | string | A URL APX can download the audio from without signing in, in a common format such as MP3 or WAV. Use HTTPS. If it's a signed link, let it last at least a few minutes. |
| `duration_seconds` | On **Send transcripts** integrations (see below) | number | The call's talk time in seconds, not including ringing. |
| `started_at` | No | string | When the call started, ISO 8601. |
| `ended_at` | No | string | When the call ended, ISO 8601. |
| `direction` | No | string | `inbound` or `outbound`. Anything else is stored as unknown. |
| `from_number` | No | string | The caller's number. E.164 (`+1555...`) is best. |
| `to_number` | No | string | The number called. |
| `language` | No | string | The transcript's language code. Defaults to `en`. |
| `word_timestamps` | No | array | Word-level timings if you already have them. Each entry needs `word` (a string) and `start_ms` and `end_ms` (numbers, in milliseconds). `speaker` is optional, a string or a number. See below. |
| `recording_mime` | No | string | The audio type, like `audio/mpeg`. APX works it out if you leave it out. |
| `metadata` | No | object | Any extra fields you want kept with the call. |

On a **Send transcripts** integration APX scores the `transcript`, even if you also send a `recording_url`. On a **Send recordings** integration APX downloads `recording_url` and transcribes it in English.

On a **Send transcripts** integration, if any entry in `word_timestamps` is missing a field or has the wrong type, APX drops the timings for that call. The call is still processed, and the call page shows the plain transcript instead of the word-by-word one. APX hides personal details in the words you send the same way it does in the transcript (see [The transcript](/docs/calls/reading-an-analysis#the-transcript)).

### Transcripts need a call length

APX measures usage in call minutes, so every call sent as a transcript has to say how long it was. Send at least one of:

1. `duration_seconds`, as a positive number (not a string).
2. Both `started_at` and `ended_at`, with `ended_at` later. Note that this includes ringing time.
3. `word_timestamps`. APX uses the last word's `end_ms`.

A transcript with none of these is rejected with a `400` (see [Responses](#responses)). If the length you send is shorter than the transcript could possibly be, APX uses the longer length the transcript shows. Calls sent as a recording don't need a length: APX measures the audio.

## Verify the signature

The signature is the HMAC-SHA256, keyed with your webhook secret, of this exact string: the `X-APX-Timestamp` value, a full stop, then the raw request body. Send it as hex in `X-APX-Signature`. Sign the same bytes you send.

A Node.js example (Node 18 or later):

```js
import crypto from "node:crypto";

const WEBHOOK_URL = process.env.APX_WEBHOOK_URL;       // from the setup step
const WEBHOOK_SECRET = process.env.APX_WEBHOOK_SECRET; // from the setup step

async function sendCall(call) {
  const body = JSON.stringify(call);             // stringify once, send these exact bytes
  const timestamp = new Date().toISOString();    // for example 2026-10-29T15:04:05.123Z
  const signature = crypto
    .createHmac("sha256", WEBHOOK_SECRET)
    .update(`${timestamp}.${body}`)
    .digest("hex");

  const res = await fetch(WEBHOOK_URL, {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
      "X-APX-Timestamp": timestamp,
      "X-APX-Signature": signature,
    },
    body,
  });
  if (!res.ok) throw new Error(`APX answered ${res.status}: ${await res.text()}`);
  return res.json();
}

await sendCall({
  external_call_id: "call-0001",
  duration_seconds: 312,
  transcript: "Agent: Thanks for calling. Customer: Hi, I have a question about my order.",
});
```

To check your own signing code, sign this example with these placeholder values:

| Input | Value |
|---|---|
| Secret | `whsec_example_0000000000000000` |
| Timestamp | `2026-10-29T15:04:05Z` |
| Body | `{"external_call_id":"call-0001","duration_seconds":312,"transcript":"Agent: Thanks for calling. Customer: Hi, I have a question about my order."}` |

The signature should be:

```text
3e7a745e63785f9d3dfcc1929ee115164867bca9dc1aa629e68bdfbf8551d72a
```

(APX would reject this exact request because the timestamp is old. Use it only to test your code.)

### Common signing mistakes

- **Signing the body on its own.** Sign the timestamp, a dot and the body, using the same timestamp string you send in the header.
- **Re-encoding the body.** If you build the JSON twice, or your HTTP client reformats it, the bytes change and the signature fails. Sign the exact string you send.
- **A trailing newline** in the body changes the signature.
- **Reusing a signature on a retry.** The timestamp has to be within 5 minutes, so sign each attempt again with a fresh timestamp.
- **Using an old secret.** After you replace the secret, the old one stops working straight away.

## Responses

| Status | Meaning | What to do |
|---|---|---|
| `200` | Accepted. The body is `{"success": true, "data": {"webhook_event_id": "..."}}`. | Nothing. |
| `400` | `MISSING_CALL_DURATION`: a transcript with no call length. | Add a length and send again. |
| `401` | `WEBHOOK_SIGNATURE_INVALID`: the signature or timestamp header is missing or wrong, or the timestamp is more than 5 minutes off. | Fix the signing and send again. |
| `404` | `INTEGRATION_NOT_FOUND`: the URL is wrong, or the integration was deleted. | Check the URL. |
| `409` | `INTEGRATION_PAUSED`: the integration is paused. | Select **Resume** on its page, then send again. |
| `413` | `WEBHOOK_PAYLOAD_TOO_LARGE`: the body is over 512 KB. | Send a `recording_url` rather than embedding large data, or trim `metadata`. |
| `5xx` | A problem on APX's side. | Retry (see below). |

Error responses have the same shape: `{"success": false, "error": {"code": "...", "message": "..."}}`. The message says what went wrong.

APX checks the signature and the call length when the request arrives. It checks the rest a moment later. A body without `external_call_id`, or without either `transcript` or `recording_url`, still gets a `200`, then shows as **Failed** on the **Webhook events** tab with the reason.

## Retries

APX doesn't retry a request your system sent. Retry from your side when you get a `5xx` or no answer at all (a timeout or dropped connection), with increasing gaps between attempts. Sign each retry again with a fresh timestamp.

Retrying is safe: a second request with the same `external_call_id` doesn't create a second call. If APX has already finished with that call, the repeat is ignored. To send a corrected call after it was processed, use a new `external_call_id`.

Don't retry a `400`, `401`, `404` or `413` unchanged: fix the request first.

## Test and monitor

A new custom webhook shows **Pending**. It accepts calls straight away. To check the whole path, open the integration and select **Send test webhook**: APX runs a sample transcript call through your agents, the status changes to **Verified**, and the test call appears in **Calls**. The sample is a transcript, so on a **Send recordings** integration test with a real signed request from your system instead.

The **Webhook events** tab lists the recent requests your system sent, with columns for **Received**, **Status**, **Signature** and **Error**. A request with a bad signature shows **Invalid** in the **Signature** column. APX never processes a request that failed the signature check.

If a request was accepted but then failed (for example APX couldn't download the recording), fix the cause and select **Replay** on that row. APX shows **Replay queued** and runs the same payload again, so it won't fix a payload that was missing something: send a corrected request instead. The **Activity** tab records **Webhook event replayed** and who replayed it.

**Replay** only appears on a **Failed** row whose **Signature** shows **Verified**. A request that failed the signature check could have come from anyone who knows the URL, so it can't be replayed: have your system send it again, signed.

## Replace the secret

If you lose the secret or think it leaked:

1. Open the integration and select **New secret**.
2. Confirm with **Replace secret**.
3. Copy the secret shown on **Your new signing secret** and select **Done**.

The old secret stops working straight away: requests signed with it get a `401` until your system uses the new one. The **Activity** tab records **Signing secret replaced**.

More fixes: [Troubleshooting](/docs/integrations/troubleshooting).
