Webhook request from Google Apps Script

Hello everyone,

I have an automation that is triggered by a Webhook. I can trigger it no problem using curl in my terminal, but for some reason I cannot get it to work in Google Apps Script. Here is how I would call it using curl:

curl -X POST -H "Content-Type: application/json" -d '{}' http://homeassistant.local:8123/api/webhook/test

And here is what my code looks like in Apps Script:

function testConnection() {
  const url = "http://homeassistant.local:8123";
  var options = {
          method: "post",
          contentType: "application/json",
          payload: ""
  };
  try {
    const response = UrlFetchApp.fetch(url, options);
    Logger.log("Response Code: " + response.getResponseCode());
    Logger.log("Content: " + response.getContentText());
  } catch (err) {
    Logger.log("Error: " + err.message);
  }
}

I get a “Bad request: http://homeassistant.local:8123” error thrown at me. I’m super confused because everything works fine with a normal HTTP request (like curl).

thanks a bunch!

Hi cartesian19,

Google will not be able to see that.
It would have to be a URL that is accessible from the cloud for Google to be able to call it.

Hm I see. Thanks for your answer! How do you know it’s not accessible to the cloud? Sorry I’m really not computer savvy, maybe this is obvious.

And I’m curious about how you would go about this? I wanted to create an automation every time a google sheets update. Maybe use Google’s API and run a local script instead?

Ok I think I get it – HA runs locally. It’s not on the “internet” per se, or the cloud. So you cannot fetch a URL using Google’s AppsScript. Is there a way to make my server accessible to the cloud? Or is that a big privacy issue?

You can, but the external_url has to work and it cannot use a self-signed certificate.
So if you have signed up for nabu-casa it will work, or if you have set-up external access properly another way it will work. TTS will have the same issues.

Open your Home Assistant instance and manage your systems network configuration.

Ok I see. I am not signed up for nabu-casa and I already have their one-month free cloud trial a while ago… Do you know of any other service to make my webhook url public?

There are literally dozens of ways. Please search the Forum for some ideas:

Thanks, this is helpful!