|
| 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. |
0 commit comments