Building a REST API with Google Apps Script
# Building a REST API with Google Apps Script Google Apps Script gives you a free web server. Deploy as a "web app" and you get a URL that handles HTTP requests — no hosting costs, no server management. ## The Basics Every GAS web app needs two functions: ```javascript function doGet(e) { // Handle GET requests return ContentService.createTextOutput('Hello!') .setMimeType(ContentService.MimeType.JSON); } function doPost(e) { // Handle POST requests const body = JSON.parse(e.postData.contents); // ... process request return ContentService.createTextOutput(JSON.stringify({ status: 'ok' })) .setMimeType(ContentService.MimeType.JSON); } ``` ## The CORS Problem (and Solution) GAS web apps redirect requests, which breaks standard CORS. The workaround: - Use POST requests only - Set `Content-Type: text/plain` (avoids preflight) - Add `redirect: 'follow'` to your fetch calls ```javascript const res = await fetch(API_URL, { method: 'POST', headers: { 'Content-Type': 'text/plain' }, body: JSON.stringify({ action: 'list', table: 'users' }), redirect: 'follow' }); ``` ## What the CLI Generates The `nsa-sheets-db-builder` CLI generates all of this for you — a full router with CRUD operations, schema validation, audit trails, and optional RBAC. You define tables in JSON, and the CLI outputs a deployable GAS project. No manual Apps Script coding needed (though you can customize `main.ts` if you want).