Members: Update (JavaScript)

View as Markdown

This recipe demonstrates how to update a member’s account type, based on memberId.

Each section of the script has inline comments provided.

Refer to the Sigma REST API Recipes QuickStart for step-by-step instructions.

Example script

JavaScript
1// This script will update the account type for a member, based on the memberId defined in the .env file
2
3// 1: Load environment variables from a specific .env file for configuration
4require('dotenv').config({ path: 'sigma-api-recipes/.env' });
5
6// 2: Import the function to obtain a bearer token from the authenticate-bearer module
7const getBearerToken = require('../get-access-token');
8
9// 3: Import Axios for making HTTP requests
10const axios = require('axios');
11
12// 4: Load use-case specific variables from environment variables
13const newMemberType = process.env.NEW_MEMBER_TYPE; // The new account type you want to assign
14const memberId = process.env.MEMBERID; // The unique identifier of the member whose type you want to update
15const baseURL = process.env.baseURL; // Your base URL
16
17// Define an asynchronous function to update a member's account type
18async function updateMemberAccountType() {
19 const accessToken = await getBearerToken();
20 if (!accessToken) {
21 console.log('Failed to obtain Bearer token.');
22 return;
23 }
24
25 try {
26 // Construct the request URL using the base URL and member ID
27 const requestURL = `${baseURL}/members/${memberId}`;
28
29 // Log the constructed URL before sending the request
30 console.log(`URL sent to Sigma: ${requestURL}`);
31
32 // Make a PATCH request to the API to update the member's account type
33 const response = await axios.patch(requestURL, {
34 memberType: newMemberType, // Data payload for the PATCH request
35 }, {
36 headers: {
37 'Content-Type': 'application/json', // Indicate that the request body is JSON
38 'Authorization': `Bearer ${accessToken}` // Authenticate the request
39 }
40 });
41
42 // Log the response data to indicate success
43 console.log('User account type updated successfully:', JSON.stringify(response.data, null, 2));
44 } catch (error) {
45 // Log detailed error information if the request fails
46 console.error('Error updating member account type:', error.response ? error.response.data : error);
47 }
48}
49
50// Execute the function to update a member's account type
51updateMemberAccountType();

Endpoints used

  1. Get authentication token: getBearerToken function
  2. Update member account type: ${baseURL}/members/${memberId}