Skip to content

Crawlee—A web scraping and browser automation library for Node.js to build reliable crawlers. In JavaScript and TypeScript. Extract data for AI, LLMs, RAG, or GPTs. Download HTML, PDF, JPG, PNG, and other files from websites. Works with Puppeteer, Playwright, Cheerio, JSDOM, and raw HTTP. Both headful and headless mode. With proxy rotation.

Add payload template to .addWebhook()

Ondra Urban7b59ba5897b5
diff --git a/src/actor.js b/src/actor.js
index d9c93c953..8690580cc 100644
--- a/src/actor.js
+++ b/src/actor.js
@@ -3,6 +3,7 @@ import _ from 'underscore';
import log from 'apify-shared/log';
import { checkParamOrThrow } from 'apify-client/build/utils';
import { APIFY_PROXY_VALUE_REGEX } from 'apify-shared/regexs';
+import WebhookPayloadTemplate from 'apify-shared/webhook_payload_template';
import { ENV_VARS, INTEGER_ENV_VARS, LOCAL_ENV_VARS, ACT_JOB_TERMINAL_STATUSES, ACT_JOB_STATUSES } from 'apify-shared/consts';
import { EXIT_CODES } from './constants';
import { initializeEvents, stopEvents } from './events';
@@ -771,19 +772,30 @@ export const getApifyProxyUrl = (options = {}) => {
* Note that webhooks are only supported for actors running on the Apify platform.
* In local environment, the function will print a warning and have no effect.
*
- * @param options.eventTypes {String[]} - Array of event types, which you can set for actor run, see
- * the <a href="https://apify.com/docs/webhooks#events-actor-run" target="_blank">actor run events</a> in the Apify doc.
- * @param options.requestUrl {String} - URL which will be requested using HTTP POST request, when actor run will be in specific event type.
- *
- * @return {Promise<Object|undefined>}
+ * @param {Object} options
+ * @param {String[]} options.eventTypes
+ * Array of event types, which you can set for actor run, see
+ * the <a href="https://apify.com/docs/webhooks#events-actor-run" target="_blank">actor run events</a> in the Apify doc.
+ * @param {String} options.requestUrl
+ * URL which will be requested using HTTP POST request, when actor run will reach the set event type.
+ * @param {String} [options.payloadTemplate]
+ * Payload template is a JSON-like string that describes the structure of the webhook POST request payload.
+ * It uses JSON syntax, extended with a double curly braces syntax for injecting variables `{{variable}}`.
+ * Those variables are resolved at the time of the webhook's dispatch, and a list of available variables with their descriptions
+ * is available in the <a href="https://apify.com/docs/webhooks" target="_blank">Apify webhook documentation</a>.
+ *
+ * When omitted, the default payload template will be used.
+ * <a href="https://apify.com/docs/webhooks" target="_blank">See the docs for the default payload template</a>.
+ * @return {Promise<Object>}
*
* @memberof module:Apify
* @function
* @name addWebhook
*/
-export const addWebhook = async ({ eventTypes, requestUrl }) => {
+export const addWebhook = async ({ eventTypes, requestUrl, payloadTemplate }) => {
checkParamOrThrow(eventTypes, 'eventTypes', '[String]');
checkParamOrThrow(requestUrl, 'requestUrl', 'String');
+ checkParamOrThrow(payloadTemplate, 'payloadTemplate', 'Maybe String');
if (!isAtHome()) {
log.warning('Apify.addWebhook() is only supported when running on the Apify platform. The webhook will not be invoked.');
@@ -803,6 +815,7 @@ export const addWebhook = async ({ eventTypes, requestUrl }) => {
actorRunId: runId,
},
requestUrl,
+ payloadTemplate,
},
});
};
diff --git a/test/actor.js b/test/actor.js
index b62774523..f0eb0e302 100644
--- a/test/actor.js
+++ b/test/actor.js
@@ -1150,6 +1150,7 @@ describe('Apify.addWebhook()', () => {
const runId = 'my-run-id';
const expectedEventTypes = ['ACTOR.RUN.SUCCEEDED'];
const expectedRequestUrl = 'http://example.com/api';
+ const expectedPayloadTemplate = '{"hello":{{world}}';
process.env[ENV_VARS.ACTOR_RUN_ID] = runId;
process.env[ENV_VARS.IS_AT_HOME] = '1';
@@ -1163,6 +1164,7 @@ describe('Apify.addWebhook()', () => {
actorRunId: runId,
},
requestUrl: expectedRequestUrl,
+ payloadTemplate: expectedPayloadTemplate,
};
webhooksMock.expects('createWebhook')
@@ -1171,7 +1173,11 @@ describe('Apify.addWebhook()', () => {
.returns(Promise.resolve());
- await Apify.addWebhook({ eventTypes: expectedEventTypes, requestUrl: expectedRequestUrl });
+ await Apify.addWebhook({
+ eventTypes: expectedEventTypes,
+ requestUrl: expectedRequestUrl,
+ payloadTemplate: expectedPayloadTemplate,
+ });
delete process.env[ENV_VARS.ACTOR_RUN_ID];
delete process.env[ENV_VARS.IS_AT_HOME];
@@ -1198,7 +1204,7 @@ describe('Apify.addWebhook()', () => {
logMock.restore();
});
- it('should failed without actor run ID', async () => {
+ it('should fail without actor run ID', async () => {
const expectedEventTypes = ['ACTOR.RUN.SUCCEEDED'];
const expectedRequestUrl = 'http://example.com/api';