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

# Bulk Update App Owners via API

```javascript AppScript
let API_KEY;

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

  if (!API_KEY) {
    readConfig();
    docProperties = PropertiesService.getDocumentProperties();
    API_KEY = docProperties.getProperty("api_key");
  }

  syncAppOwners();
}

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

  let docProperties = PropertiesService.getDocumentProperties();
  docProperties.setProperty("api_key", API_KEY);
}

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

  data.forEach(function (col) {
    let application = col[0];
    let email = col[1];
    let appCount = null;
    let userCount = null;

    let jsonResponse = JSON.parse(searchApps(application));
    appCount = Object.keys(jsonResponse.apps).length;
    console.log("AppCount " + appCount);

    let idApp = null;
    let idUser = null;

    if (appCount >= 2) {
      //Use Exact Name Match to Get AppID
      let jsonResponse = JSON.parse(listApps(application));
      Logger.log(jsonResponse);
      idApp = jsonResponse.apps[0].id;
      console.log(idApp);
    } else if (appCount == 1) {
      idApp = jsonResponse.apps[0].id;
      console.log(idApp);
    } else {
      console.log("App " + application + " not found");
    }
    //Get the ID of the user
    if (idApp) {
      //Verify App is in Org before we proceed
      if (getAppsInOrg.indexOf(idApp) != -1) {
        console.log("Application is in Org " + idApp);

        //GET USER ID
        //First Check if the email is blank
        if (email){   
        console.log ("email: " + email);     
        let jsonResponse = JSON.parse(getUser(email));
        Logger.log(jsonResponse);
        
        userCount = Object.keys(jsonResponse.users).length;

        if (userCount == 1) {
          idUser = jsonResponse.users[0].id;
          console.log(idUser);
        } else {
          console.log("User " + email + " not found");
        }
        }

        updateAppWithOwner(idApp, idUser);
       
      } //EndidApp
    } else {
      console.log("Application is Not in Org " + idApp);
    }
  });
}

function searchApps(App_Name) {
  let url = "https://api.toriihq.com/v1.0/apps/search?q=" + App_Name;
  let response = UrlFetchApp.fetch(url, {
    muteHttpExceptions: false,
    method: "get",
    contentType: "application/json",
    headers: { Authorization: `Bearer ${API_KEY}` },
  });
  Logger.log(response);

  return response;
}

function listApps(App_Name) {
  let url = "https://api.toriihq.com/v1.0/apps?fields=id&name=" + App_Name;
  let response = UrlFetchApp.fetch(url, {
    muteHttpExceptions: false,
    method: "get",
    contentType: "application/json",
    headers: { Authorization: `Bearer ${API_KEY}` },
  });
  Logger.log(response);

  return response;
}

function getAllAppsInOrg() {
  let allApps = [];
  let url = "https://api.toriihq.com/v1.0/apps?fields=id";
  console.log("Start get Apps");
  let response = UrlFetchApp.fetch(url, {
    muteHttpExceptions: false,
    method: "get",
    contentType: "application/json",
    headers: { Authorization: `Bearer ${API_KEY}` },
  });
  let jsonResponse = JSON.parse(response);

  for (var i = 0; i < Object.keys(jsonResponse.apps).length; i++) {
    allApps.push(jsonResponse.apps[i].id);
  }
  return allApps;
}

function getUser(Email) {
  let url = "https://api.toriihq.com/v1.0/users?email=" + Email;
  let response = UrlFetchApp.fetch(url, {
    muteHttpExceptions: false,
    method: "get",
    contentType: "application/json",
    headers: { Authorization: `Bearer ${API_KEY}` },
  });
  Logger.log(response);

  return response;
}

function updateAppWithOwner(App_ID, User_ID) {
  let url = "https://api.toriihq.com/v1.0/apps/" + App_ID;
  response = UrlFetchApp.fetch(url, {
    muteHttpExceptions: false,
    method: "put",
    contentType: "application/json",
    payload: JSON.stringify({
      owner: User_ID,
    }),
    headers: {
      Authorization: `Bearer ${API_KEY}`,
    },
  });
```

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

# Configure Menu Item

<!-- javascript@3-9 -->

Configure the top line menu items in the Google Sheet.

# Function to populate variable for API key and sync

<!-- javascript@11-22 -->

When the Sync Now menu item has been selected prompt for the Torii API key and then call syncAppOwners() .

# Function to configure prompt for API key.

<!-- javascript@24-31 -->

Configure the prompt for the API key.

# syncAppOwners Method

<!-- javascript@33-96 -->

Method that takes the data from the spreadsheet, and processes it into JSON that is used in the API calls to the Torii platform.

# Search Apps API Call

<!-- javascript@98-109 -->

Make a request to the search apps endpoint using a search parameter that contains the app name from the Google Sheet.  Try the API call in the explorer - https://developers.toriihq.com/reference/search-apps

# Get Apps API Request

<!-- javascript@111-122 -->

Make a request to the get apps endpoint filtering by app names from the Google Sheet.  Try the API call in the explorer - https://developers.toriihq.com/reference/list-apps