Skip to content

Instantly share code, notes, and snippets.

@oliv-j
Created February 23, 2023 15:00
Show Gist options
  • Save oliv-j/095259b93ed19c2b6d05d7d75653779d to your computer and use it in GitHub Desktop.
Save oliv-j/095259b93ed19c2b6d05d7d75653779d to your computer and use it in GitHub Desktop.
Google Sheets AppScript to query Clearbit data augmentation API and return either a company name or the company domain name
//pass text from a cell (e.g. A1) to call the function by entering the following in to a cell '=getDomain(A1)'
//The result, when obtained, will then be seen in the cell (A1, in this example).
//requires a free Clearbit account and your api token, which you need to enter below by replacing 'YOUR_TOKEN_HERE'
function getDomain(domain) {
var url = "https://company.clearbit.com/v1/domains/find?name="+domain;
var options = {
"headers" : {
Authorization:"Bearer YOUR_TOKEN_HERE"
}
};
var response = UrlFetchApp.fetch(url,options);
var responseCode = response.getResponseCode()
var responseBody = response.getContentText()
if (responseCode == 200) {
Logger.log(response.getContentText());
var out = JSON.parse(response.getContentText());
try {
return out['domain'];
} catch {
return out[0]['domain'];
}
} else {
Logger.log(Utilities.formatString("Request failed. Expected 200, got %d: %s", responseCode, responseBody));
return Utilities.formatString("Request failed. Expected 200, got %d: %s", responseCode, responseBody);
}
}
//pass text from a cell (e.g. A1) to call the function by entering the following in to a cell '=getName(A1)'
//The result, when obtained, will then be seen in the cell (A1, in this example).
//requires a free Clearbit account and your api token, which you need to enter below by replacing 'YOUR_TOKEN_HERE'
function getName(name) {
var url = "https://company.clearbit.com/v1/domains/find?name="+name;
var options = {
"headers" : {
Authorization:"Bearer YOUR_TOKEN_HERE"
}
};
var response = UrlFetchApp.fetch(url,options);
var responseCode = response.getResponseCode()
var responseBody = response.getContentText()
if (responseCode == 200) {
Logger.log(response.getContentText());
var out = JSON.parse(response.getContentText());
try {
return out['name'];
} catch {
return out[0]['name'];
}
} else {
Logger.log(Utilities.formatString("Request failed. Expected 200, got %d: %s", responseCode, responseBody));
return Utilities.formatString("Request failed. Expected 200, got %d: %s", responseCode, responseBody);
}
}
@oliv-j
Copy link
Author

oliv-j commented Feb 23, 2023

You need a free Clearbit account to access this endpoint.
Create a new AppScript from the AppScript Menu and enter the code above.

To use in the Google Sheet:
assuming you have company name data in column A, row 2
Enter the following in an adjacent cell of the same row:
=getDomain(encodeurl(A2))

A note on quality:
If you are not getting many matches: try removing any suffixes of Inc, Ltd, Plc, Limited, Group, International, country names (e.g. GB or UK), or others (e.g. SA, Gmbh)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment