apidata.one docs Get a key

Webhooks, step by step

A webhook is how the API tells your server that something happened: a saved search has new matches, a favorite changed, a listing was removed. This guide sets one up from nothing. Webhooks and notices has every rule in full, and the functions that check a signature in Node.js, Python and PHP.

Not ready for a public address? Skip all of this and read the same notices from GET /v1/events once a minute. You can add a webhook later; the notices are the same.

1. Write the endpoint

An endpoint is an https:// address on your server that takes a POST. It has four duties, in this order:

  1. A ping (the header ApiData-Event-Type: ping): answer 2xx, whatever its signature, and do nothing else.
  2. No secret stored yet: answer 503 to everything else. It comes again later, so nothing is lost.
  3. Check the signature of everything else with the endpoint's secret, on the body exactly as received, and answer 400 if it fails.
  4. Answer a good notice 2xx within 10 seconds, then do the work. Put the notice on a queue first; fetching listings or sending email before you answer is the usual cause of timeouts.
import express from 'express';
import { verifyWebhook } from './verify.mjs'; // from the docs: the function is complete as given

const app = express();
app.post('/webhooks/apidata', express.raw({ type: 'application/json' }), (req, res) => {
  const secret = process.env.APIDATA_WEBHOOK_SECRET; // this endpoint's own secret, once stored
  const verified = Boolean(secret) && verifyWebhook(secret, req.get('ApiData-Signature'), req.body);
  if (req.get('ApiData-Event-Type') === 'ping') return res.status(204).end();
  if (!secret) return res.status(503).end();
  if (!verified) return res.status(400).end();
  const notice = JSON.parse(req.body.toString('utf8'));
  queue.add(notice); // answer first, work afterwards
  res.status(204).end();
});

Take the body raw, before any JSON parsing: the signature is over its exact bytes. verify.mjs, verify.py and verify.php are ready to copy, with test cases to run your own code against.

2. Deploy it, then register it

Registration sends a ping to your address and waits for a 2xx, so the endpoint must be running first. From your server, with the secret key:

curl -X POST "https://api.apidata.one/v1/webhook-endpoints" \
  -H "Authorization: Bearer $SK_TEST" -H "Content-Type: application/json" \
  -d '{"url":"https://example.com/webhooks/apidata","eventTypes":["saved_search.matches","favorite.listing_updated","favorite.listing_removed","listing.removed"],"description":"My site"}'

3. Store the secret, at once

The answer holds the endpoint and its secret:

{ "data": { "id": "wh_…", "url": "https://example.com/webhooks/apidata", "eventTypes": ["…"], "enabled": true, "createdAt": "…", "secret": "whsec_…" } }

secret is shown this once. Put it where your endpoint reads it (APIDATA_WEBHOOK_SECRET above), like a password. Deliveries start as soon as the endpoint is saved; until the secret is in place your endpoint answers 503 (rule 2) and those notices come again.

If you lose the secret, delete the endpoint and register it again.

4. Check that it works

Send a test ping, signed with the secret you now hold, and look at what became of it:

curl -X POST -H "Authorization: Bearer $SK_TEST" "https://api.apidata.one/v1/webhook-endpoints/wh_…/test"
curl -H "Authorization: Bearer $SK_TEST" "https://api.apidata.one/v1/webhook-endpoints/wh_…/deliveries"

The delivery list shows each attempt's result: http_status with your server's status code, timeout, tls_failed, connection_failed, redirect_not_followed or refused_destination. Log in your endpoint whether the ping verified: a test ping that does not verify means the stored secret is wrong.

On the sandbox, make a real notice on demand with a trigger.

5. Act on each type

A notice names listings by key and says what kind of change it was, never more. Fetch what you need from the API when you act.

Type What your site does
saved_search.matches Fetch the listings and tell that visitor (saved searches and alerts)
favorite.listing_updated The same, for a favorite that changed
favorite.listing_removed Delete every copy of those listings; the favorites are gone
listing.removed Delete every copy of those listings (a stored page, a row, an image), and answer 410 Gone for their pages
account.suspended A record that the account was suspended, and when. Nothing to do when it arrives (see below)
account.reinstated Live access is back. Re-check every listing you stored: notices more than 24 hours old were dropped, removals among them
data.stale started: listing routes answer 500 data_stale until ended. Show "temporarily unavailable", never stored copies

A suspension is learned from the API's answers, not from a webhook. While an account is suspended nothing is sent to its endpoints, and data routes answer 403 account_suspended: show "temporarily unavailable" and send no alerts when you see it. account.suspended is recorded whenever the account is suspended, whoever did it (a payment that failed for 7 days, a subscription that ended, Bright's list of members, or apidata.one), and it reaches you with the other notices that are still good once the account is reinstated. account.reinstated is recorded only when live access really comes back (going live).

6. Keep it healthy

Building on your own computer

Your computer has no public https:// address, so a webhook can't reach it. Read GET /v1/events while you build, or put a tunnel in front of your local server and register the tunnel's address with a test key. Delete that endpoint when you are done.

This page as Markdown