Attendance integrations with webhooks

Attendance to Google Sheets through a webhook

Updated · Author: Hadirly team

Hadirly can add one row to Google Sheets for each attendance record saved. To set it up, create a Google Apps Script that is published as a web app, store a secret token in the script properties, then paste the URL ending in /exec with ?token= into the Hadirly webhook settings. Apps Script cannot read request headers, so the token in the URL is the only guard.

What to prepare

  • One spreadsheet. The script creates a sheet named Attendance if it does not exist.
  • Column titles in row one, in the same order as the script: send time, event_id, user_id, direction (in or out), occurred time, status, local day, and location.
  • A long random token. Create it with a password generator, and do not use your company name.

Step by step

  1. Open the spreadsheet, then choose Extensions > Apps Script.
  2. Delete the starter file contents, then paste the code below. It matches the Google Sheets guide in Hadirly’s integration settings.
JAVASCRIPT
const SHEET_NAME = 'Attendance';

function doPost(e) {
  const expected = PropertiesService.getScriptProperties().getProperty('WEBHOOK_TOKEN');
  if (!expected || (e.parameter.token || '') !== expected) {
    return ContentService.createTextOutput('forbidden');
  }
  // Flat format: envelope fields and data fields are at the top level of the JSON.
  const event = JSON.parse(e.postData.contents);
  if (event.event_type !== 'attendance.recorded') {
    return ContentService.createTextOutput('ignored');
  }
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const sheet = ss.getSheetByName(SHEET_NAME) || ss.insertSheet(SHEET_NAME);
  // A retried delivery carries the same event_id: skip it.
  const last = sheet.getLastRow();
  const ids = last > 1 ? sheet.getRange(2, 2, last - 1, 1).getValues().flat() : [];
  if (ids.indexOf(event.event_id) !== -1) {
    return ContentService.createTextOutput('duplicate');
  }
  sheet.appendRow([
    event.event_created_at, event.event_id, event.user_id, event.direction,
    event.occurred_at, event.status, event.local_day, event.location || '',
  ]);
  return ContentService.createTextOutput('ok');
}
  1. Open Project Settings, then in the Script properties section add WEBHOOK_TOKEN with your token as its value.
  2. Choose Deploy > New deployment, then select the Web app type. Set Execute as to Me, and Who has access to Anyone. Copy the URL that ends in /exec.
  3. In Hadirly, create an endpoint with the Google Sheets preset. Paste that URL, then add ?token= followed by the same token exactly as it appears in the Script properties.
  4. Record one test attendance, then check for a new row on the Attendance sheet.

Each time the script changes, deploy again and check that the URL you use is still correct.

How signature checking works

The Google Sheets preset does not check signatures. For other receivers that can run server code, the check follows the generic webhook:

  1. Read the webhook-id, webhook-timestamp, and webhook-signature headers.
  2. Reject the message if its timestamp differs from server time by more than 5 minutes.
  3. Build the signed content as the id, a dot, the timestamp, a dot, then the raw body as received, without parsing it again.
  4. Compute HMAC-SHA256 with the key made from the base64 bytes of the secret after the whsec_ prefix. Compare the result with the v1 value in the header using a constant-time comparison.

Hadirly adjusts the body to the format, then signs the body that is actually sent. For that reason the check always runs over the raw body. The Hadirly integration guide includes code for Node.js, Python, PHP, and Go for this step.

Limits

  • Apps Script answers a POST with a 302 redirect to googleusercontent.com after the script runs. Hadirly treats that as a success and does not follow the redirect.
  • The script reads the whole event_id column before it writes, so a very large sheet will slow down. For large volumes, consider a separate sheet for each period.
  • The location column holds JSON text, not separate columns. Use a formula if you need to split latitude and longitude.
  • The deployment URL is public and protected only by the token. If the token leaks, change WEBHOOK_TOKEN and update the URL in Hadirly.
  • A spreadsheet is not a safe place for secret data. Limit who can access the sheet.

Checklist

  • The random token is saved in Script properties and in the Hadirly URL, with the same value.
  • The deployment uses Execute as Me and Who has access Anyone.
  • The column titles in row one match the script order.
  • The test attendance appears as a new row with the correct status.
  • Access to the spreadsheet is limited to people who need to see it.

Sources

  • Google for Developers, Web Apps (Apps Script).
  • Standard Webhooks, specification (GitHub, standard-webhooks).

Try Hadirly for 14 days

The trial needs no payment up front. Register, verify your email, then install the apps.

Frequently asked questions

Do flagged attendance records appear in the spreadsheet?

Yes. This preset receives attendance.recorded, and flagged records are included. The status column shows flagged for records that were flagged.

What happens if Hadirly sends the same message again?

The script reads the event_id in the second column and skips a message that is already there. A redelivery does not create a duplicate row.

Why does the test from Hadirly not add a row?

The test sends webhook.test, and the script writes only attendance.recorded. Record one test attendance to see a new row on the Attendance sheet.

Can the signature be checked in Google Sheets?

No. Apps Script cannot read request headers, so this preset relies on the token in the URL. Checking the signature requires a receiver that runs server code.