DEV Community

Roberto Luna
Roberto Luna

Posted on

Implementing Google Sheets Integration for Device Management in tvview

Implementing Google Sheets Integration for Device Management in tvview

 

TL;DR: I integrated Google Sheets with the tvview app, enabling edits to be written back to a sheet for MAC, Model, and Lineup. This involved setting up Google API credentials, modifying the device service, and adding a SheetsWriter provider.

The Problem

The initial problem was to enhance the device management capabilities in the tvview application by integrating Google Sheets. This would allow for seamless editing and synchronization of device information, specifically MAC, Model, and Lineup, directly to a Google Sheet.

What I Tried First

Initially, I focused on setting up the Google API credentials and installing the necessary googleapis package. I added the googleapis package to the project by running:

npm install googleapis
Enter fullscreen mode Exit fullscreen mode

or

"googleapis": "^173.0.0"
Enter fullscreen mode Exit fullscreen mode

in package.json.

The Implementation

Environment Variables and Dependencies

First, I updated .env.example to include Google API credentials:

# --- Google Sheets ---
GOOGLE_SERVICE_ACCOUNT_EMAIL="your-service-account-email@your-project-id.iam.gserviceaccount.com"
GOOGLE_PRIVATE_KEY="your-private-key"
GOOGLE_SPREADSHEET_ID="your-spreadsheet-id"
Enter fullscreen mode Exit fullscreen mode

I also updated package-lock.json and package.json to include the googleapis dependency:

"googleapis": "^173.0.0",
Enter fullscreen mode Exit fullscreen mode

Google Sheets Writer Provider

Next, I created a SheetsWriter.ts provider to handle writing to Google Sheets:

// src/providers/googleSheets/SheetsWriter.ts
import { google } from "googleapis";

export interface SheetWriteResult {
  ok: boolean;
  message?: string;
}

function getAuth() {
  const email = process.env.GOOGLE_SERVICE_ACCOUNT_EMAIL;
  const privateKey = process.env.GOOGLE_PRIVATE_KEY;
  const spreadsheetId = process.env.GOOGLE_SPREADSHEET_ID;

  // Auth logic here
}

export async function writeDeviceFieldsToSheet(deviceId: string, fields: any) {
  // Write logic here
}
Enter fullscreen mode Exit fullscreen mode

Device Service Updates

I then updated device.service.ts to integrate with the new SheetsWriter:

// src/services/device.service.ts
import { prisma } from "@/lib/prisma";
import { isValidMac } from "@/lib/mac";
import { writeDeviceFieldsToSheet } from "@/providers/googleSheets/SheetsWriter";

export async function updateDevice(deviceId: string, fields: any) {
  // Update logic here
  await writeDeviceFieldsToSheet(deviceId, fields);
}
Enter fullscreen mode Exit fullscreen mode

API Route Updates

The API route for device updates was also modified:

// src/app/api/devices/[id]/route.ts
import { NextResponse } from "next/server";
import { updateDevice } from "@/services/device.service";

export async function PATCH(req: Request, { params }: { params: { id: string } }) {
  const { id } = params;
  const { body } = await req.json();

  // Update logic here
  await updateDevice(id, body);

  return NextResponse.json({ message: "Device updated successfully" });
}
Enter fullscreen mode Exit fullscreen mode

Key Takeaway

The key takeaway from this implementation is the importance of properly handling authentication and authorization when integrating with external services like Google Sheets. Ensuring that sensitive credentials are stored securely and used correctly is crucial.

What's Next

Next, I plan to enhance the error handling and logging for the Google Sheets integration. This will involve implementing retries for failed writes and adding more detailed logging to diagnose issues.

vibecoding #buildinpublic #GoogleSheets #DeviceManagement


Part of my Build in Public series — sharing the real process of building SaaS projects from Playa del Carmen, México.

Repo: zaerohell/tvview · 2026-07-09

#playadev #buildinpublic

Top comments (0)