Before working with this code you should have a basic familiarity with node.js and with terminals. You will need a node server to run these scripts.
🚩 Do not attempt to run these scripts client-side.
The following code snippet will fetch all of the members on your account and update their custom fields.
import memberstackAdmin from "@memberstack/admin";
import dotenv from "dotenv";
dotenv.config();
const SECRET_KEY = "👉 key goes here 👈" || process.env.MEMBERSTACK_SK;
const memberstack = memberstackAdmin.init(SECRET_KEY);
const limit = 60; // Number of results to fetch per request
const delay = 40; // Delay between requests in milliseconds
// Replace with your metadata object
const metaData = {
[key]: "value", //👈
[key]: "value", //👈
};
// Replace with your custom fields object
const customFields = {
[key]: "value", //👈
[key]: "value", //👈
};
async function fetchMembers() {
let cursor = null;
let counter = 1;
while (true) {
const response = await memberstack.members.list({ after: cursor, limit });
// Loop through the data and log each member's ID with the counter
for (const member of response.data) {
// console.log(`Member # ${counter}: ${member.id}`);
const updatedMember = await memberstack.members.update({
id: member.id,
data: {
metaData,
customFields,
},
});
console.log(`Updated member # ${counter}: ${member.id}`);
console.log(updatedMember);
counter++;
}
if (counter <= response.totalCount) {
await new Promise((resolve) => setTimeout(resolve, delay));
cursor = response.endCursor;
console.log("Fetching members after: ", cursor, counter - 1);
} else {
console.log("All members fetched!");
break;
}
}
}
fetchMembers();
Comments
0 comments
Please sign in to leave a comment.