DEV Community

Prakash Pawar
Prakash Pawar

Posted on

How to Add Your Inquiry API Responses into Google Sheets (Automatic, No Duplicates)

Have an API that collects customer inquiries, leads, or form submissions—and want those responses to show up automatically inside a Google Sheet?

This tutorial shows you exactly how to:

  • Fetch API responses into a sheet
  • Format data correctly (columns → values)
  • Prevent duplicate entries by id
  • Auto-refresh with a time trigger
  • Keep pagination metadata updated

Perfect for dashboards, lead management, or syncing external APIs with Google Workspace.


✅ Step 1 — Create a New Google Sheet

Open a new sheet and name it something like:

Inquiries
Enter fullscreen mode Exit fullscreen mode

✅ Step 2 — Open Apps Script

Go to:

Extensions → Apps Script
Enter fullscreen mode Exit fullscreen mode

Delete any existing code.


✅ Step 3 — Paste This Script (Fully Working)

This script fetches your API, inserts formatted rows, avoids duplicates, and updates pagination fields.

function fetchInquiries() {
  const url = "YOUR_API_URL_HERE";  // Example: https://www.example.com/api/v1/inquiry
  const response = UrlFetchApp.fetch(url);
  const json = JSON.parse(response.getContentText());
  const items = json.items;

  const ss = SpreadsheetApp.getActive();
  const sheet = ss.getSheetByName("Inquiries") || ss.insertSheet("Inquiries");

  // ---------- 1. Ensure headers exist ----------
  const headers = ["id", "name", "email", "phone", "whatsapp_number", "created_at"];

  if (sheet.getLastRow() === 0) {
    sheet.appendRow(headers);
  }

  // ---------- 2. Collect all existing IDs safely ----------
  let existingIds = new Set();

  const lastRow = sheet.getLastRow();
  if (lastRow > 1) {
    const idColumnValues = sheet.getRange(2, 1, lastRow - 1, 1).getValues();
    idColumnValues.forEach(row => {
      if (row[0]) existingIds.add(row[0].toString());
    });
  }

  // ---------- 3. Create rows only for NEW inquiries ----------
  const newRows = [];

  items.forEach(item => {
    if (!existingIds.has(item.id)) {
      newRows.push([
        item.id,
        item.name,
        item.email,
        item.phone,
        item.whatsapp_number,
        item.created_at
      ]);
    }
  });

  // ---------- 4. Append only new rows ----------
  if (newRows.length > 0) {
    sheet.getRange(sheet.getLastRow() + 1, 1, newRows.length, headers.length)
         .setValues(newRows);
  }

  // ---------- 5. Update pagination fields (DO NOT duplicate) ----------
  const summaryStart = sheet.getLastRow() + 2;

  sheet.getRange(summaryStart, 1).setValue("total");
  sheet.getRange(summaryStart, 2).setValue(json.total);

  sheet.getRange(summaryStart + 1, 1).setValue("page");
  sheet.getRange(summaryStart + 1, 2).setValue(json.page);

  sheet.getRange(summaryStart + 2, 1).setValue("page_size");
  sheet.getRange(summaryStart + 2, 2).setValue(json.page_size);

  sheet.getRange(summaryStart + 3, 1).setValue("total_pages");
  sheet.getRange(summaryStart + 3, 2).setValue(json.total_pages);
}
Enter fullscreen mode Exit fullscreen mode

Replace:

YOUR_API_URL_HERE
Enter fullscreen mode Exit fullscreen mode

with your actual API endpoint.


✅ Step 4 — Run the Function Manually Once

Click the Run ▶ button.

Google will ask for permissions → click Allow.

Your sheet will now populate with:

  • One row per inquiry
  • Proper column formatting
  • Pagination metadata below the table

✅ Step 5 — Enable Auto-Refresh (Optional but Recommended)

To auto-run every minute, hour, or day:

  1. Open Apps Script
  2. Go to: Triggers
  3. Click Add Trigger
  4. Select:
  • Function: fetchInquiries
  • Event Source: Time-driven
  • Interval: every 1 minute, 5 minutes, or 1 hour

Now your sheet updates automatically—even when it’s closed.


🎯 What This Solves

✔ Eliminates manual exporting and copying

✔ Works as a real-time API → Sheet integration

✔ Prevents duplicate entries (checks by id)

✔ Allows multiple team members to view data instantly

✔ Perfect for dashboards, sales teams, CRMs, automation systems


🔌 Bonus: Extend This Further

Once your data lands in Sheets, you can:

  • Send new inquiries to Slack, Telegram, or WhatsApp
  • Create charts & dashboards
  • Auto-update Notion, Airtable, AppScript APIs
  • Trigger email notifications when a new inquiry arrives

✨ Final Thoughts

Integrating an external API directly into Google Sheets is powerful — especially for startups collecting leads or form submissions. With just a few lines of Apps Script, you can build a lightweight CRM, a reporting dashboard, or even automate your entire inquiry pipeline.


If you have any Query regarding this post let me know in comment or Tweet me. thank you for reading this.

Top comments (0)