Guest User

n8n webhook trigger

a guest
Aug 5th, 2025
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.31 KB | None | 0 0
  1. function triggerWebhook() {
  2. // Your N8N webhook URL
  3. const webhookUrl = "{PRODUCTION-WEBHOOK-ENDPOINT}";
  4.  
  5. // Set up HTTP request options - just sending a simple trigger message
  6. const options = {
  7. method: 'post',
  8. contentType: 'application/json',
  9. payload: JSON.stringify({
  10. triggered: true,
  11. timestamp: new Date().toISOString()
  12. }),
  13. muteHttpExceptions: true
  14. };
  15.  
  16. try {
  17. // Send the HTTP request
  18. const response = UrlFetchApp.fetch(webhookUrl, options);
  19.  
  20. // Log the response
  21. Logger.log('Response code: ' + response.getResponseCode());
  22. Logger.log('Response body: ' + response.getContentText());
  23.  
  24. // Show a success message to the user
  25. SpreadsheetApp.getActiveSpreadsheet().toast('Webhook successfully triggered!', 'Success', 5);
  26. } catch (error) {
  27. // Log and show any errors
  28. Logger.log('Error: ' + error.toString());
  29. SpreadsheetApp.getActiveSpreadsheet().toast('Error triggering webhook: ' + error.toString(), 'Error', 10);
  30. }
  31. }
  32.  
  33. /**
  34. * Optional: Adds a custom menu to the Google Sheets UI
  35. * This allows users to trigger the webhook from the menu
  36. */
  37. function onOpen() {
  38. const ui = SpreadsheetApp.getUi();
  39. ui.createMenu('Webhook Tools')
  40. .addItem('Trigger N8N Webhook', 'triggerWebhook')
  41. .addToUi();
  42. }
Advertisement
Add Comment
Please, Sign In to add comment