Skip to main content
Version: 3.3

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!

import { Actor } from 'apify';

await Actor.init();

// Charge for a single occurence of an event
await Actor.charge({ eventName: 'init' });

// Prepare some mock results
const result = [
{ word: 'Lorem' },
{ word: 'Ipsum' },
{ word: 'Dolor' },
{ word: 'Sit' },
{ word: 'Amet' },
];

// Shortcut for charging for each pushed dataset item
await Actor.pushData(result, 'result-item');

// Or you can charge for a given number of events manually
await Actor.charge({
eventName: 'result-item',
count: result.length,
})

await Actor.exit();

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.getChargingManager() 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.getPricingInfo method which returns information about the current pricing model.

import { Actor } from 'apify';

await Actor.init();

// Check the dataset because there might already be items if the run migrated or was restarted
const defaultDataset = await Actor.openDataset();
let chargedItems = (await defaultDataset.getInfo())!.itemCount;

if (Actor.getChargingManager().getPricingInfo().isPayPerEvent) {
await Actor.pushData({ 'hello': 'world' }, 'dataset-item');
} else {
if (chargedItems < Number(process.env['ACTOR_MAX_PAID_DATASET_ITEMS'])) {
await Actor.pushData({ 'hello': 'world' });
chargedItems += 1;
}
}

await Actor.exit();

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 npm start

If you also wish to see a log of all the events charged throughout the run, you also need to pass the ACTOR_USE_CHARGING_LOG_DATASET environment variable. Your charging dataset will then be available 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.