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.
On this page
Three callbacks can tell you that work is done:
| Callback | Set with | Request we send |
|---|---|---|
| Pingback | pingback_url on a task_post task | GET to your URL, with $id and $tag replaced |
| Postback | postback_url on a task_post task | POST with the task_get envelope as JSON |
| Batch webhook | webhook_url on POST /v1/crawls/batch | POST with the finished job envelope as JSON, plus "event": "crawl.batch.completed" |
Headers on every callback:
| Header | Value |
|---|---|
User-Agent | DataForGAIO-Webhook/1.0 |
X-DataForGAIO-Task-Id | The task id (pingback and postback) |
X-DataForGAIO-Event | crawl.batch.completed (batch webhook) |
X-DataForGAIO-Job-Id | job_... (batch webhook) |
Content-Type | application/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:
- Take only the id from the callback (
X-DataForGAIO-Task-Id, the$idin your URL, orjob_id). - Read the result with your own key:
task_getfor queued tasks,GET /v1/crawls/{job_id}for batches. Those reads are free, authenticated, and scoped to your organization, so a forged id returns40401or 404. - Use what you read back, not the callback body.
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 "", 204import express from "express";
const app = express();
const BASE = "https://api.dataforgaio.com/v1";
app.post("/hooks/crawls/:secret", express.json({ limit: "20mb" }), async (req, res) => {
if (req.params.secret !== process.env.HOOK_SECRET) return res.sendStatus(404);
const jobId = req.get("X-DataForGAIO-Job-Id");
if (!jobId) return res.sendStatus(400);
res.sendStatus(204); // answer fast, then read back
// The Crawl API takes Bearer or X-API-Key, not Basic.
const r = await fetch(`${BASE}/crawls/${jobId}`, {
headers: { Authorization: `Bearer ${process.env.DATAFORGAIO_API_KEY}` },
});
const job = await r.json();
if (job.job_status === "completed") await save(job.tasks);
});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
httptoo; usehttpsanyway. - 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_readyevery 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_resendwith{"id": "<task id>"}delivers the pingback and postback again for a finished task. It answers40602if the task hasn't finished yet. - Batches: poll
GET /v1/crawls/{job_id}untiljob_statusiscompleted.
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"}'