How I Built a REST API Using Google Sheets
By: Tek Loon
Tags: JavaScript, Serverless, Software Engineering, Google Cloud Platform, Programming

Details for Selected Steps

Step 2. Create Node project and install Express framework

mkdir node-push-up-tracker // Change directory to the created project folder

cd node-push-up-tracker // Create Node Project

npm init -y // Install Express Framework

npm install express

Step 3. Write the business logic and create the API endpoint

We will create two endpoints in this step. The first endpoint is to retrieve today’s push-ups while the second endpoint is to retrieve the push-ups within the date range.

Step 5. Perform authentication using googleapis and retrieve Google Sheets data

We will create the pushupService.js that’s responsible for retrieving Google Sheets data. But before that, we will have to perform authentication using the googleapis library.

// Install Googleapis dependency

npm install googleapis

Step 6. Create the basic Dockerfile on how to start your application

In this step, I copy the basic Dockerfile on how to start the Node application.

Step 7. Build your container image and submit it to Cloud Run

In this particular step, we will build the container image and submit it using the gcloud command. If you have yet to install gcloud, please refer to the installation guide.

Before we start, you’ll have to know your Google Project ID. Go to Google Cloud Console and select your project. You’ll see the Project ID in the Project Info section. Refer to the screenshot below.

Get your Project ID in the Project Info section

Let’s build the container image now and submit it to Cloud Run. I have written a bash script for the build process to avoid having to repeatedly type the build command (which I am always forgetting so always end up referring to the documentation again).

Run the build script via sh build.sh .

What this build script does is:

It will assign your Google Project ID to the GOOGLE_PROJECT_ID variable and use this variable to identify which project this container image belongs to.

variable and use this variable to identify which project this container image belongs to. You will have also named the container image under CONTAINER_IMAGE_NAME variable. Thus, if you're using the same container image name, each build process will generate a revision for you. Refer to the screenshot below where I have two revisions since I submitted the built image twice.

I have two revisions of images

Step 8. Deploy your container

Let’s deploy the container image you have built and submitted. For your information, you can actually deploy the container image using the gcloud command too. However, I would like to show how you can deploy the service using the Google Cloud Platform website — it’s easy.

Go to your Google Cloud Console and select the project that you have created. Click the Navigation Menu, and then click Cloud Run within the navigation menu. (You can also refer to the red box in the screenshot below.)

Cloud Run on Navigation Menu

3. You will see the Cloud Run Services screen, where you can create the service using the container image you have built. Each Cloud Run service has a unique endpoint.

4. Let’s create a new service by clicking Create Service.

Create Service

5. Fill in the input fields (deployment platform, service name, and authentication) and then click Next. You can refer to the example below.

Example of how you can fill up

6. Select the container image you have built by clicking Select. You should see a list of built container images.

7. Before you click Create, click the Variables tab to add environment variables, if you have any. In my use case, I would need to add SHEET_ID and TAB_ID so it knows which spreadsheets and which tab it should retrieve the data from.

Adding Environment Variable at Variables Tab

8. Click Create, and your service will be created and deployed. You will see the screen below if your service is successfully created and deployed. You can verify by calling the API endpoint you created with the URL.

The service is ready now!!

Step 9. Grant permission to the compute engine to access your Google Sheets

If you have followed the steps to here and tried to call the API, you might get “permission denied” when trying to access Google Sheets. By default, you’re the only one who can access your Google spreadsheets due to security and privacy concerns.

Copy the compute account number that you found in the Permissions tab and add it to the spreadsheets sharing. Now you will be able to successfully retrieve the spreadsheet data. Refer to the screenshot below.

Copy compute account member

Share your spreadsheet to the compute account

Now you should have a working Express application, you should be able to retrieve the total push-ups from spreadsheets.

Here is the screenshot where I successfully retrieved my push-up data.