Skip to the content

Blog

Wait, poll, or be told: three ways to collect a run

A run takes anywhere from a second to several minutes. Here is how to choose between holding the request open, asking again, and a webhook.

API3 min read

By the EnrichedNow team

A scrape is not instant. A page with a public feed can be read in a second or two; a site read page by page takes longer. So starting a run and getting its records are two steps, and there are three ways to get from one to the other.

1. Wait in the same request

Add wait to the request that starts the run: a number of seconds from 0 to 300. The API holds the request open until the run ends or the time is up, whichever comes first.

The run…You get
ended within wait200 and the finished run
is still going when wait runs out202 and the run as it is, queued or running

This is the simplest code to write, and it suits a script or a request handler that needs the answer now. Two things to get right: set your HTTP client's timeout comfortably above wait, and treat a 202 as ordinary. When the wait runs out the run carries on; nothing is lost.

2. Ask again

GET /v1/runs/{id} returns the run as it is now. The classic loop asks every few seconds until the status is final. It works, and it spends most of its requests learning nothing.

The same call takes wait. GET /v1/runs/{id}?wait=60 holds the request open and answers the moment the run ends. One such call replaces a loop of short ones, and you learn the result sooner than a loop with a five-second sleep would tell you.

Start, then follow with one long call at a time
RUN=$(curl -s -X POST "https://api.enrichednow.com/v1/scrapers/careers.company/runs" \
  -H "Authorization: Bearer $ENRICHEDNOW_API_KEY" -H "Content-Type: application/json" \
  -d '{"input": {"domain": "northwind.example"}}' | jq -r .id)

curl -s "https://api.enrichednow.com/v1/runs/$RUN?wait=120" \
  -H "Authorization: Bearer $ENRICHEDNOW_API_KEY" | jq '{status, record_count}'

3. Be told

A webhook turns the question around. You give an https address; when a run ends, a signed POST arrives there with the run in it. Nothing of yours waits, which matters when you start hundreds of runs or when the thing that starts them is not the thing that uses them.

There are two ways to ask for one. An endpoint on the account is told about every run that ends with one of the events it asked for. A webhook_url in the body that starts a run is told about that run only.

Anybody who learns your address can post to it, so check the signature. It is the HMAC-SHA256 of the timestamp, a dot and the body exactly as it arrived:

Python: verify a webhook before trusting it
import hashlib
import hmac
import time


def verify_webhook(secret: str, timestamp: str, body: bytes, signature: str) -> bool:
    """timestamp and signature are the X-Webhook-Timestamp and X-Webhook-Signature headers."""
    if not timestamp.isdigit() or abs(time.time() - int(timestamp)) > 300:
        return False  # older than five minutes: somebody may be replaying it
    mac = hmac.new(secret.encode(), timestamp.encode() + b"." + body, hashlib.sha256)
    return hmac.compare_digest("v1=" + mac.hexdigest(), signature or "")

An event can arrive more than once. Its id, in X-Webhook-Id, is the same on every attempt: keep the ids you have handled and skip a repeat.

Which one

You are writingUse
A script, a notebook, a request handler that needs the answerwait on the request that starts the run
A worker that follows runs it started earlierGET /v1/runs/{id}?wait=…
A pipeline that starts many runs and processes results elsewhereA webhook

The official packages combine the first two: scrape() waits on the first request and follows the run until it ends. The whole picture is in Running a scraper and Webhooks.

Questions

What is the longest a request can wait for a run?
300 seconds, on the request that starts a run and on the request that reads one. A run that takes longer carries on; ask again or use a webhook.
Does a wait that runs out cancel the run?
No. The wait changes only how long the request stays open. The run is the same run either way.
Are webhooks signed?
Yes. Each delivery carries a timestamp and an HMAC-SHA256 signature made with your endpoint's secret.

Try it on a page of yours.

Every account has a free allowance of units each month. A run is charged only when it ends with data you can use.

Keep reading