You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -51,6 +51,79 @@ An Actor's negative net profit does not affect the positive profit of another Ac
51
51
1._Test your pricing_: Run your Actor and analyze cost-effectiveness using a special dataset.
52
52
1._Communicate value_: Ensure pricing reflects the value provided and is competitive.
53
53
54
+
## Respect user spending limits
55
+
56
+
Finish the Actor run once charging reaches user-configured maximum cost per run. Apify SDKs (JS and Python) return `ChargeResult` that helps determine when to finish.
57
+
58
+
The `eventChargeLimitReached` property checks if the current event type can be charged more. If you have multiple event types, analyze the `chargeableWithinLimit` property to see if other events can still be charged before stopping the Actor.
For pay-per-event Actors, users set a spending limit through the Apify Console. This limit is available in your Actor code as the `ACTOR_MAX_TOTAL_CHARGE_USD`[environment variable](/platform/actors/development/programming-interface/environment-variables), which contains the user's maximum cost.
When using [Crawlee](https://crawlee.dev/), use `crawler.autoscaledPool.abort()` instead of `Actor.exit()` to gracefully finish the crawler and allow the rest of your code to process normally.
124
+
125
+
:::
126
+
54
127
## Best practices for PPE Actors
55
128
56
129
Use our SDKs ([JS](/sdk/js/) and, [Python](/sdk/python/) or use [`apify actor charge`](/cli/docs/next/reference#apify-actor-charge-eventname) when using our Apify CLI) to simplify PPE implementation into your Actor. This tool can handle pricing, usage tracking, idempotency keys, API errors, and, event charging via an API.
// Charge for the work done and return error item in one call
166
230
awaitActor.pushData({
167
231
url: url,
168
232
error:"404",
169
233
errorMessage:"Page not found"
170
-
});
234
+
}, 'scraped-result');
171
235
172
236
return;
173
237
}
174
238
175
-
// Rest of the Actor logic
239
+
// Rest of the process_url function
176
240
};
177
241
178
242
awaitActor.init();
179
243
180
-
constmain=async () => {
181
-
constinput=awaitActor.getInput();
182
-
const { urls } = input;
244
+
constinput=awaitActor.getInput();
245
+
const { urls } = input;
183
246
184
-
for (consturlof urls) {
185
-
awaitprocessUrl(url);
186
-
}
187
-
188
-
// Rest of the Actor logic
189
-
};
247
+
for (consturlof urls) {
248
+
awaitprocessUrl(url);
249
+
}
190
250
191
-
awaitmain();
251
+
// Rest of the Actor logic
192
252
193
253
awaitActor.exit();
194
254
```
@@ -204,19 +264,16 @@ async def process_url(url):
204
264
response = requests.get(url)
205
265
206
266
if response.status_code ==404:
207
-
# Charge for the work done (opening the page)
208
-
await Actor.charge(event_name='scraped-result')
209
-
210
-
# Return error item instead of failing
211
-
await Actor.push_data({
212
-
'url': url,
213
-
'error': '404',
214
-
'errorMessage': 'Page not found'
215
-
})
267
+
# Charge for the work done and return error item in one call
268
+
await Actor.push_data({
269
+
'url': url,
270
+
'error': '404',
271
+
'errorMessage': 'Page not found'
272
+
}, 'scraped-result')
216
273
217
-
return
274
+
return
218
275
219
-
# Rest of the Actor logic
276
+
# Rest of the process_url function
220
277
221
278
asyncdefmain():
222
279
await Actor.init()
@@ -235,80 +292,6 @@ async def main():
235
292
</TabItem>
236
293
</Tabs>
237
294
238
-
### Respect user spending limits
239
-
240
-
Finish the Actor run once charging reaches user-configured maximum cost per run. Apify SDKs (JS and Python) return `ChargeResult` that helps determine when to finish.
241
-
242
-
The `eventChargeLimitReached` property checks if the current event type can be charged more. If you have multiple event types, analyze the `chargeableWithinLimit` property to see if other events can still be charged before stopping the Actor.
243
-
244
-
<TabsgroupId="main">
245
-
<TabItemvalue="JavaScript"label="JavaScript">
246
-
247
-
```js
248
-
import { Actor } from'apify';
249
-
250
-
constchargForApiProductDetail=async () => {
251
-
constchargeResult=awaitActor.charge({
252
-
eventName:"product-detail",
253
-
});
254
-
255
-
return chargeResult;
256
-
};
257
-
258
-
awaitActor.init();
259
-
260
-
constmain=async () => {
261
-
// API call, or any other logic that you want to charge for
When using [Crawlee](https://crawlee.dev/), use `crawler.autoscaledPool.abort()` instead of `Actor.exit()` to gracefully finish the crawler and allow the rest of your code to process normally.
309
-
310
-
:::
311
-
312
295
### Keep pricing simple with fewer events
313
296
314
297
Try to limit the number of events. Fewer events make it easier for users to understand your pricing and predict their costs.
0 commit comments