-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetusers.js
More file actions
71 lines (63 loc) · 1.65 KB
/
Copy pathgetusers.js
File metadata and controls
71 lines (63 loc) · 1.65 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import axios from "axios";
import { writeFileSync } from "fs";
import { Parser } from "@json2csv/plainjs";
const API_URL = "https://api.frontegg.com/identity/resources/users/v3";
const BEARER_TOKEN = "[YOUR_VENDOR_TOKEN]";
const TENANT_ID = "[YOUR_TENANT_ID]"; // Optional tenant ID - set as null for no specific tenant
async function getUsers() {
let users = [];
let offset = 0;
const limit = 200;
let hasMore = true;
while (hasMore) {
try {
const response = await axios.get(API_URL, {
headers: {
Authorization: `Bearer ${BEARER_TOKEN}`,
"frontegg-tenant-id": TENANT_ID,
},
params: {
_limit: limit,
_offset: offset,
_sortBy: "createdAt",
_order: "ASC",
},
});
users = users.concat(response.data.items);
if (response.data.items.length < limit) {
hasMore = false;
} else {
offset += limit;
}
} catch (error) {
console.error(
"Error fetching users:",
error.response?.data || error.message
);
break;
}
}
console.log(`Total users fetched: ${users.length}`);
return users;
}
function exportToCSV(users) {
if (users.length === 0) {
console.log("No users to export.");
return;
}
try {
const parser = new Parser();
const csv = parser.parse(users);
writeFileSync("users.csv", csv);
console.log("Users exported to users.csv");
} catch (error) {
console.error("Error exporting users to CSV:", error);
}
}
getUsers()
.then((users) => {
exportToCSV(users);
})
.catch((error) => {
console.error("Failed to fetch users:", error);
});