API to post your Front-end data to Google sheets using Node script

API Test Demo


  1. Enable the Google Sheet API and get keys
    -> For this, you need to set up a GCP project, once done head over to
    API & Services > Enable APIs & Services > search for Google Sheet API
npm i google-spreadsheet
npm i cors dotenv
import * as functions from "firebase-functions"; // this is for Firebase cloud functionsimport admin from "firebase-admin"; // this is for Firebase cloud functionsconst { GoogleSpreadsheet } = require('google-spreadsheet'); // spread sheet apiimport * as fs from "fs";import cors from "cors";import "dotenv/config"; // HERE I used ES6 Module const corsHandler = cors({origin: true}); // CORS handling admin.initializeApp(functions.config().firebase); // initializing the app
// Initialize the sheet - doc ID is the long id in the sheets URL
const doc = new GoogleSpreadsheet('<the sheet ID from the url>');
export const addSheetRowData = functions.https.onRequest((request, response) => {corsHandler(request, response, async () => {const {rowData} = request?.body;await doc.useServiceAccountAuth({client_email: process.env.GOOGLE_CLIENT_EMAIL ?? "",private_key: process.env.GOOGLE_PRIVATE_KEY ?? "",});await doc.loadInfo();const sheet = doc.sheetsByIndex[0];  // or use doc.sheetsById[id] or doc.sheetsByTitle[title]await sheet.addRow(rowData);});});
firebase deploy --only functions



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