Drafts Action Directory

Zapier: Catch Hook Example using Credentials

Posted by agiletortoise, Last update 5 months ago

Example script demonstrates how to trigger a Zap at Zapier.com which has a “Catch Hook” trigger. This will prompt you to add a Catch Hook URL copied from Zapier.com the first time it is run. This URL will be stored as a credential.

To reuse this action for multiple zaps, duplicate it and edit the zapIdentifier var in the first script step to be a different unique identifier for each zap.

Install

Steps

  • script

    /*
    	Change these values as needed
    */
    
    // Can be any string
    // Pick something to identify credential
    // used with this action in Settings > Credentials
    
    const zapIdentifier = "Zapier-Example";
  • script

    // authorize credential to load hookURL 
    var credential = Credential.create(zapIdentifier, "Paste catch hook URL from Zap configuration at Zapier.com");
    credential.addURLField("hookURL", "Hook URL");
    
    if (credential.authorize()) {
    	const hookURL = credential.getValue("hookURL");
    	alert(hookURL);
    	// Setup data you want passed to the Zap
    	// Default pass title, body and full content of draft
    	// Other values could be passed
    	var data = {
    		"title": draft.title,
    		"body": draft.processTemplate("[[body]]"),
    		"content": draft.content
    	}
    
    	// create HTTP and make POST request to Catch Hook
    	var http = HTTP.create();
    	var response = http.request({
    	  "url": hookURL,
    	  "method": "POST",
    	  "data": data,
    	  "headers": {
    	    "Accept": "application/json"
    		}
    	});
    
    	// log result
    	console.log("HTTP Status: " + response.statusCode);
    	console.log("HTTP Response: " + response.responseText);
    
    	// if not 200 OK, fail the action
    	// Otherwise Zapier successfully received post
    	if (response.statusCode != 200) {
    		context.fail();
    	}
    	else {
    		console.log("Zap triggered");
    	}
    }
    else {
    	context.cancel();
    }

Options

  • After Success Default
    Notification Info
    Log Level Info

Comments

Actions available in the Action Directory are uploaded by community members. Use appropriate caution reviewing downloaded actions before use.