# Webhooks

> Receive listing events, verify signatures and handle repeated deliveries.

Source: https://platform.kite.sa/docs/guides/webhooks

Rather than polling for new listings, have them posted to you. Subscribe an HTTPS URL to the events you want, with the filters that decide which listings count:

```bash
curl -X POST "https://api.kite.sa/v1/organization/webhooks" \
  -H "Authorization: Bearer $KITE_ADMIN_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "url": "https://example.com/kite-webhook",
    "event_types": ["listing.created"],
    "filters": { "city": "riyadh", "listing_type": "rent", "beds_min": "3" }
  }'
```

The response carries the signing secret once. Store it; you cannot read it back.

Your URL must be `https://`, on the default port, and resolve to a public address — we check that when you register it and again before every delivery, and we don't follow redirects. A receiver that moves needs updating here rather than forwarding.

Filters use the same vocabulary as `GET /listings`, and **a city is required** — without one the subscription is "every new listing in the Kingdom", and neither of us finds that out until the delivery volume arrives.

## What you receive

```json
{
  "id": "evt_9f2c4a1b8e7d6c5b4a3f2e1d",
  "type": "listing.created",
  "created": "2026-09-21T10:00:00.000Z",
  "data": {
    "listing_id": 128647,
    "city": "Riyadh",
    "neighborhood_en": "Al Malqa",
    "neighborhood_ar": "الملقا",
    "listing_type": "rent",
    "listing_category": "residential",
    "property_type": "apartment",
    "price_yearly_sar": 60000,
    "price_as_posted_sar": 5000,
    "rent_frequency": "monthly",
    "bedrooms": 3,
    "bathrooms": 2,
    "area_sqm": 145,
    "lat": 24.8105,
    "lng": 46.6127,
    "url": "https://kite.sa/en/listing/128647"
  }
}
```

The listing as it stands when the event is built: enough to route or filter it without a call back. Not the description, photos or contact details: they would be stale by the time a retry landed hours later, and the 30-day storage limit applies to anything you keep. Fetch `GET /listings/{id}` when you need them.

`listing.updated` adds what changed:

```json
"changed_fields": ["bedrooms", "description"],
"previous_attributes": { "bedrooms": 2 }
```

`changed_fields` names every field that changed, `previous_attributes` the old value of each that has one (`description`, `location` and `neighborhood` are named without one). The rest of `data` is the new state.

Headers: `Kite-Signature`, `Kite-Event-Id`, `Kite-Event-Type` and `Kite-Delivery-Attempt`. Each is also sent under its old name (`Darak-Signature`, `Darak-Event-Id`, `Darak-Event-Type`, `Darak-Delivery-Attempt`) with the same value, so existing receivers keep working. The `Darak-*` names are deprecated: read the `Kite-*` ones.

Every event type's payload and headers are in the API reference, under **Webhooks**, with a schema for each. The SDKs verify and parse a delivery in one call: `parseWebhookEvent` from `@kite-sa/sdk/webhooks`, or `parse_event` from `kite_sa.webhooks`, returns the event typed by its `type`.

## Verifying a delivery

Anyone can POST to your URL. Check the signature before you trust the body:

```ts
import { createHmac, timingSafeEqual } from "node:crypto";

export function verify(header: string, body: string, secret: string): boolean {
  const pairs = header.split(",").map((p) => p.trim().split("=", 2) as [string, string]);
  const t = Number(pairs.find(([k]) => k === "t")?.[1]);
  // Several v1 entries while a secret rotation overlaps: any one may match.
  const v1s = pairs.filter(([k]) => k === "v1").map(([, v]) => v);
  if (v1s.length === 0 || !Number.isFinite(t)) return false;
  // Reject anything older than five minutes: the timestamp is inside what was
  // signed, so this is what stops a captured request being replayed later.
  if (Math.abs(Math.floor(Date.now() / 1000) - t) > 300) return false;

  const expected = createHmac("sha256", secret).update(`${t}.${body}`).digest("hex");
  const a = Buffer.from(expected);
  return v1s.some((v1) => {
    const b = Buffer.from(v1);
    return a.length === b.length && timingSafeEqual(a, b);
  });
}
```

Sign the **raw body**, before any JSON parsing — a re-serialised object will not match.

## Delivery, retries and failure

- **At least once.** A delivery that times out after you committed will arrive again. Deduplicate on `Kite-Event-Id`, which is stable across retries and replays, and is the same for every endpoint one change is delivered to.
- **No ordering.** Events can arrive out of order, and a retry can land after a later event. Order by `created` and the listing's state rather than by arrival.
- **Answer 2xx to accept.** Anything else is retried, including a `4xx` — a 404 usually means a deploy is in flight, not that the event is unwanted, and dropping it would lose data you can't ask for again.
- **Six attempts** over about eight hours: after a minute, then five, thirty, two hours and six. Due deliveries are sent every minute, so each attempt goes out within a minute of its delay. New events are found on an hourly run, so a first delivery can come up to an hour after the change. Answer quickly and do the work afterwards; a receiver that holds the connection open is a receiver we time out at ten seconds.
- **Repeated failure disables the endpoint.** Twenty deliveries that exhaust their attempts and we stop and tell you, rather than keep posting at something that has been gone for days. Deliveries are kept either way — fix the receiver, call `POST /organization/webhooks/{id}/enable` (it resets the failure count, and events that matched while it was off are queued from where it stopped), then replay the failed ones.

## Limits

At most 500 events per endpoint per run. Beyond that the rest wait for the next run rather than being dropped, so a busy subscription catches up instead of losing events.

Events come from what Kite can see, which means a listing that appeared and disappeared between two scrapes was never there to tell you about. Most new listings are picked up hourly; price changes, removals and the smaller sources are read twice a day.

Five types deliver: `listing.created`, `listing.price_changed`, `listing.updated`, `listing.delisted` and `listing.relisted`. Each is tracked separately, so subscribing to a new one starts it from your endpoint's creation rather than from wherever the others had got to.

`listing.updated` fires when the advertiser edits the ad: its title, description, bedrooms, bathrooms, living rooms, areas, floor, furnishing, age, property or listing type, category, neighbourhood or location. Photo changes don't send it yet, nor do amenity flags (parking, kitchen…), which some sources drop and restore between reads. Price changes are `listing.price_changed`, not this. Kite's own corrections to a listing (a re-read neighbourhood, a fixed floor) send nothing. Edits reach Kite when it next reads the ad's full page, which for the two largest sources is mostly when the ad is new, re-priced or relisted, so an edit can arrive days after it was made.

`listing.relisted` fires when a listing that was delisted is back, under the same `listing_id`. It is the pair to `listing.delisted`: a listing that comes back never produces a second `listing.created`.

`listing.price_changed` fires only when the number actually moved — a listing being priced for the first time isn't a change, and has nothing to put in `previous_price_yearly_sar`.

`listing.delisted` covers only listings you could have seen. A listing Kite hides — a duplicate of one it already shows, or one without photos that isn't land — never produced a `listing.created`, so its disappearance produces nothing either. Roughly a fifth of daily deactivations fall into that group.

## Managing them

The **Webhooks** page in the dashboard does all of this without code: add an endpoint (the secret is shown once), send a test event and see what your server answered, read each delivery with its payload, replay one, rotate the secret, re-enable or delete. Owners and admins make changes; every member can see them.

To check a receiver, `POST /organization/webhooks/{id}/test` (or the dashboard's **Send test event**) sends one event now, in the shape of the `event_type` you pick, and returns the answer. Its payload is a sample listing, marked `"test": true` with an `evt_test_…` id; it is never retried and never counts toward disabling the endpoint.

`GET /organization/webhooks` lists your endpoints; `…/deliveries` shows what was sent and what your server said, including the first 500 characters of its reply; `…/deliveries/{id}/replay` sends one again once you've fixed whatever rejected it. `…/deliveries` pages with `next_cursor`/`cursor` like the request log. `…/update` changes the URL, event types, filters or description in place (history and the signing secret are kept; an event type you add starts from now, not from when the endpoint was created); `…/enable` turns a disabled endpoint back on.

To rotate a signing secret, call `…/rotate-secret`: the new secret is in the response once. For `overlap_seconds` (default one day, up to seven; 0 after a leak) each delivery's `Kite-Signature` (and its `Darak-Signature` copy) carries two `v1=` entries, new secret first, so a receiver holding either one verifies. Accept the header if any `v1` matches: the new secret's entry comes first, so a parser that reads only the last `v1` gets the previous secret's signature until the overlap ends. All of these take an admin key (`dk_admin_…`).
