Home > OS >  How to get shipped orders data from Shipstation using Google Apps Script?
How to get shipped orders data from Shipstation using Google Apps Script?

Time:11-13

I am trying to get shipped orders data from ShipStation to Google sheets, when I looked into Developer tools, I found the following javascript code to get the orders list:

var myHeaders = new Headers();
myHeaders.append("Host", "ssapi.shipstation.com");
myHeaders.append("Authorization", "__YOUR_AUTH_HERE__");

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

fetch("https://ssapi.shipstation.com/shipments", requestOptions)
  .then(response => response.text())
  .then(result => console.log(result))
  .catch(error => console.log('error', error));

The orders I am trying to fetch can be found on the following link in my account:

https://ship13.shipstation.com/orders/shipped

I tried to use it in Google apps script but apps script does not recognize new Headers() method and and I have been able to do the following conversion due to my novice skills:

    function Test(){
    
       const apiKey = "###"; // Please set your API key.
       const url = 'https://ssapi.shipstation.com/shipments';

       var options = {
      'method': 'GET',
      "contentType": 'application/json',
      "followRedirects" : false,
      'headers': {
        'Host': 'ssapi.shipstation.com',
        'Authorization': 'apiKey'
  }
};

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

But it gives following error message everytime I try to run it:

Exception: Attribute provided with invalid value: Header:Host

I have obtained API and Secret Key from my ShipStation account also. I found this source to get ShipStation data into Google sheet however it only works with MySQL Remoting Service which is not really helpful in this case. Any guidance would be much appreciated. Thank you.

CodePudding user response:

When I saw the sample curl command from your provided URL, it seems that Host is not included in the request header. And also, UrlFetchApp cannot set Host in the request header. I think that this might be the reason for your current issue of Exception: Attribute provided with invalid value: Header:Host. So, how about the following modification?

Modified script:

function Test() {
  const apiKey = "###"; // Please set your API key.
  const url = 'https://ssapi.shipstation.com/shipments';
  var options = { 'headers': { 'Authorization': apiKey } };
  var response = UrlFetchApp.fetch(url, options);
  Logger.log(response);
}
  • It seems that the sample curl command in the official document is as follows. So, when the above script occurs an error, please confirm the query parameters and your API key, again.

    curl -iX GET 'https://ssapi.shipstation.com/shipments?recipientName=recipientName&recipientCountryCode=recipientCountryCode&orderNumber=orderNumber&orderId=orderId&carrierCode=carrierCode&serviceCode=serviceCode&trackingNumber=trackingNumber&createDateStart=createDateStart&createDateEnd=createDateEnd&shipDateStart=shipDateStart&shipDateEnd=shipDateEnd&voidDateStart=voidDateStart&voidDateEnd=voidDateEnd&storeId=storeId&includeShipmentItems=includeShipmentItems&sortBy=sortBy&sortDir=sortDir&page=page&pageSize=pageSize' \
    -H 'Authorization: __YOUR_AUTH_HERE__' \
    

Note:

  • If your ### of const apiKey = "###" doesn't include the API secret, how about the following modification?

    function Test() {
      const apiKey = "###"; // Please set your API key.
      const apiSecret = "###"; // Please set your API secret.
      const url = 'https://ssapi.shipstation.com/shipments';
      var options = { 'headers': { 'Authorization': 'Basic '   Utilities.base64Encode(`${apiKey}:${apiSecret}`) } };
      var response = UrlFetchApp.fetch(url, options);
      Logger.log(response);
    }
    
    • When the above script occurs the same error 401, please modify ${apiKey}:${apiSecret} to ${apiSecret}:${apiKey}.
  • Related