Google Apps Script for accessing NeverBounce RESTful API (and writing to a GSheet)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var VALID_COLUMN_INDEX = 4 | |
var EMAIL_COLUMN_INDEX = 3 | |
function onOpen() { | |
SpreadsheetApp | |
.getUi() | |
.createMenu('Validate Emails') | |
.addItem('Validate Sheet', 'validateEmail') | |
.addToUi() | |
} | |
function validateEmail() { | |
var properties = PropertiesService.getScriptProperties().getProperties() | |
var API_SECRET = properties['API_SECRET'] | |
if (typeof API_SECRET === 'undefined') { | |
throw new Error('You need to define a script property for the API Secret') | |
} | |
var API_USERNAME = properties['API_SECRET'] | |
if (typeof API_USERNAME === 'undefined') { | |
throw new Error('You need to define a script property for the API Username') | |
} | |
var headers = { | |
"Authorization": "Basic " + Utilities.base64Encode(API_USERNAME + ":" + API_SECRET) | |
} | |
var payload = { | |
"grant_type": "client_credentials", | |
"scope": "basic user web", | |
} | |
var options = { | |
"headers": headers, | |
"method": "post", | |
"payload": payload, | |
} | |
var json = UrlFetchApp | |
.fetch('https://api.neverbounce.com/v3/access_token', options) | |
.getContentText() | |
var accessToken = JSON.parse(json).access_token | |
var dataRange = SpreadsheetApp | |
.getActiveSpreadsheet() | |
.getSheetByName('Sheet1') | |
.getDataRange() | |
var data = dataRange.getValues() | |
var header = data.shift() | |
data.forEach(function(row) { | |
payload = { | |
"access_token": accessToken, | |
"email": row[EMAIL_COLUMN_INDEX], | |
} | |
options = { | |
"method": "post", | |
"payload": payload, | |
} | |
var json = UrlFetchApp | |
.fetch('https://api.neverbounce.com/v3/single', options) | |
.getContentText() | |
var response = JSON.parse(json) | |
var success = response.success | |
row[VALID_COLUMN_INDEX] = (success) ? response.result : response.msg | |
}) | |
data.unshift(header) | |
dataRange.setValues(data) | |
return | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment