Social Climate Tech

Sat 31 2025
Image

How to Use Ngrok to Test Your Local App and Connect It with Google Apps Script

by bernt & torsten

Testing a local application that needs to be accessed over the internet (like for webhooks or from Google Apps Script) can be a challenge — ngrok solves that by exposing your local server with a public HTTPS URL.

This guide covers:

  • How to run ngrok to expose your local server

  • How to call that endpoint from Google Apps Script

  • How to handle dynamic ngrok URLs

  • How to configure appsscript.json for external fetch requests


Step 1: Run Your Local App

Make sure your local app is running, for example, at:

http://localhost:8080

Step 2: Start ngrok

Use this command to expose your local port:

ngrok http 8080

You’ll see output like this:

Forwarding https://...ngrok-free.app -> http://localhost:8080

Copy the https://...ngrok-free.app URL. This is your public endpoint.


Step 3: Add the ngrok URL to appsscript.json

Google Apps Script requires you to whitelist external URLs in your manifest file. Open appsscript.json and include:

{
  "urlFetchWhitelist": [
    "https://...ngrok-free.app"
  ]
}

Make sure to update the ngrok URL each time it changes.


Step 4: Call Your Endpoint in Google Apps Script

Here’s a complete function you can run from a menu or a trigger:

function callLocalNgrokAPI() {
  var url = "https://...ngrok-free.app";

  var options = {
    method: "post",
    contentType: "application/json",
    headers: {
      "X-API-Key": "your-api-key-here"
    },
    payload: JSON.stringify({
      model: "gpt-4o",
      prompt: "Tell me a joke"
    }),
    muteHttpExceptions: true
  };

  var response = UrlFetchApp.fetch(url, options);
  Logger.log(response.getContentText());
}

Step 5: If the ngrok URL Changes

ngrok URLs change each time you restart ngrok unless you have a paid plan with a reserved domain.

You can:

  • Manually update the URL in appsscript.json And your function

  • OR use a script (e.g., Node.js or Bash) to fetch the current ngrok URL and push it into a database, spreadsheet, or version-controlled file

Example (to get the current ngrok tunnel from the local API):

curl http://127.0.0.1:4040/api/tunnels

The output will include your current public URL.


Step 6: Testing with curl

You can also test your endpoint with curl:

curl -X POST "https://...ngrok-free.app" \
  -H "Content-Type: application/json" \
  -H "X-API-Key: your-api-key-here" \
  -d '{"model": "gpt-4o", "prompt": "Tell me a joke"}'

Summary

Task Command or Action
Run the local server localhost:8080
Expose using ngrok ngrok http 8080
Test with curl curl -X POST ...
Call from Apps Script UrlFetchApp.fetch(...)
Whitelist URLs in the manifest "urlFetchWhitelist" in appsscript.json
Get the current ngrok URL curl http://127.0.0.1:4040/api/tunnels

 

Share: