Search code examples
apigoogle-apps-scriptput

Apps Script PUT API


My goal is to update an API using Apps Script but I cannot work out how to do it. For simplicity sake here is my code:

let APIkey = "...";
let url = "..."

newData = {"stock_status": "instock"}

//Update API

The problem is I do not know how to get any further. I have read the relevant docs to this API but to no avail and I couldn't find anything about put requests in the Apps Script docs.


Solution

  • Answer:

    You need to use UrlFetchApp.

    Example:

    I don't know how your API accepts authentication, but assuming it accepts the key as a URL parameter then you can do something like:

    let APIkey = "..."
    let url = "..."
    
    const newData = {
      "stock_status": "instock"
    }
    
    var options = {
      'method' : 'PUT',
      'contentType': 'application/json',
      'payload' : JSON.stringify(newData)
    }
    
    UrlFetchApp.fetch(`${url}?key=${APIkey}`, options);
    

    You can read the full documentation on fetch here