Build a Simple Microservice Google Review API through Heroku

Hanwen Zhang
3 min readJul 16, 2022

--

Microservice architecture has been the trend in software engineering that creates a small independent service while enabling powerful API calls.

In this article, I will extend my last article Get Google Review API and Display the Rating and Reviews on Your Website to show how to build a Google Review API, use it, and deploy it to Heroku.

Google has pretty much already written the basic setup code for us, please check the Place Details example for the initial setup. Since this API link is for the backend, I will create my Google Review API using JavaScript runtime Node, and its framework Express to build the endpoint.

Code in my App.js that builds the simple Google Review API
Make sure you have installed the required dependencies in your app

BOOM! That’s all, you can deploy to the Heroku now and get the place information on Google when you hit the correct URL

const url = `https://{nameOfTheApp}.herokuapp.com/reviews/${ID}`;

Heroku Setup and Deployment

Initial Setup

First, make sure you signed up for an account at Heroku and created a project.

Procfile

Procfile is the file that gives instructions to Heroku on how to run your app.

touch Procfile

Create a Procfile. For my simple app, I just need to add a line of code

web: node app.js

Deployment

Three ways to deploy your code to Heroku

  1. CLI — Command Line Interface
heroku git:remote -a “{nameOfTheApp}”
git push heroku master
heroku open

2. Connect to your GitHub Repository (Personally Preferred)

3. Connect to Container Registry

Config Vars

Very important!!! Don’t forget to add your API Key (the environment variable) onto Heroku through the Settings section.

Finally

After deploying this microservice API to Heroku, just hit the API in your project with the Place ID to get the corresponding review data.

https://{nameOfTheApp}.herokuapp.com/reviews/${ID}

Below is the response JSON data I got from the API.

I modified my data structure when I did the screenshot, it should return the result object only in my code.

Optional: Enabling CORS

In order to avoid people using your API across different URLs since you do not want to pay for their services, make sure you add the Access-Control-Allow-Origin in your app.

Install express and cors and set up Access-Control-Allow-Origin in your app.

npm install express corsconst express = require('express');
const cors = require('cors');
const app = express();
app.use(cors());
app.use(cors({
origin: 'http://yourapp.com'
}));

--

--

Hanwen Zhang

Full-Stack Software Engineer at a Healthcare Tech Company | Document My Coding Journey | Improve My Knowledge | Share Coding Concepts in a Simple Way