Pay-per-event monetization
Apify provides several pricing models for monetizing your Actors. The most recent and most flexible one is 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.
To use the pay-per-event pricing model, you first need to set it up for your Actor in the Apify console. After that, you're free to start charging for events.
Charging for events
After monetization is set in the Apify console, you can add Actor.charge
calls to your code and start monetizing!
from apify import Actor
async def main() -> None:
async with Actor:
# Charge for a single occurence of an event
await Actor.charge(event_name='init')
# Prepare some mock results
result = [
{'word': 'Lorem'},
{'word': 'Ipsum'},
{'word': 'Dolor'},
{'word': 'Sit'},
{'word': 'Amet'},
]
# Shortcut for charging for each pushed dataset item
await Actor.push_data(result, 'result-item')
# Or you can charge for a given number of events manually
await Actor.charge(
event_name='result-item',
count=len(result),
)
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.
If you need finer control over charging, you can access call Actor.get_charging_manager()
to access the ChargingManager
, which can provide more detailed information - for example how many events of each type can be charged before reaching the configured limit.
Transitioning from a different pricing model
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 ChargingManager.get_pricing_info()
method which returns information about the current pricing model.
from apify import Actor
async def main() -> None:
async with Actor:
# Check the dataset because there might already be items
# if the run migrated or was restarted
default_dataset = await Actor.open_dataset()
dataset_info = await default_dataset.get_info()
charged_items = dataset_info.item_count if dataset_info else 0
if Actor.get_charging_manager().get_pricing_info().is_pay_per_event:
await Actor.push_data({'hello': 'world'}, 'dataset-item')
elif charged_items < (Actor.config.max_paid_dataset_items or 0):
await Actor.push_data({'hello': 'world'})
charged_items += 1
Local development
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:
ACTOR_TEST_PAY_PER_EVENT=true python -m youractor
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.
Because pricing configuration is stored by the Apify platform, all events will have a default price of $1.