Webhook Templates

You can control the content and format for your webhook body with a template. Your template may include:

The webhook body will contain your template with all variables and expressions evaluated and replaced with the resulting values, as described below.

You can use template files for both event-triggered and scheduled reports. The templates for both are largely identical, differing only in the start expression used to select the table rows being displayed.

Template Types

We support the following types of webhook body templates:

  1. CSV
  2. FORM_URL_ENCODED
  3. HTML
  4. JSON
  5. PDF
  6. XLSX
  7. XML

Template Data Sources

We support Google Docs template files stored on these data sources:

  • Google Drive

We support Microsoft Word, Microsoft Excel, CSV, FORM_URL_ENCODED, HTML, JSON, and XML template files stored on these data sources:

  • Box
  • Dropbox
  • Google Drive
  • Microsoft Office 365
  • Microsoft OneDrive

If you wish to use a workflow template, your account must include one or more of these data sources. You can add a new data source from the Account > Data Sources tab.


Creating a Webhook Body Template

You can ask AppSheet to create a template file. You can then customize the template file. Create a webhook body template file as follows:

  1. Add a new workflow or report and save it for the first time. Alternatively, open any existing workflow or report.
  2. Select a template type from the HTTP Attachment Content Type drop down.
  3. Click the Create button following the Body Template property. When you click Create, AppSheet will create an body template and write it to your default data source.
  4. The template will be given a unique name based on the workflow or report name, the Body template type, and the date and time you click Create, ensuring every template has a unique name.
  5. Click View to view and edit the new template file.

If you are using the webhook to invoke the AppSheet API, you should enter the webhook URL value, as described above, before clicking the Create button. We examine the webhook URL value to determine if the AppSheet API is being invoked. If so, we use the table name you specify following "/tables/" in the URL to determine the field values to include in the JSON body template.

If your table contains a List of Refs to related child records, the created JSON Body Template file will include a Start expression that extracts the data values of all child records of the parent record. 

If the child table contains a List of Refs to related grandchild records, the created JSON Body Template file will include a Start expression that extracts the data values of all grandchild records of the child records. 

The created JSON Body Template file will extract the parent record, the child records, the grandchild records, the great grandchild records, and so on. This allow you to extract the entire hierarchy of records.

The Webhook Body Template property always refers to a single template file. You cannot use an expression to specify the template file. If you need to choose between two or more template files, define a separate workflow rule for each template file and specify an expression in the Condition property to select which workflow rule to invoke.

Controlling the Webhook Template Data Source

By default, the template is created on your default data source. For example, if your default data source is Google, the Create button will create the template file on Google Drive. If your default data source is Box, Dropbox, Microsoft Office 365, or Microsoft OneDrive, the Create button will create the template file on that data source.

The Editor field Body Template Data Source indicates which data source contains your template file.

You can control which data source is used by clicking the Browse icon and selecting a template file using the Get data from ... dialog box. If you click the Create button after doing so, the created template will be saved on the data source you selected while browsing.

Viewing the Template

Once the template is created, click the View button to view or edit it.

Manually Editing the Template

You can edit the template file by:

  1. Downloading the current template file from Google Drive.
  2. Editing the template file using a text editor.
  3. Uploading the updated template file to Google Drive.
  4. Ensuring the Editor is referring to the latest template file.

Downloading the template file from Google Drive

  1. Go to the Account>Setting pane in the Editor and find the "Default folder path".
  2. In Google Drive, locate the "Default folder".
  3. Withing the "Default folder", locate the folder containing your app.
  4. Within the folder containing your app, locate the  "Context" folder.
  5. Right click the template .txt file and select "Download". This will down load the template file to your local computer.

You can then edit the template file. Once the template file is edited you can upload the template file to Google Drive as follows.

Uploading the template file to Google Drive

  1. Open Google Drive and navigate to the "Content" folder on Google Drive as described in "To Download the template file from Google Drive". 
  2. Click the "New" button and then click "File upload". 
  3. When the file open dialog appears, navigate to the template file on your personal computer, select the file, and click "Open". This will upload the template file to Google Drive.

After Uploading the template file to Google Drive

  1. Open the AppSheet Editor and go to the appropriate Workflow or Report.
  2. Click the file icon for the "JSON Body Template" field. 
  3. When the "Select a file" dialog appears, navigate to the uploaded template file you uploaded to Google Drive and select it.
  4. This makes the uploaded template file the current JSON Body Template.

Each time you modify the template you must upload the template file to Google Drive. Google Drive will retain the original DocId. AppSheet will use the newly uploaded template file thereafter.

Manually Creating the Template

You can manually create a template file by creating an empty .txt file on your personal computer, entering text like that below, and saving the file to your personal computer.

{
  "MyTable": [
  <<Start: Select(MyTable[MyKey], TRUE)>>
      {
         "UpdateMode": "<<_UPDATEMODE>>",
         "Application": "<<_APPNAME>>,
         "TableName": "<<_TABLENAME>>,
         "UserName": "<<_USERNAME>>,
         "LastName": "<<[LastName]>>",
         "FirstName": "<<[FirstName]>>",
         "Age": "<<[Age]>>",
         "Computed Total": "<<[Qty]*[Price]>>",
         "ComputedName": "<<_ComputedName>>"
      },
   <<End>>
   ]
}

Once the template file is saved to you personal computer, upload it to one of the following Data Sources:

  1. Box
  2. Dropbox
  3. Google Drive
  4. Microsoft Office 265
  5. Microsoft OneDrive

For example, you can upload the template file to Google Drive, as follows. 

  1. Open Google Drive and navigate to the Google Drive folder where you wish to upload the file. 
  2. Click the "New" button and then click "File upload". 
  3. When the file open dialog appears, navigate to the template file on your personal computer, select the file, and click "Open". This will upload the template file to Google Drive.

Once the template file is uploaded:

  1. Click the file icon following the Body Template property. 
  2. When the "Select a file" dialog appears, navigate to the uploaded template file and select it.
  3. This makes the uploaded template file the current Template.

Each time you modify the template you must upload the template file to Google Drive. Google Drive will retain the original DocId. AppSheet will use the newly uploaded template file thereafter.

Using Start Expressions in the Template

Your template file can contain Start expressions. This will repeat the values between the <<Start>> and <<End>> tags for each row returned by the Start expression.

{
   "Action": "Edit",
   "Properties": {
      "Locale": "en-US",
      "Location": "47.623098, -122.330184",
      "Timezone": "Pacific Standard Time"
   },
  "Rows": [
    <<Start: Filter(OrderDetails, true)>>
    {
      "Name": "<<Name>>",
      "Qty": "<<[Qty]>>",
      "Price": "<<[Price]>>",
      "Total": "<<[Qty]*[Price]>>",
      "Sales Tax": "<<[Qty]*[Price]*0.085>>",
      "Email": "<<[Email]>>",
      "Product Image": "<<[Product Image]>>"
    },
    <<End>>
  ]
}

Using If Expressions in the Template

Your template file can contain If expressions. This will conditionally include the values between the <<If>> and <<EndIf>> tags . In the following example, field Sales Tax is included if Total is $1.00 or more.

{
   "Action": "Edit",
   "Properties": {
      "Locale": "en-US",
      "Location": "47.623098, -122.330184",
      "Timezone": "Pacific Standard Time"
   },
  "Rows": [
    <<Start: Filter(OrderDetails, true)>>
    {
      "Name": "<<[Name]>>",
      "Qty": "<<[Qty]>>",
      "Price": "<<[Price]>>",
      "Total": "<<[Qty]*[Price]>>",
      <<If: [Qty]*[Price] >= 1.00>>
      "Sales Tax": "<<[Qty]*[Price]*0.085>>",
      <<EndIf>>
      "Email": "<<[Email]>>",
      "Product Image": "<<[Product Image]>>"
    },
    <<End>>
  ]
}

Did this answer your question?