---
updatedAt: 2025-09-25T21:10:45.000Z
agentTools:
  projectIndex: https://developers.toriihq.com/llms.txt
---

# Figma - Custom Integration

```javascript AppScript
let API_KEY;
let APP_ID;

function onOpen() {
    let ui = SpreadsheetApp.getUi();
    ui.createMenu('Torii')
        .addItem('Sync now', 'syncNow')
        .addItem('Reconfigure', 'readConfig')
        .addToUi();
}

function syncNow() {
    let docProperties = PropertiesService.getDocumentProperties();
    API_KEY = docProperties.getProperty('api_key');
    APP_ID = docProperties.getProperty('app_id');


    if (!API_KEY || !APP_ID) {
        readConfig();
        docProperties = PropertiesService.getDocumentProperties();
        API_KEY = docProperties.getProperty('api_key');
        APP_ID = docProperties.getProperty('app_id');
    }

    let file = createJsonFile();
    let fileId = uploadFile(file);
    syncIntegration(fileId);
}

function readConfig() {
    let ui = SpreadsheetApp.getUi();
    let result = ui.prompt("Please enter Torii API Key");
    API_KEY = result.getResponseText();

    result = ui.prompt("Please enter the APP ID");
    APP_ID = result.getResponseText();

    let docProperties = PropertiesService.getDocumentProperties();
    docProperties.setProperty('api_key', API_KEY);
    docProperties.setProperty('app_id', APP_ID);
}

function createJsonFile() {
    let = sheet = SpreadsheetApp.getActive();
    let data = sheet.getDataRange().getValues();
    Logger.log(SpreadsheetApp.getActiveSheet().getName());
    Logger.log(data);
    data.shift();


    let fullDataSet = [];
    let licensesMap = {};
    let userDataSet = [];

    data.forEach(function(row) {
            let fullName = row[0];
            let firstName = fullName.split(' ').slice(0, -1).join(' ');
            let lastName = fullName.split(' ').slice(-1).join(' ');
            let email = row[1];
            let role = row[2];
            let figmaAccount = "Figma - " + row[3];
            let figjamAccount = "Figjam - " + row[4];

            let figmalastseen = (row[9] == "") ? null : Utilities.formatDate(new Date(row[9]), "UTC", 'YYYY-MM-dd');
            let figjamlastseen = (row[10] == "") ? null : Utilities.formatDate(new Date(row[10]), "UTC", 'YYYY-MM-dd');
            let figmalastedit = (row[11] == "") ? null : Utilities.formatDate(new Date(row[11]), "UTC", 'YYYY-MM-dd');
            let figjamlastedit = (row[12] == "") ? null : Utilities.formatDate(new Date(row[12]), "UTC", 'YYYY-MM-dd');



            fullDataSet.push({
                email: email,
                firstName: firstName,
                lastName: lastName,
                status: "active",
                externalStatus: "active",
                role: role,
                figmaAccount: figmaAccount,
                figjamAccount: figjamAccount,
                figmalastseen: figmalastseen,
                figjamlastseen: figjamlastseen,
                figmalastedit: figmalastedit,
                figjamlastedit: figjamlastedit
            });

        }

    );

    for (var i = 0; i < Object.keys(fullDataSet).length; i++) {
        if (fullDataSet[i].role == "member" || fullDataSet[i].role == "admin" ) {
            let userFigma;
            if (licensesMap[fullDataSet[i].figmaAccount]) {
                userFigma = licensesMap[fullDataSet[i].figmaAccount]
            } else {
                userFigma = []
            }

            userFigma.push({
                email: fullDataSet[i].email,
                licenseStatus: "active",
                lastUsedDate: (fullDataSet[i].figmaAccount.indexOf("Editor") > 0) ? fullDataSet[i].figmalastedit : fullDataSet[i].figmalastseen
            })
            licensesMap[fullDataSet[i].figmaAccount] = userFigma;

            let userFigjam;
            if (licensesMap[fullDataSet[i].figjamAccount]) {
                userFigjam = licensesMap[fullDataSet[i].figjamAccount]
            } else {
                userFigjam = []
            }

            userFigjam.push({
                email: fullDataSet[i].email,
                licenseStatus: "active",
                lastUsedDate: (fullDataSet[i].figjamAccount.indexOf("Editor") > 0) ? fullDataSet[i].figjamlastedit : fullDataSet[i].figjamlastseen
            })
            licensesMap[fullDataSet[i].figjamAccount] = userFigjam;

        } else if(fullDataSet[i].role == "guest") {
          let userGuestFigma;
          let userGuestFigmaAcount = fullDataSet[i].figmaAccount.concat(" (Guest)");

          if (licensesMap[userGuestFigmaAcount]) {
                userGuestFigma = licensesMap[userGuestFigmaAcount]
            } else {
                userGuestFigma = []
            }

            userGuestFigma.push({
                email: fullDataSet[i].email,
                licenseStatus: "active",
                lastUsedDate: (userGuestFigmaAcount.indexOf("Editor") > 0) ? fullDataSet[i].figmalastedit : fullDataSet[i].figmalastseen
            })
            licensesMap[userGuestFigmaAcount] = userGuestFigma;

            let userGuestFigjam;
            let userGuestFigjamAcount = fullDataSet[i].figjamAccount.concat(" (Guest)");

            if (licensesMap[userGuestFigjamAcount]) {
                userGuestFigjam = licensesMap[userGuestFigjamAcount]
            } else {
                userGuestFigjam = []
            }

            userGuestFigjam.push({
                email: fullDataSet[i].email,
                licenseStatus: "active",
                lastUsedDate: (userGuestFigjamAcount.indexOf("Editor") > 0) ? fullDataSet[i].figjamlastedit : fullDataSet[i].figjamlastseen
            })
            licensesMap[userGuestFigjamAcount] = userGuestFigjam;

        } else {
            let userOther;
            if (licensesMap[fullDataSet[i].role]) {
                userOther = licensesMap[fullDataSet[i].role]
            } else {
                userOther = []
            }
            userOther.push({
                email: fullDataSet[i].email,
                licenseStatus: "active"
            })
            licensesMap[fullDataSet[i].role] = userOther;
        }
        //Create the main user data set
        const isUserFound = userDataSet.some(element => {
            if (element.email === fullDataSet[i].email) {
                return true;
            }

            return false;
        });
        if (isUserFound === false) {
            userDataSet.push({
                email: fullDataSet[i].email,
                firstName: fullDataSet[i].firstName,
                lastName: fullDataSet[i].lastName,
                status: "active"
            });

        }

    }

    const licenses = Object.keys(licensesMap).map(licenseName => {
        const users = licensesMap[licenseName]
        return {
            name: licenseName,
            users: users
        }
    })

    let file = {
        users: userDataSet,
        licenses: licenses
    }
    Logger.log(file);
    console.log(JSON.stringify(file));
    return file;
}

function syncIntegration(fileId) {
    response = UrlFetchApp.fetch('https://api.toriihq.com/beta/services/sync/custom', {
        muteHttpExceptions: false,
        'method': 'put',
        'contentType': 'application/json',
        'payload': JSON.stringify({
            idFile: fileId,
            idAppAccount: APP_ID
        }),
        headers: {
            'Authorization': `Bearer ${API_KEY}`
        }
    });
    Logger.log(response);
}

function uploadFile(file) {
    let response = UrlFetchApp.fetch('https://api.toriihq.com/beta/files/url?name=my_file.json&type=customIntegrationData', {
        headers: {
            'Authorization': `Bearer ${API_KEY}`
        }
    });

    let jsonResponse = JSON.parse(response);
    let s3url = jsonResponse.uploadFileParameters.url;
    let filePath = jsonResponse.uploadFileParameters.filePath;
    Logger.log(s3url);
    Logger.log(filePath);

    response = UrlFetchApp.fetch(s3url, {
        'method': 'put',
        'contentType': 'customIntegrationData',
        'payload': JSON.stringify(file)
    });

    // Get file id
    response = UrlFetchApp.fetch('https://api.toriihq.com/beta/files', {
        method: 'post',
        contentType: 'application/json',
        headers: {
            'Authorization': `Bearer ${API_KEY}`
        },
        payload: JSON.stringify({
            path: filePath,
            type: 'customIntegrationData'
        }),
        headers: {
            'Authorization': `Bearer ${API_KEY}`
        }
    });

    jsonResponse = JSON.parse(response);
    let fileId = jsonResponse.id;
    Logger.log(fileId);
    return fileId;
};


```

```json Response Example
{"success":true}
```

# Configure Menu Item

<!-- javascript@4-10 -->

Configure the top line menu items within the Google sheet.

# Sync Function

<!-- javascript@12-28 -->

Execute the steps needed for the sync function.

# Prompt for configuration

<!-- javascript@30-41 -->

Prompt for API Key and App Account ID.

# Create the JSON payload

<!-- javascript@43-201 -->

Create JSON payload for users, licenses and usage using the data in the sheet.

# Sync Integration

<!-- javascript@203-217 -->

Function to call the Sync Integration API endpoint - https://developers.toriihq.com/reference/sync-integration

# Upload File

<!-- javascript@226-236 -->

Function to upload the JSON file - makes a request to the upload JSON endpoint.  https://developers.toriihq.com/reference/upload-file-with-users-and-licenses

# Get File ID

<!-- javascript@219-224 -->

Request to get the file ID.  Uses the Get file ID endpoint.  https://developers.toriihq.com/reference/get-file-id