I have 24 public actors on the Apify Store. When one of them fails for a user, the Store counts it against the actor's success rate, and I want to know why it failed. Apify doesn't show an actor's owner anything about runs started by other users: no run object, no log, no input. There's only a 30-day count of runs per status.
So I set up webhooks that write every failed, timed-out or aborted run into one dataset. The setup works and I still use it. It also turned out to have a hard limit that I should have tested on day one. Both parts below.
The setup
One webhook per actor, all pointing at the same dataset. Apify webhooks can POST to any URL, and the dataset "push items" endpoint is a URL, so no server is needed:
body = { "eventTypes": ["ACTOR.RUN.FAILED", "ACTOR.RUN.TIMED_OUT", "ACTOR.RUN.ABORTED"], "condition": {"actorId": actor_id}, "requestUrl": f"https://api.apify.com/v2/datasets/{DATASET_ID}/items?token={TOKEN}", "payloadTemplate": TEMPLATE, "shouldInterpolateStrings": True, } The payload template decides what a row looks like. I keep it flat so the dataset is readable as a table:
{ "ts": {{createdAt}}, "event": {{eventType}}, "actorId": {{eventData.actorId}}, "runId": {{eventData.actorRunId}}, "status": {{resource.status}}, "statusMessage": {{resource.statusMessage}}, "exitCode": {{resource.exitCode}}, "durationMillis": {{resource.stats.durationMillis}}, "memoryMbytes": {{resource.options.memoryMbytes}}, "timeoutSecs": {{resource.options.timeoutSecs}}, "maxTotalChargeUsd": {{resource.options.maxTotalChargeUsd}}, "buildNumber": {{resource.buildNumber}} } The script lists existing webhooks first and updates the one for each actor instead of creating a second, so I can re-run it whenever I add an actor. Note that the token ends up in the stored webhook URL.
maxTotalChargeUsd and timeoutSecs are in there on purpose. A lot of "failures" were runs that hit the user's max cost or timeout, and those need a different fix than a crash.
The status message is the useful field
A row that says FAILED with exit code 1 tells you nothing. Every actor now ends a failed run with a readable status message (for example: No advertiser name given. Enter at least one name in 'Advertiser names', for example Nike.). That message is also the only thing the user sees in their run.
In Apify SDK 3.7.2, Actor.exit({ exitCode: 1, statusMessage }) sends the message and exits about 1 ms later without waiting for the request. On roughly 1 in 5 failed runs the message never arrived, so the row had an empty statusMessage. Setting the message with Actor.setStatusMessage(msg, { isStatusMessageTerminal: true }) and awaiting it before Actor.exit fixed that. I also catch both unhandledRejection and uncaughtException, because a crash inside an imported module and one in top-level code arrive through different events, and either can otherwise end the run without a message.
What it can't see
A few days after setting it up I compared the dataset with the actor's public stats. In the same period the webhook had fired once, for my own test run, while the platform counted 15 failed runs from other users.
Actor-level webhooks fire for runs in your own account. Runs that other users start from the Store happen in their accounts, and your webhook doesn't fire for those. Looking at the dataset now: 56 rows since late September, and every one of them is from my own account. Meanwhile the public counter of one actor went past 700 failed runs in three days in October.
I haven't found a way around this, and I don't think there is one by design. Users' runs are their data.
What I use instead for public runs
- The 30-day counters.
GET /v2/acts/{id}returnsstats.publicActorRunStats30Daysper status, and it works without a token for public actors. A script stores that once a day per actor, and an alert fires on +5 failures since the previous day, or when an actor goes over its 30-day failure limit. Own runs don't show up in these counters, so test runs don't skew them. - The monetization export. The Console export has daily FAILED and SUCCEEDED counts per actor. That's how you tell a diffuse problem from one bad day. The 700 failures above came in three days, which pointed at one user's large batch. The cause was mine: Apify proxy session names are account-wide, every run used the same session name, so the user's parallel runs all went out through one IP and got blocked together. Session names that include the run ID fixed it.
- Fewer ways to fail. Tolerant input parsing, required fields that the platform rejects before a run exists, and stopping before the run timeout instead of being killed by it. These moved more than any amount of telemetry.
Once that burst left the 30-day window, the portfolio went from 744 failed runs out of 7,773 (9.57%) to 6 out of 2,011 (0.30%).
Where the webhooks still pay off
Scheduled runs in your own account. I run a small daily canary on three actors that depend on Google pages. Their failures land in the same dataset through the same webhooks. On top of that, a second webhook on SUCCEEDED writes chargedEventCounts for each canary run. A run that succeeds with zero results is the failure I most want to catch, and it doesn't show up as FAILED anywhere.
If you only want one thing from this post: before you build telemetry on webhooks for a public actor, start a run from a second account and check whether your webhook fires.
