Quickstarts explain how to set up and run an app that calls a
Google Workspace API. This quickstart uses a
simplified authentication approach that is appropriate for a testing
environment. For a production environment, we recommend learning aboutauthentication and authorizationbeforechoosing the access credentialsthat are appropriate for your app.
In Apps Script, Google Workspace
quickstarts useAdvanced Google servicesto call
Google Workspace APIs and handle some details of the authentication
and authorization flow.
/*** Lists users in a Google Workspace domain.* @see https://developers.google.com/admin-sdk/directory/reference/rest/v1/users/list*/functionlistUsers(){constoptionalArgs={customer:'my_customer',maxResults:10,orderBy:'email'};try{constresponse=AdminDirectory.Users.list(optionalArgs);constusers=response.users;if(!users||users.length===0){console.log('No users found.');return;}// Print the list of user's full name and emailconsole.log('Users:');for(constuserofusers){console.log('%s (%s)',user.primaryEmail,user.name.fullName);}}catch(err){// TODO (developer)- Handle exception from the Directory APIconsole.log('Failed with error %s',err.message);}}
Click Save.
ClickUntitled project, typeQuickstart, and clickRename.
Configure the script
Enable the Directory API
Open the Apps Script project.
ClickEditorcode.
Next toServices, click Add a serviceadd.
Select
Admin Directory API
and clickAdd.
Run the sample
In the Apps Script editor, clickRun.
The first time you run the sample, it prompts you to authorize access:
ClickReview permissions.
Choose an account.
ClickAllow.
The script's execution log appears at the bottom of the window.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-09-04 UTC."],[],[],null,["Create a\n[Google Apps Script](/apps-script/overview)\nthat makes requests to the Directory API.\n\nQuickstarts explain how to set up and run an app that calls a\nGoogle Workspace API. This quickstart uses a\nsimplified authentication approach that is appropriate for a testing\nenvironment. For a production environment, we recommend learning about\n[authentication and authorization](/workspace/guides/auth-overview)\nbefore\n[choosing the access credentials](/workspace/guides/create-credentials#choose_the_access_credential_that_is_right_for_you)\nthat are appropriate for your app.\n\nIn Apps Script, Google Workspace\nquickstarts use\n[Advanced Google services](/apps-script/guides/services/advanced) to call\nGoogle Workspace APIs and handle some details of the authentication\nand authorization flow.\n\nObjectives\n\n- Configure the environment.\n- Create and configure the script.\n- Run the script.\n\nPrerequisites\n\n\n- A Google Workspace domain with [API access enabled](https://support.google.com/a/answer/60757).\n- A Google Account in that domain with administrator privileges.\n\n\u003cbr /\u003e\n\n- Access to Google Drive\n\nCreate the script\n\n1. Create a new script in the Apps Script editor by going to [script.google.com/create](https://script.google.com/create).\n2. Replace the contents of the script editor with the following code:\n\n\nadminSDK/directory/quickstart.gs \n[View on GitHub](https://github.com/googleworkspace/apps-script-samples/blob/main/adminSDK/directory/quickstart.gs) \n\n```javascript\n/**\n * Lists users in a Google Workspace domain.\n * @see https://developers.google.com/admin-sdk/directory/reference/rest/v1/users/list\n */\nfunction listUsers() {\n const optionalArgs = {\n customer: 'my_customer',\n maxResults: 10,\n orderBy: 'email'\n };\n try {\n const response = AdminDirectory.Users.list(optionalArgs);\n const users = response.users;\n if (!users || users.length === 0) {\n console.log('No users found.');\n return;\n }\n // Print the list of user's full name and email\n console.log('Users:');\n for (const user of users) {\n console.log('%s (%s)', user.primaryEmail, user.name.fullName);\n }\n } catch (err) {\n // TODO (developer)- Handle exception from the Directory API\n console.log('Failed with error %s', err.message);\n }\n}\n```\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n3. Click Save .\n4. Click **Untitled project** , type **Quickstart** , and click **Rename**.\n\n\u003cbr /\u003e\n\nConfigure the script\n\nEnable the Directory API\n\nOpen the Apps Script project.\n\n1. Click **Editor** code.\n2. Next to **Services** , click Add a service add .\n3. Select Admin Directory API and click **Add**.\n\nRun the sample\n\nIn the Apps Script editor, click **Run**.\n\nThe first time you run the sample, it prompts you to authorize access:\n\n1. Click **Review permissions**.\n2. Choose an account.\n3. Click **Allow**.\n\nThe script's execution log appears at the bottom of the window. \ndone It worked! **Great!** Check out the further reading section below to learn more.\nwarning There was a problem **Bummer** , [let us know what went wrong](#). Check out our [troubleshooting](#troubleshooting) section below for some common errors and solutions. If you have found a bug in the code, [report the issue on GitHub](https://github.com/googleworkspace/apps-script-samples/issues) or submit a pull request.\n\nNext steps\n\n- [Google Apps Script Advanced Services documentation](/apps-script/guides/services/advanced)\n- [Try the Google Workspace APIs in the APIs explorer](/workspace/explore)\n- [Troubleshoot authentication and authorization issues](/workspace/admin/directory/v1/guides/troubleshoot-authentication-authorization)\n- [Directory API developer guides](/workspace/admin/directory/v1/guides)\n- [Directory API reference documentation](/workspace/admin/directory/v1/reference)"]]