Skip to main content

Creating webhooks

Webhooks allow you to configure the Apify platform to perform an action when a certain event occurs. For example, you can use them to start another Actor when the current run finishes or fails.

You can learn more in the documentation for webhooks.

Creating an ad-hoc webhook dynamically

Besides creating webhooks manually in Apify Console, or through the Apify API, you can also create ad-hoc webhooks dynamically from the code of your Actor using the Actor.add_webhook() method:

src/main.py
from apify import Actor

async def main():
async with Actor:
await Actor.add_webhook(
event_types: ['ACTOR.RUN.FAILED'],
request_url: 'https://example.com/run-failed',
)
raise RuntimeError('I am an error and I know it!')

Note that webhooks are only supported when running on the Apify platform. When running the Actor locally, the method will print a warning and have no effect.

Preventing duplicate webhooks

To ensure that duplicate ad-hoc webhooks won't get created in a case of Actor restart, you can use the idempotencyKey parameter. The idempotency key must be unique across all the webhooks of a user so that only one webhook gets created for a given value. You can use, for example, the Actor run ID as the idempotency key:

src/main.py
from apify import Actor

async def main():
async with Actor:
await Actor.add_webhook(
event_types: ['ACTOR.RUN.FAILED'],
request_url: 'https://example.com/run-failed',
idempotency_key: Actor.config.actor_run_id,
)
raise RuntimeError('I am an error and I know it!')