Handling webhooks securely

Receive pingback, postback and Crawl API batch webhooks, verify them by reading the result back with your key, and handle missed deliveries.
Open as Markdown
On this page

Three callbacks can tell you that work is done:

CallbackSet withRequest we send
Pingbackpingback_url on a task_post taskGET to your URL, with $id and $tag replaced
Postbackpostback_url on a task_post taskPOST with the task_get envelope as JSON
Batch webhookwebhook_url on POST /v1/crawls/batchPOST with the finished job envelope as JSON, plus "event": "crawl.batch.completed"

Headers on every callback:

HeaderValue
User-AgentDataForGAIO-Webhook/1.0
X-DataForGAIO-Task-IdThe task id (pingback and postback)
X-DataForGAIO-Eventcrawl.batch.completed (batch webhook)
X-DataForGAIO-Job-Idjob_... (batch webhook)
Content-Typeapplication/json (postback and batch webhook)

Callbacks aren't signed: verify by reading back

We don't sign callback requests yet. Anyone who learns your callback URL could send a request that looks like ours, so treat every callback as a notification, not as data:

  1. Take only the id from the callback (X-DataForGAIO-Task-Id, the $id in your URL, or job_id).
  2. Read the result with your own key: task_get for queued tasks, GET /v1/crawls/{job_id} for batches. Those reads are free, authenticated, and scoped to your organization, so a forged id returns 40401 or 404.
  3. Use what you read back, not the callback body.
Python (Flask)
import os
import requests
from flask import Flask, request
 
app = Flask(__name__)
AUTH = ("api", os.environ["DATAFORGAIO_API_KEY"])
BASE = "https://api.dataforgaio.com/v1"
 
@app.post("/hooks/serp/<secret>")
def serp_postback(secret):
    if secret != os.environ["HOOK_SECRET"]:
        return "", 404
    task_id = request.headers.get("X-DataForGAIO-Task-Id", "")
    if not task_id:
        return "", 400
    # Authoritative copy, read with our key. Free.
    res = requests.get(f"{BASE}/serp/google/organic/task_get/advanced/{task_id}", auth=AUTH, timeout=60)
    task = res.json()["tasks"][0]
    if task["status_code"] == 20000:
        save(task["data"].get("tag"), task["result"])
    return "", 204

Hardening your receiver

  • Put a secret in the path, as above, and reject anything else with 404. It doesn't replace the read-back, but it cuts noise.
  • Use HTTPS. Batch webhooks require it. Pingback and postback accept http too; use https anyway.
  • Answer within 10 seconds. We wait 10 seconds and don't follow redirects. Return 2xx quickly and do the work in the background.
  • Be idempotent. Key your writes by task id or job id so a re-sent callback doesn't duplicate data.
  • Public hosts only. We only call hosts that resolve to public IP addresses. localhost, private ranges and internal names are skipped, so use a tunnel to test locally.

Missed deliveries

We make one attempt per callback and don't retry on failure. Plan for misses:

  • Queued tasks: poll tasks_ready every few minutes as a backstop. It lists completed tasks you haven't collected yet, whether or not a callback reached you.
  • Re-send a callback: POST /v1/account/webhook_resend with {"id": "<task id>"} delivers the pingback and postback again for a finished task. It answers 40602 if the task hasn't finished yet.
  • Batches: poll GET /v1/crawls/{job_id} until job_status is completed.
curl -X POST "https://api.dataforgaio.com/v1/account/webhook_resend" \
  -u "api:$DATAFORGAIO_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"id": "6f1c2a7e-3b0d-4c55-9a51-0e2f8d7b1c44"}'