Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ReferenceError: Headers is not defined in Google scripts

I am trying to automate between reply.io and Gsheets, to automatically do API calls, so I can create a 'live' dashboard in Google Sheets through script editor.

I am using the documentation on their website (https://apidocs.reply.io/#b24cfec9-3e95-4409-b520-8dfecd75e682). I only work with Python and I succeed there, but somehow I can't seem to make it work in JavaScript.

The script I use is:

function myFunction() {
var myHeaders = new Headers();
myHeaders.append("x-api-key", "{{Your_apiKey}}");

var raw = "";

var requestOptions = {
  method: 'GET',
  headers: myHeaders,
  body: raw,
  redirect: 'follow'
};

fetch("https://api.reply.io/v2/schedules/default", requestOptions)
  .then(response => response.text())
  .then(result => console.log(result))
  .catch(error => console.log('error', error));
}

The error I am getting is:

ReferenceError: Headers is not defined
myFunction  @ Code.gs:2
like image 563
Sven123456789 Avatar asked Apr 08 '26 00:04

Sven123456789


1 Answers

Google Apps Script do not use the browser built ins, instead they have their own builtins. In this case you would need to use UrlFetchApp.

function myFunction() {
    let myHeaders = {
        "x-api-key": "{{Your_apiKey}}"
    };

    let params = {
      method: 'get',
      headers: myHeaders,
      followRedirects : true,
    };

    let response = UrlFetchApp.fetch("https://api.reply.io/v2/schedules/default", params);
    Logger.log(response.getContentText());
}
like image 52
MinusFour Avatar answered Apr 11 '26 04:04

MinusFour