A Google Apps Script project that dynamically generates modern, feature-rich client libraries for any public Google API directly from the Google API Discovery Service.
This tool creates libraries with features including flexible authentication, and automatic exponential backoff.
A Google Apps Script project that dynamically generates modern, feature-rich client libraries for any public Google API directly from the Google API Discovery Service.
This generator has been completely revamped to align with the official Google API Node.js Client Libraries, enabling you to:
- Use
async/await: Write cleaner, more readable, and easier-to-maintain asynchronous code. - Write Portable Code: Share the exact same business logic between your Apps Script projects and Node.js environments like Google Cloud Run. This allows you to prototype rapidly in Apps Script and "eject" to a more scalable cloud environment when needed, without a rewrite.
For a deep dive into this powerful new workflow, read the companion blog post: How to Write Portable Apps Script Code for the Cloud.
This repository includes a build/ directory containing auto-generated client libraries for every discoverable Google API.
For most users, you do not need to run the generator yourself. You can find the library you need in the build/ directory.
Call for Contributions: While all libraries are generated, not all have been extensively tested. We welcome and encourage community contributions. If you find a bug or have a suggestion, please open a GitHub Issue. If you have fixes or tests to add, please feel free to submit a Pull Request.
While Google Apps Script has excellent built-in and advanced services, a generated client library offers two critical advantages:
- Complete API Coverage: Apps Script's built-in services only cover a fraction of all Google APIs. This generator allows you to create a client for any service with a Discovery Document, such as Firebase, Cloud Billing, and over 400 more.
- Flexible Authentication Flows: Generated libraries allow you to use a custom OAuth token. This unlocks powerful workflows, such as using your own OAuth clients and service accounts for server-to-server tasks, which is not possible with standard built-in services.
Every API call your script makes must be associated with a Google Cloud Project that has that specific API enabled.
- For Service Accounts & Custom OAuth Clients: You must enable the API in the GCP project that owns your credentials. Your Apps Script project does not need to be linked.
- For Default User Authentication: The API must be enabled either by adding it as an "Advanced Service" (for those that are available) or by linking your script to a standard GCP project where the API has been manually enabled.
For a detailed guide on these setups, please see the official documentation on Standard Google Cloud Platform projects.
- Navigate to the
build/directory in this repository. - Find the folder for the API you want to use and open the specific version directory.
- Add the library code to your project by copying the content from either
[ApiName].with_docs.gs(for development) or[ApiName].gs(for production) into a new script file.
Tip: Providing the
[ApiName].with_docs.gsfile to a conversation in gemini.google.com can help when vibe coding with Gemini.
For service account usage only the https://www.googleapis.com/auth/script.external_request scope is required in the manifest
- From the library folder in the
build/directory, open theappsscript.jsonfile. - Copy the scopes you need from the
"oauthScopes"array into your own project's manifest file (Project Settings βοΈ > Show "appsscript.json" manifest file). - Note: You must always include the
https://www.googleapis.com/auth/script.external_requestscope to allow the library to make API calls.
Instantiate the library by calling new [ApiName](). You can pass a configuration object to the constructor to customize its behavior.
No configuration is needed. The library automatically uses the script's default user token.
// The library uses the script's default OAuth token.
const ytAnalytics = new YoutubeAnalytics();Provide a pre-fetched OAuth token. This is the pattern used for service accounts.
// Prerequisite: You have already obtained a token string.
const myToken = getServiceAccountToken(); // Your function to get the token
const ytAnalytics = new YoutubeAnalytics({
token: myToken
});By default, the library retries on rate limit and server errors. You can disable this to fail fast.
// The library will now fail immediately on 429 or 5xx errors.
const ytAnalytics = new YoutubeAnalytics({
backoff: {
retries: 0
}
});The configuration options can be combined.
// Use a custom token AND disable automatic retries.
const ytAnalytics = new YoutubeAnalytics({
token: myToken,
backoff: {
retries: 0
}
});This detailed walkthrough shows how to get a token using the OAuth2 for Apps Script library and pass it to a generated library. This flow is essential for scripts that run on a trigger or act on behalf of a system.
- Add the OAuth2 for Apps Script library. In your project, go to Libraries + and add it using this Script ID:
1B7FSrk5Zi6L1rSxxTDgDEUsPzlukDsi4KGuTMorsTQHhGBzBkMun4iDF - Create a Service Account in your Google Cloud Platform project. Download the JSON key file. Official Google Guide
// The email of the user to impersonate (for domain-wide delegation).
const USER_EMAIL = 'user-to-impersonate@yourdomain.com';
function getService_() {
// Credentials from the service account's JSON key file.
const serviceAccountCreds = {
"private_key": "-----BEGIN PRIVATE KEY-----\n...",
"client_email": "your-service-account@your-project.iam.gserviceaccount.com",
};
// Use a unique name for the service, like 'Drive' or 'BigQuery', to avoid
// token collisions between different services.
return OAuth2.createService('Drive:' + USER_EMAIL)
.setTokenUrl('https://oauth2.googleapis.com/token')
.setPrivateKey(serviceAccountCreds.private_key)
.setIssuer(serviceAccountCreds.client_email)
.setSubject(USER_EMAIL)
.setCache(CacheService.getUserCache())
.setScope('https://www.googleapis.com/auth/drive');
}
/**
* Lists files using a service account token.
* This function is now async to handle the Promise returned by the Drive library.
*/
async function listFilesWithServiceAccount() {
const service = getService_();
if (!service.hasAccess()) {
console.log('Service Account authentication failed: ' + service.getLastError());
return;
}
const token = service.getAccessToken();
// Instantiate the Drive library with the service account token.
const drive = new Drive({
token: token
});
try {
// Use 'await' to wait for the asynchronous API call to complete.
const response = await drive.files.list({
supportsAllDrives: true,
pageSize: 10
});
// The actual list of files is in the 'data' property of the response.
console.log('Files found via service account:', JSON.stringify(response.data, null, 2));
} catch (e) {
console.error(`Error listing files: ${e.message}`);
}
}If the library you need needs adjusting or or you would like to incorporate this into your own workflows, you can run the generator yourself.
- Create a new, standalone Google Apps Script project at script.google.com. Name it ApiLibraryGenerator.
- Replace the contents of
Code.gswith the complete code from thegenerator/folder in this repository. - Enable the Drive API Advanced Service:
- In the editor, click Services +.
- Find Drive API, select it, and click Add.
- Open the
Code.gsfile in your ApiLibraryGenerator project. - To generate a new library, call
createCompleteApiLibrary()with the desiredapiNameandversion. You can also configure whether to include JSDoc comments.
function generateMyLibrary() {
// Generate YouTube Analytics v2 library with full JSDoc comments
createCompleteApiLibrary('youtubeAnalytics', 'v2', { generateJsdoc: true });
}
- Select your function from the toolbar and click Run.
- Authorize the script. A new Apps Script project file will appear in your Google Drive.
This project was heavily inspired by the pioneering work of Spencer Easton on his Apps-Script-GoogleApis-Libraries repository. This generator aims to build upon that foundation with modern class structures, automated project creation, and additional features.