API & Webhooks AutomationDifficulty: Advanced⚡ Web App Deployment

Build a Serverless Webhook Endpoint (doPost) in Apps Script

Turns a Google Sheet into an instant, free serverless webhook endpoint that receives JSON payloads from Stripe, GitHub, or Shopify and logs records in real time.

Required OAuth Scopes: https://www.googleapis.com/auth/spreadsheets
Code.gs (Apps Script V8 Runtime)
function doPost(e) {
  try {
    const postData = JSON.parse(e.postData.contents);
    const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Webhooks');
    
    const timestamp = new Date();
    const eventType = postData.event || 'unknown';
    const payloadString = JSON.stringify(postData);
    
    sheet.appendRow([timestamp, eventType, payloadString]);
    
    return ContentService.createTextOutput(JSON.stringify({ status: 'success' }))
      .setMimeType(ContentService.MimeType.JSON);
  } catch (err) {
    return ContentService.createTextOutput(JSON.stringify({ status: 'error', message: err.message }))
      .setMimeType(ContentService.MimeType.JSON);
  }
}

Line-by-Line Code Explanation

Line 1: Special Apps Script entry function `doPost(e)` triggered on HTTP POST.
Line 3: Parses incoming JSON payload from request body.
Line 9: Appends record directly to the spreadsheet.
Line 11-12: Returns HTTP JSON response with status success.

Step-by-Step Setup & Deployment

  1. In Apps Script, click Deploy > New Deployment > Select type: Web App.
  2. Set "Execute as: Me" and "Who has access: Anyone".
  3. Copy the generated Web App URL and paste it as your webhook endpoint in Stripe or GitHub.

Best Practices for Quota & Rate Limit Management

When deploying this automation in Google Workspace, adhere to these operational principles:

  • Batch Reads & Writes: Never execute getValue() or setValue() inside loops. Read ranges into memory in 1 call, process arrays in V8 memory, and commit changes with a single setValues() call.
  • Execution Timeouts: Apps Script terminates scripts exceeding 6 minutes. For multi-thousand row datasets, implement batch pagination with resume timestamps stored in PropertiesService.
  • OAuth Permissions: Scope your manifest (appsscript.json) narrowly to prevent security prompts from alarming external team members.

Frequently Asked Questions

Is Google Apps Script completely free to use?

Yes. Google Apps Script is built into all free Google accounts and Google Workspace editions. There are no server fees or subscription costs for running scripts within daily quotas.

Can I run this script on an automated schedule without keeping my computer on?

Yes. Time-driven triggers execute on Google's cloud infrastructure. Your script will run reliably even when your computer is closed or offline.

More API & Webhooks Automation Snippets