Help

Help creating script to update Memberstack JSON

3284 4
cancel
Showing results for 
Search instead for 
Did you mean: 
NoCodeLearner32
5 - Automation Enthusiast
5 - Automation Enthusiast

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();

 

 

4 Replies 4

I don’t know JavaScript so I can’t help you there, but I would just use Make’s MemberStack integrations. 

It’s a no-code/low-code environment, so you don’t need to write any scripts. 

There is a small learning curve with Make, so I created this basic navigation video to help, along with providing the links to a few other Make training resources. For example, to instantly trigger your Make scenarios from Airtable, check out this thread.

 

 

Hey @ScottWorld, it won't work because the make api doesn't accept JSON without writing over everything in the existing JSON format. 

Make has an entire library of JSON tools that you can use, if necessary. Although it might not even be necessary, because Make writes all the JSON code for you.

I appreciate your response but as I mentioned the Make API doesn't integrate with JSON and Memberstack natively so there will need to be some custom approach.