Skip to content

Commit 6b5712b

Browse files
committed
docs: Add PPE guide
1 parent 09a6542 commit 6b5712b

File tree

4 files changed

+95
-1
lines changed

4 files changed

+95
-1
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from apify import Actor
2+
3+
4+
async def main() -> None:
5+
async with Actor:
6+
# highlight-start
7+
# Charge for a single occurence of an event
8+
await Actor.charge(event_name='init')
9+
# highlight-end
10+
11+
# Prepare some mock results
12+
result = [
13+
{'word': 'Lorem'},
14+
{'word': 'Ipsum'},
15+
{'word': 'Dolor'},
16+
{'word': 'Sit'},
17+
{'word': 'Amet'},
18+
]
19+
# highlight-start
20+
# Shortcut for charging for each pushed dataset item
21+
await Actor.push_data(result, 'result-item')
22+
# highlight-end
23+
24+
# highlight-start
25+
# Or you can charge for a given number of events manually
26+
await Actor.charge(
27+
event_name='result-item',
28+
count=len(result),
29+
)
30+
# highlight-end
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from apify import Actor
2+
3+
4+
async def main() -> None:
5+
async with Actor:
6+
# Check the dataset because there might already be items
7+
# if the run migrated or was restarted
8+
default_dataset = await Actor.open_dataset()
9+
dataset_info = await default_dataset.get_info()
10+
charged_items = dataset_info.item_count if dataset_info else 0
11+
12+
# highlight-start
13+
if Actor.get_charging_manager().get_pricing_info().is_pay_per_event:
14+
# highlight-end
15+
await Actor.push_data({'hello': 'world'}, 'dataset-item')
16+
elif charged_items < (Actor.config.max_paid_dataset_items or 0):
17+
await Actor.push_data({'hello': 'world'})
18+
charged_items += 1

docs/02_guides/pay_per_event.mdx

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
---
2+
id: pay-per-event
3+
title: Pay-per-event Monetization
4+
description: Monetize your Actors using the pay-per-event pricing model
5+
---
6+
7+
import ActorChargeSource from '!!raw-loader!./code/actor_charge.py';
8+
import ConditionalActorChargeSource from '!!raw-loader!./code/conditional_actor_charge.py';
9+
import ApiLink from '@site/src/components/ApiLink';
10+
import CodeBlock from '@theme/CodeBlock';
11+
12+
Apify provides several [pricing models](https://docs.apify.com/platform/actors/publishing/monetize) for monetizing your Actors. The most recent and most flexible one is [pay-per-event](https://docs.apify.com/platform/actors/running/actors-in-store#pay-per-event), which lets you charge your users programmatically directly from your Actor. As the name suggests, you may charge the users each time a specific event occurs, for example a call to an external API or when you return a result.
13+
14+
To use the pay-per-event pricing model, you first need to [set it up](https://docs.apify.com/platform/actors/running/actors-in-store#pay-per-event) for your Actor in the Apify console. After that, you're free to start charging for events.
15+
16+
## Charging for events
17+
18+
After monetization is set in the Apify console, you can add <ApiLink to="class/Actor#charge">`Actor.charge`</ApiLink> calls to your code and start monetizing!
19+
20+
<CodeBlock language="python">
21+
{ActorChargeSource}
22+
</CodeBlock>
23+
24+
Then you just push your code to Apify and that's it! The SDK will even keep track of the max total charge setting for you, so you will not provide more value than what the user chose to pay for.
25+
26+
If you need finer control over charging, you can access call <ApiLink to="class/Actor#get_charging_manager">`Actor.get_charging_manager()`</ApiLink> to access the <ApiLink to="class/ChargingManager">`ChargingManager`</ApiLink>, which can provide more detailed information - for example how many events of each type can be charged before reaching the configured limit.
27+
28+
## Transitioning from a different pricing model
29+
30+
When you plan to start using the pay-per-event pricing model for an Actor that is already monetized with a different pricing model, your source code will need support both pricing models during the transition period enforced by the Apify platform. Arguably the most frequent case is the transition from the pay-per-result model which utilizes the `ACTOR_MAX_PAID_DATASET_ITEMS` environment variable to prevent returning unpaid dataset items. The following is an example how to handle such scenarios. The key part is the <ApiLink to="class/ChargingManager#get_pricing_info">`ChargingManager.get_pricing_info`</ApiLink> method which returns information about the current pricing model.
31+
32+
<CodeBlock language="python">
33+
{ConditionalActorChargeSource}
34+
</CodeBlock>
35+
36+
## Local development
37+
38+
It is encouraged to test your monetization code on your machine before releasing it to the public. To tell your Actor that it should work in pay-per-event mode, pass it the `ACTOR_TEST_PAY_PER_EVENT` environment variable:
39+
40+
```shell
41+
ACTOR_TEST_PAY_PER_EVENT=true npm start
42+
```
43+
44+
If you also wish to see a log of all the events charged throughout the run, the Apify SDK keeps a log of charged events in a so called charging dataset. Your charging dataset can be found under the `charging_log` name (unless you change your storage settings, this dataset is stored in `storage/datasets/charging_log/`). Please note that this log is not available when running the Actor in production on the Apify platform.
45+
46+
Because pricing configuration is stored by the Apify platform, all events will have a default price of $1.

uv.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)