How to make API requests with the Request Method

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.

Configuring 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 it not already exists.
  2. In requests.json, use the following syntax to configure all request templates the app is expected to use as JSON objects.

Declare Templates

From templates configured in the config/requests.json, in manifest.json, declare the ones 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 following syntax and create a requests attribute.

In the example manifest.json file, the "createTicket" and "getTickets" are the templates declared to be used in the app.

Invoke the API request

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

In the front-end HTML files (such as iparams.html, index.html), include the client JS resource as follows:

<script src="{{{appclient}}}"></script>

Navigate to the app.js file and include the invokeTemplate() method using the following syntax.

1client.request.invokeTemplate(requestTemplateName, {
2  context: {},
3  body: JSON.stringify(body)
4});
5

Refer to the documentation for more information about the Request Method.