Get Script Project Files

getScriptProjectFiles(scriptId) calls the Apps Script API with ScriptApp.getOAuthToken() and returns the project's files as [{name, type, source}]. It throws a clear error if the API is not enabled, the user-settings toggle is off, or the scope is missing.

When to use

  • Backing up a project's .gs, HTML, and appsscript.json to Drive
  • Feeding Function Inventory For Prompt without a second fetch
  • Reading the current project (ScriptApp.getScriptId()) or another project you can access

How to use this snippet

Enable the Apps Script API in the Cloud project for this script. Turn on the Apps Script API toggle at script.google.com/home/usersettings for the account that runs it. In appsscript.json add https://www.googleapis.com/auth/script.projects.readonly and https://www.googleapis.com/auth/script.external_request. That is a read. The helper does not change the project.

/** * Return this or another project's files as [{name, type, source}] via the * Apps Script API (projects.getContent) and ScriptApp.getOAuthToken(). * scriptId is required (ScriptApp.getScriptId() for the current project). * Throws a clear error if the API is not enabled, the user-settings toggle * is off, or the scope is missing. * * @param {string} scriptId * @return {{name: string, type: string, source: string}[]} */ function getScriptProjectFiles(scriptId) { var id = String(scriptId == null ? '' : scriptId).trim(); if (!id) throw new Error('getScriptProjectFiles: scriptId required'); var url = 'https://script.googleapis.com/v1/projects/' + encodeURIComponent(id) + '/content'; var resp; try { resp = UrlFetchApp.fetch(url, { method: 'get', headers: { Authorization: 'Bearer ' + ScriptApp.getOAuthToken() }, muteHttpExceptions: true }); } catch (e) { throw new Error( 'getScriptProjectFiles: request failed. Enable the Apps Script API, turn on the Apps Script API toggle at https://script.google.com/home/usersettings, and add the script.projects.readonly and script.external_request scopes.' ); } var code = resp.getResponseCode(); var body = resp.getContentText() || ''; if (code < 200 || code >= 300) { throw new Error(formatScriptApiError_(code, body)); } var data; try { data = JSON.parse(body); } catch (e) { throw new Error('getScriptProjectFiles: unexpected response from the Apps Script API'); } var files = data.files || []; return files.map(function (f) { return { name: f.name || '', type: f.type || '', source: f.source == null ? '' : String(f.source) }; }); } function formatScriptApiError_(code, body) { var message = ''; try { var parsed = JSON.parse(body); if (parsed && parsed.error) { message = parsed.error.message || parsed.error.status || ''; } } catch (e) {} var hint = ' Enable the Apps Script API in the Cloud project, turn on the Apps Script API toggle at https://script.google.com/home/usersettings, and use the script.projects.readonly scope.'; if (code === 403 || /not been used|not enabled|PERMISSION_DENIED|ACCESS_DENIED/i.test(message)) { return ( 'getScriptProjectFiles: the Apps Script API is not enabled or the script cannot access this project (' + code + (message ? ': ' + message : '') + ').' + hint ); } return ( 'getScriptProjectFiles: Apps Script API request failed (' + code + (message ? ': ' + message : '') + ').' + hint ); }

Example

function backupScriptToDrive() { var files = getScriptProjectFiles(ScriptApp.getScriptId()); var folder = DriveApp.getFolderById('BACKUP_FOLDER_ID'); var stamp = Utilities.formatDate(new Date(), Session.getScriptTimeZone(), 'yyyy-MM-dd'); var blobs = files.map(function (f) { var ext = f.type === 'JSON' ? '.json' : f.type === 'HTML' ? '.html' : '.gs'; var name = f.name; if (name.slice(-ext.length) !== ext) name += ext; return Utilities.newBlob(f.source, 'text/plain', name); }); folder.createFile(Utilities.zip(blobs, 'script-backup-' + stamp + '.zip')); }

SERVER_JS files are .gs, HTML is .html, and JSON is usually appsscript.json.

Notes

  • A 403 almost always means the API is not enabled, the user-settings toggle is off, or script.projects.readonly is missing from the manifest.
  • The token is used only to read project content. This helper does not update or deploy the script.
  • You can only read projects the running account already has access to.

Happy Coding!

NitroGAS Chrome Extension

Want this snippet handy inside the Apps Script editor? NitroGAS gives you free snippets — plus optional Co-Pilot when you want a boost (Day Pass — $3, or 1-week, 1-month, or yearly passes — no subscription). Happy Coding!

Get the Extension