REST API Timeout after Period of Inactivity

REST API Timeout after Period of Inactivity

We're using the Zoho REST API with the Node SDK. OAuth authentication successfully works and we use Mongo to store the tokens as per the documentation. We can perform CRUD operations on Leads and Contacts throughout the day. However, we're having a strange problem where the Zoho API stops responding overnight. In the morning (after about 12 hours of the application running without interacting with Zoho), when our app goes to make a request to Zoho, it never returns and eventually times out. If I restart the server, the application successfully authenticates again. I'm wondering if this has anything to do with the refresh_token expiring or something of that nature. 



I have a Zoho service which gets initialized on server start with our config, and checks to see if we already have token:




  1. function ZohoService() { //constructor
  2. }

  3. ZohoService.prototype.initialize = function () {
  4.     ZCRMRestClient.setUserIdentifier(USER_IDENTIFIER);
  5.     return ZCRMRestClient.initialize({
  6.         client_id: config.zohoClientId,
  7.         client_secret: config.zohoClientSecret,
  8.         redirect_url: REDIRECT_URI,
  9.         mysql_module: path.join(__dirname, 'zoho')//get this file path dynamically. Mongo store
  10.     })
  11.         .then(() => this.getOAuthTokens(USER_IDENTIFIER))
  12.         .then(tokens => {
  13.             if (tokens.length) {//we already have a token stored
  14.                 return ZCRMRestClient.generateAuthTokenfromRefreshToken(USER_IDENTIFIER, tokens[0].refreshtoken);
  15.             } else {
  16.                 return ZCRMRestClient.generateAuthTokens(USER_IDENTIFIER, config.zohoGrantToken);
  17.             }
  18.         });
  19. };


The method which eventually gets called and times out (getContactsByEmail):

  1. ZohoService.prototype.getContactsByEmail = function (email) {
  2.     return genericZohoModuleRequest(ZohoModules.CONTACTS, 'search',  { email });
  3. };


  4. function genericZohoModuleRequest(mod, action, params, body) {
  5.     return ZCRMRestClient.API.MODULES[action]({
  6.         module: mod,
  7.         params: params,
  8.         body: body
  9.     }).then(r => {
  10.         let statusCode;
  11.         if (typeof r === 'string') {//inconsistent API return value- either String or IncomingMessage
  12.             r = JSON.parse(r);
  13.             statusCode = Number(r.status_code);
  14.         } else {
  15.             statusCode = r.statusCode;
  16.         }
  17.         if (statusCode == 200) {
  18.             return JSON.parse(r.body);
  19.         } else if (statusCode == 204) {//no content, return consistent object
  20.             return {
  21.                 data: [],
  22.                 info: {
  23.                     per_page: 1,
  24.                     page: 1,
  25.                     count: 0,
  26.                     more_records: false
  27.                 }
  28.             };
  29.         } else if (statusCode > 399) {
  30.             return Promise.reject('Zoho HTTP Error ' + statusCode);
  31.         }
  32.         console.warn('Unexpected response status: ' + statusCode + '. Attemping to parse anyway.');
  33.         return JSON.parse(r.body);
  34.     });
  35. }


Any idea why this happens? My guess is that restarting the server corrects the problem because it calls generateAuthTokenfromRefreshToken again on startup, getting a new refresh_token.