If you sell an Apify Actor with pay-per-event (PPE) pricing, the most important line of code is the one that decides when a user gets charged. Get it wrong and users pay for timeouts, 404s and empty pages, and they leave a one-star review. Get it right and the pricing sells itself: "you only pay for results you actually got".
This post shows the pattern in about 30 lines of Node.js, and, more usefully, how to test the charging locally before you publish, using two SDK environment variables that are easy to miss.
Everything below was run today with apify 3.8.0 on Node 22.
The rule
Success: save the row and charge one event, in the same call.
Failure: still save the row (so the user can see what went wrong), but don't charge .
Budget hit: if the user's "max cost per run" is reached, stop cleanly instead of doing work nobody pays for.
In current 3.x versions of the SDK, Actor.pushData() takes an optional event name as its second argument.…