-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
45 lines (41 loc) · 1.12 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/**
* Install node.js
* run 'npm install'
* create a .env file with a single key value pair in it - key: API_KEY=<HUBSPOT_API_KEY>
* run 'node index.js'np
*/
const dotenv = require('dotenv');
dotenv.config();
const apikey = process.env.API_KEY;
const owners = `http://api.hubapi.com/owners/v2/owners?hapikey=${apikey}`;
const axios = require('axios');
const { parse } = require('json2csv');
const write = require('write');
async function query(url) {
axios.default
.get(url)
.then(function(response) {
const json = response.data;
const fields = ['type', 'ownerId', 'firstName', 'lastName', 'email'];
const opts = { fields };
try {
const csv = parse(json, opts);
write.sync('hubspot-owners.csv', csv, { newline: true });
console.log(`The following was written to file: hubspot-owners.csv`);
console.log(csv);
} catch (err) {
console.error(err);
}
})
.catch(function(error) {
// handle error
console.log(error);
})
.finally(function() {
// always executed
})
}
async function run() {
await query(owners);
}
run();