A Google Cloud Function Microservice Cookbook

“Most martial artists want to know how a technique is done. A seasoned Sensei will demonstrate why.”

Soke Behzad Ahmadi

In An AWS Lambda Microservice Cookbook, I took you through the steps required to create a Lambda function that was ultimately integrated into an Avaya Virtual Agent agent. Realizing that AWS isn’t the only game in town for serverless software modules, I wanted to do the same for Google Cloud Functions. This article is the result of that endeavor.

The high level steps to create a Google Cloud function are as follows:

  • Write your code in the language of your choice. This code consists of the business logic of your integration. For example, if all you need to do is pull data from a database, the code might be limited to some SQL (e.g. “Select * From Table Where id=xyz”), packaging the returned dataset into a JSON object, and sending the object back to the API caller.
  • Create a zip file. The zip contains your code, any external libraries referenced by your code, and a package.json file.
  • Create a Google Cloud Function. At the end of this step, Google provides you with a public link for the function.
  • Upload your zip file into the function. This connects your code with the public link. Call the link and you call your code.

Allow me to unpack these steps by creating a Google Cloud Function that can be invoked by Avaya’s Virtual Agent to retrieve the current Bitcoin price in various currencies.

Write Your Code

My function will use a Coinbase REST API to obtain the current Bitcoin price. For a price in United States dollars, you call:


A successful invocation returns data similar to this:


  “data”: {






To prepare the environment, create a “gcp-btc” directory on your PC. Within that directory, launch PowerShell and run:

  • npm init
  • npm install request-promise

“npm init” creates a package.json file. Take the default for “main” and name your node.js file index.js.

“npm install request-promise” is required since you will be using the request-promise package to invoke the Coinbase API. This not only updates my package.json file to add request-promise as a dependency, it creates a node-modules directory under gcp-btc. This directory contains all the code necessary to use request-promise.

You can now write the code for index.js. Note that the programming logic is contained in the exports.btc block and any parameters passed to the function are contained in req.body. In my case, I will eventually invoke the function from Avaya Virtual Agent as a REST POST with a JSON body containing:

{“UserInput” : {“value” : “2”}}

In Virtual Agent, the “UserInput” object is used to pass caller entered DTMF digits in the “value” field. In the above example, the caller pressed a 2. If you look into the code you will find that:

1 == United States dollars

2 == British pound sterling

3 == Canadian dollars

4 == Mexican pesos

After the Bitcoin price for the requested currency is found, the function returns the following JSON object to the calling application:


    “amount”: “17015 pound sterling and 89 pence”


const request = require('request-promise');
const COINBASE_URL = "https://api.coinbase.com/v2/prices/spot?currency=";

exports.btc = async (req, res) => {
	var body;
	var virtualAgentResult = {
		amount: null
	var currency;
	if (req.body.UserInput.value == "1") {
		currency = "USD";
	} else if (req.body.UserInput.value == "2") {
		currency = "GBP";
	} else if (req.body.UserInput.value == "3") {
		currency = "CAD";
	} else if (req.body.UserInput.value == "4") {
		currency = "MXN";
	var url = COINBASE_URL + currency;
	var options = {
		'method': 'GET',
		'url': url,
		'headers': {
			'Accept': 'application/json'
	try {
		var coinbaseResponse = await request.get(options, function(e , r , body) {
		var jsonResponse = JSON.parse(coinbaseResponse);
		// Massage amount information for the Virtual Agent prompt
		var amount = jsonResponse.data.amount.split(".");
		var newAmount;
		if (req.body.UserInput.value == "4") {
			newAmount = amount[0] + " pesos and " + amount[1].substring(0, 2) + " centavos";
		} else if (req.body.UserInput.value == "2") {
			newAmount = amount[0] + " pound sterling and " + amount[1].substring(0, 2) + " pence";
		} else {				
			newAmount = amount[0] + " dollars and " + amount[1].substring(0, 2) + " cents";
		virtualAgentResult.amount = newAmount;
		body = JSON.stringify(virtualAgentResult);
	} catch (e) {
		body = JSON.stringify(virtualAgentResult);

Create a Zip File

Now that the code has been written, you will have a directory structure that looks like this:

Zip up everything except package-lock.json into a single file. This file will be loaded into the Google Cloud Function in the fourth step. You can name the zip file anything you want.

Create Google Cloud Function

To create a Google Cloud Function, go to your Google console and launch Cloud Functions or click this link. You will be presented with a screen similar to this. Click “Go to console.”

Once you are in the console, click on “Create Function.”

Give your function a name. I chose “coinbase.” Set the Trigger type to HTTP and “Allow unauthenticated invocations.”

Click Save and Next.

You are now ready to upload your code. Select “ZIP Upload” and set the “Entry point” to the entry point in your code. The entry point for my code above is “btc.” Next, browse to your zip file.

Your zip file will be loaded into a Stage bucket. If you don’t already have a bucket, click Browse and create a new one.

You are now ready to click “Deploy.”

Once your code has successfully deployed, you will see something similar to this:

Click on the name (e.g. “coinbase”) to see the details of the function. To invoke the function you need the Trigger URL.

You are now ready to give your function a whirl. To test my function, I invoked it from Postman. Notice that the POST URL is the function’s Trigger URL. Also, since my ultimate goal is to invoke this function from a Virtual Agent, I encoded the Body parameters in the format Virtual Agent sends for caller entered DTMF.

Click Send and the function is invoked. Since I did everything right, I received the response I expected.

Add Function as Virtual Agent Integration

This is a bonus step.

Since my goal was to create a Google microservice that can be invoked by Avaya Virtual Agent, I created a new Virtual Agent and added the Google Function URL as the integration in a VA node. Note that the integration is called as an “After action” Sequence. This allows the prompt to be played and the single digit collected before the Google function is called.

When that node is hit in the Virtual Agent flow, the caller will be asked for a currency type (1, 2, 3, or 4), Virtual Agent will invoke the Google Cloud Function, and in the Valid node, the caller will hear the Bitcoin price in dollars, pound sterling, or pesos.

Pretty cool, isn’t it? No Linux server nor having to write a node.js Express application to house the function call. With serverless microservices, my programming life has become much easier!

Mischief Managed

I gave you a lot to chew on, but if you follow these steps you can have your very own Google Cloud Function, too. Of course, I hope you consider using your functions when building Avaya Virtual Agents, but the sky is the limit as to where microservices are useful.

Happy Programming!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: