0
votes

I am trying to access a capsule CRM API (documentation here)

I am quite new to google app script and was trying to convert a curl request into a gas function using UrlFetchApp

I am trying to search for a certain party via the API -- they provide this endpoint:

GET

https://api.capsulecrm.com/api/v2/parties/search

These are the Search parameters in the request:

  • q _________Type(String)___:The value to search for e.g. a name
  • page______Type(Integer)__:The page of results to return
  • perPage___Type(Integer)__:Number of entities to return per page

Their API Documentation says that you would read data from the API by doing the following:

curl -H "Authorization: Bearer {token}" 
     -H "Accept: application/json" 
     https://api.capsulecrm.com/api/v2/parties

The following is my gas function which does return the data, but does not effectively search for the correct record and return only the resultset. I would like to know if there is an easier and better way to perform this search?

function curlRequest(partyid) {

  var authenticationToken = "XvDEdg652DG...xYYRg";
  //var url = "https://api.capsulecrm.com/api/v2/parties";
  var url = "https://api.capsulecrm.com/api/v2/parties/search";
  var headers = {
    "Authorization" : "Bearer " + authenticationToken,
  };

  var payload = {  
    "Accept": "application/json",
    "q":{
      "id":101756643
    },
     "page":1,
     "perPage": 50,
  };

  var options = {
    "headers" : headers,
    "method" : "GET"
  };

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

EDIT: based on feedback from Jack Brown:

It seems that the advice is correct, however the Capsule API is not geared for this structure: enter image description here

2

2 Answers

1
votes

You never pass the payload to the urlFetch method, you create a payload. However you need to insert the payload in the options object like so:

var options = {
    "headers" : headers,
    "method" : "GET",
    "payload" : payload
  };

Also -H in curl means a header value, so "Accept": "application/json" should be defined in the header

var headers = {
    "Authorization" : "Bearer " + authenticationToken,
    "Accept": "application/json"
  }

Finally, your payload will look like this:

var payload = {  
    "q":{
      "id":101756643
    },
     "page":1,
     "perPage": 50,
  };

Now when you make the urlfetch Call it should work like excepted.

Logger.log(UrlFetchApp.fetch(url, options))

Hope that helps!

1
votes

If payload is included in options then fetch will make a POST request even if you specify method: 'get'. This explains why you're receiving the "Method not allowed" error from the API - the "/api/v2/parties/search" endpoint accepts only GET requests and not POSTs.

So this means that for a GET request the search query should be included in the URL instead of in a payload. It's also probably best to control the escaping yourself instead of using the escaping provided by UrlFetchApp - this will allow for any special characters in the search term. GET is the default method so that can be omitted from the options.

Making those changes, the code becomes:

function myFunction() {
  var authenticationToken = "XvDEdg652DG...xYYRg";

  var query = "your search";
  var url = "https://api.capsulecrm.com/api/v2/parties/search?q=" + 
    encodeURIComponent(query);

  var headers = {
    "Authorization" : "Bearer " + authenticationToken,
    "Accept": "application/json"
  };

  var options = {
    headers: headers,
    escaping: false
  };

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