Call Google Cloud function from Vercel serverless hosting.

Recently I’ve been working with several different google cloud services especially google cloud functions.

With AWS Lamda, Google Cloud Functions, MongoDB Stitch and Vercel serverless (just to name a few) in the market, it’s not uncommon that we would be mixing around the different services and calling each other’s functions.

The Painful Google Credentials initialization

While working with NextJS hosted on Vercel, I needed to call my google cloud functions to send out emails (since Vercel doesn’t allow SMTP services from their functions). You may have stumble across this article because of these questions.

  1. How to add GOOGLE_APPLICATION_CREDENTIAL to Lamda
  2. How to add GOOGLE_APPLICATION_CREDENTIALS to Vercel and NextJS applications
  3. How to add service account credential to serverless functions.
  4. How to add service account credentials to .env file
  5. How to access google service account credentials json from Lamda / Vercel.

It’s a pain isn’t it? And the countless number of google documents is not helping either.

There are many solutions available online, but should all else fail, you may want to try this simple solution.

Javascript / NodeJS Google SDK Initialization

The normal method to initialize your google credentials is to export your service account key like this

export GOOGLE_APPLICATION_CREDENTIALS="[FILE_NAME].json"

The alternate way if you host it on a physical server is like this

var projectId = '<Project-ID>';
var keyFilename = '/home/username/project/keyfile';
new GoogleAuth({projectId, keyFilename});

Where both projectId, and keyFilename are strings and keyFilename is the path to your json file. However both methods doesn’t really work in a serverless environment. (there are ways….. just complicated.)

I use the following method, which is not documented much in google sdk documentations.

const url =  ‘https://<region>-<project-id>.cloudfunctions.net/app’;const {GoogleAuth} = require(‘google-auth-library’);
var file = require(‘./your-service-account-key.json’)
export default async (req, res) => {const auth = new GoogleAuth({credentials: {
client_id: file.client_id,
client_email: file.client_email,
project_id: file.project_id,
private_key: file.private_key
}
});
const client = await auth.getIdTokenClient(targetAudience);
const response = await client.request({url});
console.log(‘response from function’, response)
res.send(‘OK’)
}

The above code allows me to call my function url from my Vercel deployment.

  • file=require(path) is crucial because it make sure your file is being uploaded together with your function.
  • Google() if you have GOOGLE_APPLICATION_CREDENTIAL set in your env variable then you do not need to enter any parameters. However since we are in a serverless environment. We can create the credential without setting the parameter by setting the credential object with the client_email, project_id, private_key and client_id.

That’s it, those are the 4 most important things you need to set up your credentials. Some sdk may require you to add in the scope, just add in if you need them. The sdk will throw a error if you miss out something.

The above should be sufficient for initialising of your credentials to google services without the chore of setting environment variables, or uploading of your key.json file in a serverless environment.

Disclaimer: I’m not sure how secure is it, but it works for me.

Hope you found this and save you some variable time. It took me a few days to go through all the documentations, and I have to say… google have very bad documentations.

--

--

--

entrepreneur, developer, dream chaser

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Advanced Redux in Xamarin Part 2: Persistent Actions Middleware

Next.js -12

Gatsby.js, React and GraphQL for documentation websites

JWT Authentication in JavaScript in 5 Minutes

Connecting to Azure blob storage from React js

Let’s know more about React — benefits, demand, salary & other topics

RAIL: A Framework to measure performance

Javascript types

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Dave Teu

Dave Teu

entrepreneur, developer, dream chaser

More from Medium

Create a URL Shortener for personal use using CloudFlare worker in 5 min.

Migrating contents from ACF fields to the Gutenberg

How to integrate Wix with Cloudflare

Nano syntext highligh