Hi there,
I'm trying to create a script that takes data from Airtable and updates it into Memberstack JSON. I went this scripting route because I couldn't figure out a no-code alternative to this. The problem is I'm not getting any errors messages with my console logs and I don't know where to start on debugging what's wrong.
Here is my setup:
const MEMBERSTACK_API_KEY = 'API KEY HERE';
const AIRTABLE_API_TOKEN = 'API KEY HERE';
const AIRTABLE_BASE_ID = 'appFifDmh7qLKggjt';
const AIRTABLE_TABLE_ID = 'tblH65jLDy3WXddUQ';
const AIRTABLE_VIEW_ID = 'viwOTOaTHeGogrF5h';
// Function to update Memberstack user profiles
async function updateMemberstackProfiles() {
try {
// Fetch records from Airtable
const airtableUrl = `https://api.airtable.com/v0/${AIRTABLE_BASE_ID}/${AIRTABLE_TABLE_ID}?view=${AIRTABLE_VIEW_ID}`;
const airtableHeaders = {
headers: {
Authorization: `Bearer ${AIRTABLE_API_TOKEN}`,
},
};
const airtableResponse = await fetch(airtableUrl, airtableHeaders);
const airtableData = await airtableResponse.json();
const airtableRecords = airtableData.records;
console.log(`Found ${airtableRecords.length} records in Airtable.`);
// Process and update records in Memberstack
for (const record of airtableRecords) {
const memberstackId = record.fields['Memberstack ID']; // Replace 'Memberstack ID' with the actual field name
const adaptability = record.fields['Adaptability-Q1'];
const courage = record.fields['Courage-Q1'];
const perseverance = record.fields['Perseverance-Q1'];
const resilience = record.fields['Resilience-Q1'];
console.log(`Updating profile for Memberstack ID ${memberstackId}`);
const memberstackJson = {
'grit-results': {
adaptability,
courage,
perseverance,
resilience,
},
// You can add more fields to the JSON as needed
};
// Update Memberstack profile using the Memberstack API
const memberstackUrl = `https://api.memberstack.com/v1/members/${memberstackId}`;
const memberstackHeaders = {
method: 'PATCH',
headers: {
Authorization: `Bearer ${MEMBERSTACK_API_KEY}`,
'Content-Type': 'application/json',
},
body: JSON.stringify(memberstackJson),
};
await fetch(memberstackUrl, memberstackHeaders);
console.log(`Updated Memberstack profile for ID ${memberstackId}`);
}
console.log('Bulk update completed.');
} catch (error) {
console.error('An error occurred:', error);
}
}
// Call the function to start the update process
updateMemberstackProfiles();