Members: Change Email Address (JavaScript)
Members: Change Email Address (JavaScript)
This script automates the process of changing a user’s email address in Sigma.
Refer to the Sigma REST API Recipes QuickStart for step-by-step instructions.
Example script
JavaScript
// This script will change an members email address, based on the memberId defined in the .env file// 1: Load environment variables from a specific .env file for configurationrequire('dotenv').config({ path: 'sigma-api-recipes/.env' });// 2: Import the function to obtain a bearer token from the authenticate-bearer moduleconst getBearerToken = require('../get-access-token');// 3: Import Axios for making HTTP requestsconst axios = require('axios');// 4: Load use-case specific variables from environment variablesconst newemail = process.env.EMAIL; // The new account type you want to assignconst memberId = process.env.MEMBERID; // The unique identifier of the member whose type you want to updateconst baseURL = process.env.baseURL; // Your base URL// Define an asynchronous function to update a member's account typeasync function updateMemberAccountType() {const accessToken = await getBearerToken();if (!accessToken) {console.log('Failed to obtain Bearer token.');return;}try {// Construct the request URL using the base URL and member IDconst requestURL = `${baseURL}/members/${memberId}`;// Log the constructed URL before sending the requestconsole.log(`URL sent to Sigma: ${requestURL}`);// Make a PATCH request to the API to update the member's account typeconst response = await axios.patch(requestURL, {email: newemail, // Data payload for the PATCH request}, {headers: {'Content-Type': 'application/json', // Indicate that the request body is JSON'Authorization': `Bearer ${accessToken}` // Authenticate the request}});// Log the response data to indicate successconsole.log('User account type updated successfully:', JSON.stringify(response.data, null, 2));} catch (error) {// Log detailed error information if the request failsconsole.error('Error updating member account type:', error.response ? error.response.data : error);}}// Execute the function to update a member's account typeupdateMemberAccountType();
Endpoints used
- Get authentication token:
getBearerTokenfunction - Update member email:
${baseURL}/members/${memberId}

