Use request method to place secure HTTP calls

Request method is a secure mechanism to send HTTP requests from an app to a third-party domain, without exposing any sensitive information that is part of the request. Through browsers, app users may intercept sensitive information such as API keys or user credentials. Request method safeguards against such exposure. Typically, HTTP requests are routed through proxy servers. In such cases, sending the HTTP requests through the request method helps identify the origin in scenarios where the third-party domain has enabled Cross Origin Resource Sharing (CORS) support.

To use the request method,

  1. As part of the app configuration, configure request templates.
  2. In manifest.json, declare the template(s) that the app code uses.
  3. From the app logic, invoke the configured template(s), and make HTTP requests to third-party domains.

For a demonstration of this feature, see our collection of sample apps with the request method.

Notes:
  • The default request timeout is five seconds. For the local testing of apps that use the request method, you can specify an appropriate timeout. For information on local testing with REQUEST_TIMEOUT, see Test apps that use request method.
  • The rate limit for requests is 50 requests per minute per app per account.
  • In the requests.json file, you can configure a maximum of 100 templates.

Allow-list IPs

At times, API/resource providers do not want apps to connect to their APIs/resources, unless the app is trying to connect from a known and approved system/org. To get the origin (from where the requests originate) approved, the app devs will have to provide the list of IPs from which the requests can originate. The resource providers will allow-list these IPs in their system and allow any request that comes from these IPs to access their resources. If you want to access such a resource, you will have to provide the following IPs to the resource providers, for allow-listing.

For custom app devsIf you are building an app for Freshdesk and if IP whitelisting is enabled for your Freshdesk account, allow-list the following IPs to enable your app to access the Freshdesk resources.

RegionIPs
United States18.233.117.211 and 35.168.222.30
Germany/Europe-Central18.197.138.225 and 52.57.69.21
India13.232.159.149 and 13.233.170.242
Australia13.211.182.225 and 52.63.187.64
United Arab Emirates3.29.180.34 and 51.112.23.180
United Kingdom/Europe-West18.169.146.42 and 13.42.232.164

Configure request templates

Request templates are snapshots of all the HTTP requests that an app is expected to make.

  1. From the app’s root directory, navigate to the config folder and create a requests.json file (if requests.json does not exist).
  2. In requests.json, use the following syntax to configure all request templates that the app is expected to use, as JSON objects.

Attributes of the request.json file

  • <requestTemplateName>object

    <requestTemplateName> is the name used at runtime to invoke the request template. This object contains the attributes that are required to build the request.

Template substitutions

Template substitutions enable the usage of variables in the <requestTemplateName>.schema attributes. During runtime, depending on where the variables are used, they can be populated by:

Variable limitations

Variables can be used only in the following <requestTemplateName>.schema attributes. Also, the type of data that can populate the variables varies depending on the attribute in which the variables are used.

<requestTemplateName>.schema attributesType of data that can populate the variables
Note:For a detailed description of these schema attributes, see the <requestTemplateName>.
host
  • Non-secure iparam values
  • Contextual data
path
  • Non-secure iparam values
  • Contextual data
headers (can be used only in <header-value>)
  • Non-secure iparam values
  • Secure iparam values
  • Stored access tokens
  • Contextual data
query
  • Non-secure iparam values
  • Contextual data

Substitute template variables with iparam values

Use the following syntax to declare a template variable that is populated with iparam values at runtime.

<%= iparam.validIparamName %>

variableName must match the context variable name used in the runtime API when invoking the request template. If there is a mismatch in the names, the runtime API call fails.

encode() This function enables encoding secure iparams that are passed as part of the request.

SyntaxExample
<%= encode(iparam.validIparamName) %>"Authorization": "Bearer <%= encode(iparam.api_key) +':x'>"

Substitute template variables with contextual data

Use the following syntax to declare a template variable that is populated with contextual data, passed through the runtime API that invokes the request template.

<%= context.variableName %>

Substitute template variables with access tokens

Use the following syntax to declare a template variable that is populated with an access token value.

<%= access_token %>

Note:This variable is used only in <requestTemplateName>.schema.headers.<header_value> to facilitate the app to place an OAuth request call. This variable should be used when <requestTemplateName>.options.isOAuth is true.

Declare templates

From templates configured in the config/requests.json, in manifest.json, declare the ones that the app code uses, for a specific product.

  1. From the app’s root directory, navigate to the manifest.json file.
  2. Under product.<productName>, use the sample shown in the right pane and create a requests attribute.

Invoke templates

To send an HTTP request to a third-party domain, in the app code, include the invokeTemplate() method. This method serves as a runtime API call. As part of the call, the app can pass a dynamic context and any other data that the template requires.

  1. In the front-end HTML files (such as iparams.html), include the client JS resource as follows: <script src="{{{appclient}}}"></script>
  2. Navigate to the app.js file and use the syntax in the right pane to include the invokeTemplate() method.

Arguments of invokeTemplate()

  • requestTemplateName string Mandatory
    Identifier of the request template in config/requests.json and the request in manifest.json. The requestTemplateName must be the same in the app code, configuration (config/requests.json), and declaration (manifest.json).

  • An object with the following optional attributes:

    • contextobject

      All contextual data that the template requires, specified as key-value pairs of <variableName>:<variableValue>. <variableName> must be the same as that used in config/requests.json to receive contextual data.

    • bodyobject

      Request body.

      Response caching: Requests made through the request method are governed by rate limits. To prevent repetitive calls from breaching the rate limits, the request method comes with an opt-in response caching feature, for front-end apps. The cache and ttl options available as part of the invokeTemplate() method, enable browsers to cache the responses that are retrieved from third-party calls. The responses are cached in the Browser’s local storage. For requests that haven’t changed since the last call, the response is retrieved from the cache. This conserves the app’s rate limits as the number of requests made to the third-party are conserved.

Response

If the runtime API call results in placing a successful request call to the third-party domain, a data object similar to the following sample is returned.

{
"status" : 200,
"headers":
  {
    "Content-Length": 20,
    "Content-Type": "application/json;charset=utf-8"
  },
"response": '{ "Name": "Rachel"}'
}
  • status number

    HTTP status code.

  • headers object

    Additional context about the response, received as an object of valid <headerParameterName>:<headerParameterValue> pairs.

  • response string

    Data retrieved successfully from the third-party domain, through the request method, specified as a string.

Test apps that use request method

In the local testing of apps that use the Request Method to make third-party HTTP requests, you can specify a value for the request timeout.

To configure the request timeout to a suitable value, perform one of the following:

  • Use the REQUEST_TIMEOUT parameter along with the fdk run command as follows:
    SyntaxExample
    REQUEST_TIMEOUT=<timeout in milliseconds> fdk runREQUEST_TIMEOUT=10000 fdk run
  • Navigate to the .env file and add the following:
    SyntaxExample
    export REQUEST_TIMEOUT=<timeout in milliseconds>export REQUEST_TIMEOUT=10000
Important:
  • Minimum value for REQUEST_TIMEOUT: 5000
  • Maximum value for REQUEST_TIMEOUT: 10000
  • If you specify a REQUEST_TIMEOUT value that breaches the min or max limits, the timeout is defaulted to the min or max value and a warning message is displayed.
  • If you don’t specify a value for REQUEST_TIMEOUT, the timeout value is 5 seconds.

For information on how to test an app that uses the Request method feature, see Test your App.