Hello everyone! I recently decided to create a custom node which is using the webhooksMethods create and delete.
My goal, at the moment of the creation, is to register the node in a third-party app and get a unique ID, so what I need to do is to get access to environments variables to understand what is the environment I am trying to connect with, and once I get the response from the client, I would like to store the value to be used in the delete method.
Here is my code:
export class CustomTrigger implements INodeType {
description: INodeTypeDescription = {
displayName: 'Custom Trigger',
name: 'customTrigger',
description: '...',
icon: '...',
group: ['trigger'],
version: 1,
defaults: {
name: 'Custom Trigger',
color: '...',
},
inputs: [],
outputs: ['main'],
credentials: [
{
name: 'myCustomCredentials',
required: true,
}
],
webhooks: [
{
name: 'default',
httpMethod: 'POST',
path: 'customTrigger',
responseMode: 'onReceived',
},
],
properties: [
{
displayName: 'Event Type',
name: 'eventType',
type: 'options',
required: true,
default: '',
options: [
{
name: 'Option 1',
value: 'option1',
},
{
name: 'Option 2',
value: 'option2',
}
],
},
],
};
webhookMethods = {
default: {
async checkExists(this: IHookFunctions): Promise<boolean> {
// TODO: Check if this workflow is subscribed already to the external service
return false;
},
async create(this: IHookFunctions): Promise<boolean> {
const credentials = this.getCredentials('customCredentials');
const webhookUrl = this.getNodeWebhookUrl('default');
const event = this.getNodeParameter('eventType') as string;
// TODO: Subscribe to the external service and get the ID
// TODO: Store the webhook ID so we can delete it later
const client = platformClient({
**endpoint: "https://url-of-external-service.io", // This should be read from an environment variable defined in cli/config/index**
authorizationToken: credentials.apiKey
})
const newSubscription = await client.subscribeToWebhook({
url: webhookUrl,
eventTypes: event
})
// TODO: We need to persist the **newSubscription.id**
return true;
},
async delete(this: IHookFunctions): Promise<boolean> {
// TODO: Similar to create but here we will remove the subscription
return true;
},
},
};
async webhook(this: IWebhookFunctions): Promise<IWebhookResponseData> {
const req = this.getRequestObject();
const { body } = req as { body: IDataObject };
return {
workflowData: [[{ json: body }]],
};
}
}
So what I need to do is 1st, read an environment variable I have defined as part of cli/config/index.ts
, and then I need to persist the value either, as part of the Node information or the Workflow.
How can I do this?