> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://help.sigmacomputing.com/llms.txt.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://help.sigmacomputing.com/_mcp/server.

# Members: Create New (JavaScript)

> This script creates a new member, workspace and the associated grants in Sigma by sending a POST request to the API endpoints required.

This recipe demonstrates how to use the add new user endpoint to add new users to your Sigma organization

Each section of the script has inline comments provided.

Refer to the [Sigma REST API Recipes](https://quickstarts.sigmacomputing.com/guide/developers_api_code_samples/index.html?index=../..index#0) QuickStart for step-by-step instructions.

## Example script

```javascript JavaScript
// This script creates a new member in Sigma after ensuring the email does not already exist.

// 1: Load environment variables from a specific .env file for configuration
require('dotenv').config({ path: 'sigma-api-recipes/.env' });

// 2: Import the function to obtain a bearer token from the authenticate-bearer module
const getBearerToken = require('../get-access-token');

// 3: Import Axios for making HTTP requests
const axios = require('axios');

// 4: Load use-case specific variables from environment variables
const baseURL = process.env.baseURL; // Your base URL
const baseEmail = process.env.EMAIL; // Retrieve the base email from environment variables

// Dynamically generate a unique email using the base email in the format: baseEmail+mmddhhmm@sigmacomputing.com
const now = new Date();
const timestamp = `${String(now.getMonth() + 1).padStart(2, '0')}${String(now.getDate()).padStart(2, '0')}${String(now.getHours()).padStart(2, '0')}${String(now.getMinutes()).padStart(2, '0')}`;
const newMemberEmail = `${baseEmail.split('@')[0]}+${timestamp}@${baseEmail.split('@')[1]}`;
console.log(`Generated email for new member: ${newMemberEmail}`);

// Load additional member details from the environment variables
const newMemberFirstName = process.env.NEW_MEMBER_FIRST_NAME;
const newMemberLastName = process.env.NEW_MEMBER_LAST_NAME;
const newMemberType = process.env.NEW_MEMBER_TYPE;

async function memberExists(email, accessToken) {
    const requestURL = `${baseURL}/members?search=${encodeURIComponent(email)}`;
    console.log(`Checking if member exists with search parameter: ${email}`);
    try {
        const response = await axios.get(requestURL, {
            headers: {
                'Authorization': `Bearer ${accessToken}`,
                'Accept': 'application/json',
            }
        });

        // Log the full response for debugging
        console.log('Response data:', JSON.stringify(response.data, null, 2));

        // Check if any member in the results matches the email exactly
        const members = response.data.entries || [];
        const exists = members.some(member => member.email.toLowerCase() === email.toLowerCase());

        console.log(`Member check result: ${exists ? 'Exists' : 'Does not exist'}`);
        return exists;
    } catch (error) {
        console.error('Error checking member existence:', error.response ? error.response.data : error.message);
        throw new Error('Failed to check member existence.');
    }
}

// Function to create a new member
async function addNewMember() {
    const accessToken = await getBearerToken();
    if (!accessToken) {
        console.error('Failed to obtain Bearer token.');
        return;
    }

    // Log the environment variables to validate inputs
    console.log(`New member details:
        Email: ${newMemberEmail}
        First Name: ${newMemberFirstName}
        Last Name: ${newMemberLastName}
        Member Type: ${newMemberType}`);

    // Check if the member already exists
    const exists = await memberExists(newMemberEmail, accessToken);
    if (exists) {
        console.log(`Member with email ${newMemberEmail} already exists. No action taken.`);
        return;
    }

    const requestURL = `${baseURL}/members`;
    console.log(`URL sent to Sigma: ${requestURL}`);

    try {
        // Make the API request to create the new member
        const response = await axios.post(requestURL, {
            email: newMemberEmail,
            firstName: newMemberFirstName,
            lastName: newMemberLastName,
            memberType: newMemberType, // Ensure this is passed correctly
        }, {
            headers: {
                'Content-Type': 'application/json',
                'Authorization': `Bearer ${accessToken}`
            }
        });

        // Log the successful response
        const { memberId, memberType: createdMemberType } = response.data;
        console.log('New member added successfully:');
        console.log(`Member ID: ${memberId}`);
        console.log(`Account Type: ${createdMemberType}`);
    } catch (error) {
        // Handle errors and log details
        console.error('Error adding new member:', error.response ? error.response.data : error.message);
    }
}

// Execute the function if this script is run directly
if (require.main === module) {
    addNewMember();
}

// Export the function for reuse
module.exports = addNewMember;
```

```json Response Example
New member details:
        Email: phil+11201022@sigmacomputing.com
        First Name: phil
        Last Name: api
        Member Type: Pro
create-new.js:62
Checking if member exists with search parameter: phil+11201022@sigmacomputing.com
create-new.js:29
Response data: {
  "entries": [],
  "nextPage": null
}
create-new.js:39
Member check result: Does not exist
create-new.js:45
URL sent to Sigma: https://aws-api.sigmacomputing.com/v2/members
create-new.js:76
New member added successfully:
create-new.js:94
Member ID: 3B1pLjqbSOfyk4YIBPMWLm6pyExOu
create-new.js:95
Account Type: undefined
```

## Endpoints used

1. Get authentication token: `${baseURL}/authURL`
2. List recent documents: `${baseURL}/members/${memberId}/files/recents`
3. Get all workbooks: `${baseURL}/workbooks?page=${nextPage}`
4. List users: `${baseURL}/members?page=${page}`
5. Add new user: `${baseURL}/members`