GAPI — The Google APIs Client Library for Browser JavaScript

Grant Timmerman
Sep 3, 2019 · 4 min read

One of the easiest ways to use an API is by not having a server at all. Some applications are perfect for client-side-only code. The GAPI library offers a simple, flexible way to call Google APIs from your browser.

In this blogpost, you’ll learn a bit about the GAPI library, how to get started, and how to use features like JavaScript Promises and Google OAuth.

What is GAPI?

Google offers a set of client libraries for calling Google APIs in a variety of languages like Python, Java, and Node.

GAPI is Google’s client library for browser-side JavaScript. It’s used in Google Sign-in, Google Drive, and thousands of internal and external web pages for easily connecting with Google APIs.

First launched at Google I/O in 2012 to provide easy Google identity and API access, the client library is now used for thousands of domains and currently handles billions of Google API queries every day.

Getting Started with GAPI

The beautiful part about GAPI is that you do not need to download any library to use the API client. In an HTML file, simply include this script tag:

<script src=""></script>

At this URL, you get a minified version of GAPI (minified via the Closure Compiler).

A full index.html may look like this:

We can serve this HTML file locally using python -m SimpleHTTPServer.

…or use Node: npm install http-server -g && http-server -p 8000.

If we open localhost:8000, we’ll see a blank page that silently loads the API client with no javascript errors:

We’ve got our “Hello, World” webpage up! Now let’s use this gapi client.

Making your First GAPI Request

To make a request in GAPI, let’s create a JS file.

In our HTML, add this line in the body:

<script src="script.js"></script>

Then create a new file script.js with the following contents:

In this piece of code, we’re:

  1. Loading the gapi client
  2. Initializing the client with our Google API Discovery Document
  3. Make an API request
  4. Handle the API response results

We’re essentially looking for all Google APIs related to Android. Our webpage will have this response:

There are 4 discoverable APIs related to Android.


For many APIs, you’ll need to properly authenticate with the Google API before making a request. For example, making an API request using the Google Sheets API. If you don’t authenticate, you’ll see this error in your API response:

The error code you’ll see when making an unauthenticated request.

Google APIs that connect to user data use OAuth 2 for authentication.

Google Docs API + GAPI

Let’s get into something more advanced by using the Google Docs API. The Google Docs API allows you to read and write Docs programmatically. I think it’s a great API for GAPI for quickly building applications backed by Docs.

It’s free to use, and by default you’re allowed 300 read and 60 write queries every minute per user.

Let’s walk through how to enable the API:


In your index.html file, copy the following script:

Enable the Google Docs API

Then, create both a Client ID and API Key for your API client by clicking on the two blue buttons on this Google Cloud Console page:

Turn on the Google Docs API

Create Credentials

For this application, we’ll create an API Key for the Google Docs API, and an OAuth client ID for OAuth identification.

We’ll use these two values and paste them in our application here:


First, go to and Create credentials:

  1. First, create an API key. Don’t select the Restrict Key option (although, you would want to in production.
  2. Second, create an OAuth client ID. We want a Web Application. Under Authorized JavaScript origins, enter http://localhost. Again, this is a way to increase security for your API requests. Copy the client ID and ignore the client secret.

Once you’ve pasted them, try loading the webpage again and press the Authorize button. You should go through a Google OAuth prompt and see a request being made to the Google Docs API!

Wicked! We successfully called a Google API with the GAPI client!

Google Cloud - Community

A collection of technical articles published or curated by Google Cloud Developer Advocates. The views expressed are those of the authors and don't necessarily reflect those of Google.

Grant Timmerman

Written by

Google • Explorer

Google Cloud - Community

A collection of technical articles published or curated by Google Cloud Developer Advocates. The views expressed are those of the authors and don't necessarily reflect those of Google.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade