import { createTrigger, TriggerStrategy, WebhookHandshakeStrategy } from '@activepieces/pieces-framework';
import { HttpMethod, httpClient } from '@activepieces/pieces-common';
export const newEvent = createTrigger({
auth: someAuth,
name: 'new_event',
displayName: 'New Event',
description: 'Fires when a new event is generated',
type: TriggerStrategy.WEBHOOK,
props: {},
sampleData: {
id: 'evt_123',
type: 'event.created',
},
// Called once when the flow is published; register the webhook with the third-party app.
async onEnable(context) {
const response = await httpClient.sendRequest<{ id: string }>({
method: HttpMethod.POST,
url: 'https://api.example.com/webhooks',
body: {
url: context.webhookUrl,
},
});
await context.store.put('webhookId', response.body.id);
},
// Called once when the flow is disabled; deregister the webhook.
async onDisable(context) {
const webhookId = await context.store.get('webhookId');
if (webhookId) {
await httpClient.sendRequest({
method: HttpMethod.DELETE,
url: `https://api.example.com/webhooks/${webhookId}`,
});
}
},
// Optional; only needed if the third-party app requires a verification challenge
// when the webhook URL is first registered.
handshakeConfiguration: {
strategy: WebhookHandshakeStrategy.HEADER_PRESENT,
paramName: 'x-verification-challenge',
},
async onHandshake(context) {
const challenge = context.payload.headers['x-verification-challenge'];
return {
status: 200,
body: { challenge },
};
},
// Called on every incoming webhook request once the trigger is live.
async run(context) {
return [context.payload.body];
},
});