Error while substituting templates (migrating app to v2.3)

Hello,

I’m trying to migrate our app to platform version 2.3 and I get error

Error while substituting templates

while trying to test onAppInstall event after running fdk run command.

I’ve read all the similar topics here and tried many things considering authorization and so on but nothing helped.

Error I’m getting in the terminal is:

{
  status: 400,
  headers: {},
  response: 'error while substituting templates.',
  errors: {},
  errorSource: 'APP',
  attempts: 1
}

I’m also attaching screenshot and a code snippet of how this error looks in browser dev tools:

{
    "requestID": "e6249a6d-cffe-4331-8521-997794b9f7a6",
    "status": 400,
    "message": "The error should be a JSON Object with a message or a status parameter.",
    "errorSource": "APP"
}

My manifest.json is:

{
  "platform-version": "2.3",
  "product": {
    "freshdesk": {
      "events": {
        "onConversationCreate": {
          "handler": "onConversationCreateHandler"
        },
        "onAppInstall": {
          "handler": "onAppInstallHandler"
        },
        "onAppUninstall": {
          "handler": "onAppUninstallHandler"
        }
      },
      "requests": {
        "onConversationCreate":{},
        "onAppInstall":{},
        "onAppUninstall":{}
      },
      "location": {
        "ticket_sidebar": {
          "url": "index.html",
          "icon": "logo.png"
        }
      }
    }
  },

  "engines": {
    "node": "18.20.4",
    "fdk": "9.1.2"
  }
}

My onAppInstall event part of requests.json is

  "onAppInstall": {
    "schema": {
      "method": "POST",
      "protocol": "https",
      "host": "<%= iparams.url %>",
      "path": "/api/v1/webhook/freshdesk",
      "headers": {
        "X-ACCOUNT-KEY": "<%= iparams.account_key %>",
        "Authorization": "Basic <%= encode(iparams.apiKey) %>",
        "Content-Type": "application/json"
      }
    }
  }

My test data of onAppInstall.json is:

{
  "iparams": {
    "integrations": {
      "id": "83f35397-41c9-4ee7-8545-0af663b993a8",
      "name": "Redlinebrand ProdTest",
      "status": "Active"
    },
    "account_key": "57c28612-c96d-487f-9ddd-7c9db8f3040e",
    "url": "channels-staging.ocdn.cloud",
    "valid": true,
    "apiKey": "XXX"
  },
  "domain": "d3v-ontec.freshdesk.com",
  "timestamp": 1721892446493,
  "region": "US",
  "account_id": "946460",
  "event": "onAppInstall",
  "headers": {
    "Content-Type": "application/json"
  }
}

My server.js part for this event is:

onAppInstallHandler: async function (payload) {
        try {
              await $request.invokeTemplate(event, {
                    body: JSON.stringify(payload)
                });
               renderData();
        }
        catch (e) {
             console.error('EventError', e, payload);
              renderData(JSON.stringify(payload));
        }
    },

Hi @eugborisov,

Welcome to the Freshworks Developer Community! :tada:

The onAppInstall event expects the error in the following format if there’s an error. That’s why the error message says it should have a message in the second error.

renderData({message: "Installation failed due to network error."});

In the requests.json file, when the iparams are used directly with a template. It should be named as “iparam”. So, can you please replace your requests.json file with this request template and check? It should resolve the first error saying that the templates couldn’t be substituted.

"onAppInstall": {
    "schema": {
      "method": "POST",
      "protocol": "https",
      "host": "<%= iparam.url %>",
      "path": "/api/v1/webhook/freshdesk",
      "headers": {
        "X-ACCOUNT-KEY": "<%= iparam.account_key %>",
        "Authorization": "Basic <%= encode(iparam.apiKey) %>",
        "Content-Type": "application/json"
      }
    }
  }

If the error still repeats, can you hard-code all the templates with plain string first and test if it works with those values and then replace them with these templates one-by-one to check what is going wrong?

Hi Raviraj,

Thank you for your help.

After renaming to iparam, I encountered an error:

errors: [ { message: 'must not be empty', instancePath: '/schema/host' } ],

Even though I have the host declared as:

"host": "<%= iparam.url %>",

However, if I hardcode the host as a string URL, there is no error.

@eugborisov The syntax looks correct. Please check the documentation as well for confirmation.

From this error, the app is trying to replace the template with the respective value from the installation parameter. Could you submit the installation parameters values to the app while doing the local testing?

Whatever URL iparam value is provided will be filled in here.

In this case I’m getting an error

{
    "requestID": "5fa86a22-6da7-4c05-b329-5c64b299088c",
    "status": 500,
    "message": "{\"iparams\":{\"account_key\":\"ff90bc8a-eead-41cc-a57d-41c9e955a11c\",\"url\":\"https://channels-staging.ocdn.cloud/\",\"valid\":true},\"domain\":\"d3v-ontec.freshdesk.com\",\"timestamp\":1722876361357,\"region\":\"US\",\"account_id\":\"946460\",\"event\":\"onAppInstall\",\"headers\":{\"Content-Type\":\"application/json\"}}",
    "errorSource": "APP",
    "statusCode": 500
}

@eugborisov Does the API call to the endpoint work fine with cURL or Postman?

It looks like an unexpected error. But I can’t find out if it’s coming from the API endpoint on your end or if the platform faces some issue while trying to make this API request and throws 500. Can you please share more context about it?

@Raviraj Do you mean this request?

If so, it receives the same response in Postman

{
    "requestID": "ca40b93b-226f-4da4-b422-f12c672ffbd0",
    "status": 500,
    "message": "{\"iparams\":{\"account_key\":\"f1cd38f1-10de-481e-9f2e-410faceb469a\",\"url\":\"https://channels-staging.ocdn.cloud/\",\"valid\":true},\"domain\":\"d3v-ontec.freshdesk.com\",\"timestamp\":1723206084678,\"region\":\"US\",\"account_id\":\"946460\",\"event\":\"onAppInstall\",\"headers\":{\"Content-Type\":\"application/json\"}}",
    "errorSource": "APP",
    "statusCode": 500
}

@eugborisov Since you’re making an API request from the onAppInstall event, if the API fails with an error, the event fails or fails to return any response, which results in the app error failing to install.

Please fix the API and respond properly with the renderData() callback for success and failure cases for proper app installation. For any scenario, add description logs to check on the serverless logs for troubleshooting.

@Raviraj thank you Raviraj.
You were very helpful on this issue and eventually we were able to migrate our app to platform v2.3

1 Like

This topic was automatically closed 6 days after the last reply. New replies are no longer allowed.