How to Use the Box API with Google Apps Script

Published in: Google Apps Script

You can use the OAuth2 library with Google Apps script to connect to the Box API. The Box API is available to all users and provide full access (read and write) to your Box account.

To get started, create a new Box application at and set the redirect_uri to your Google Apps Script project. Next include the OAuth2 library in your Google Script and call authorizeBox() from the Run menu to authorize the connection between your Google Account and Box account.

The getFolderList method will logs a list of all folders that are in the root folder of your Box account.

// Written by Amit Agarwal

// Step 1.
function authorizeBox() {
  var service = getBoxService_();
  if (!service.hasAccess()) {
    var authorizationUrl = service.getAuthorizationUrl();
    Logger.log('Open the following URL to authorize: %s',
  } else {
    Logger.log('Your account is already authorized');

// Step 2.
function getFoldersList() {

  var response = UrlFetchApp.fetch(',type', {
    headers: {
      'Authorization': 'Bearer ' + getBoxService_().getAccessToken()

  var result = JSON.parse(response.getContentText());
  var items = result.entries;

  var folders = [];

  for (var i=0; i<items.length; i++) {
    if (items[i].type === "folder") {
      folders.push({name: items[i].name, id: items[i].id});


* Configures the service.
function getBoxService_() {
  return OAuth2.createService('Box')

* Handles the OAuth callback.
function authCallback(request) {
  var service = getBoxService_();
  var authorized = service.handleCallback(request);
  if (authorized) {
    return HtmlService.createHtmlOutput('Your Google account is now connected to Box');
  } else {
    return HtmlService.createHtmlOutput('Sorry, the connection to Box was denied');
📮  Subscribe to our Email Newsletter for Google tips and tutorials!
Published in: Google Apps Script

Looking for something? Find here!

Meet the Author

Web Geek, Google Developer Expert
Amit Agarwal

Amit Agarwal is a Google Developer Expert in Google Workspace and Google Apps Script. He holds an engineering degree in Computer Science (I.I.T.) and is the first professional blogger in India. He is the developer of Mail Merge for Gmail and Document Studio. Read more on Lifehacker and YourStory

Get in touch

Google Add-ons

Do more with your Gmail and GSuite account

We build bespoke solutions that use the capabilities and the features of Google Workspace for automating business processes and driving work productivity.

  1. Mail Merge with Attachments
    Send personalized email to your Google Contact with a Google Sheet and Gmail
  2. Save Emails and Attachments
    Download email messages and file attachments from Gmail to your Google Drive
  3. Google Forms Email Notifications
    Send email notifications to multiple people when a new Google Form is submitted
  4. Document Studio
    Create beautiful pixel perfect documents merging data from Google Sheets and Google Forms
  5. Creator Studio for Google Slides
    Turn your Google Slides presentations into animated GIFs and videos for uploading to YouTube