Guide for Memento scripters

Call a webhook from a Memento Database trigger

Memento Database can run JavaScript when an entry is created or updated, and that script can make HTTP requests. Here is a complete, working example.

This is the trigger MQSync uses to start a sync as soon as a work order is saved. The same pattern works for any outside service that accepts a POST.

Pro plan feature. On-save sync is part of the MQSync Pro plan. On other plans, tasks run on a schedule or on demand. The trigger technique itself works with any service.

The script

var MQSYNC_URL   = "https://mqsync.com/webhook/task/YOUR-TASK-TOKEN";
var MQSYNC_DELAY = 10;  // seconds

http().post(MQSYNC_URL + "?delay=" + MQSYNC_DELAY, "{}");

http() is Memento's built-in HTTP object, and post(url, body) sends the request. MQSync needs no body, so the script sends an empty JSON object; the task is identified by the token in the URL.

Your own task page in MQSync shows this script with your real URL, ready to copy. Treat that URL like a password: anyone who has it can start your task. If it ever leaks, rotate it on the task page and paste the new one into the triggers.

Setting it up in Memento

  1. Open the library, for example Work Orders, go to Automations, press + and choose Trigger.
  2. Paste the script into the JavaScript field.
  3. Set Event to Creating an entry and Phase to After saving the entry. Name it, for example “Webhook (Creating an entry)”, and save.
  4. Open the trigger's menu, choose Copy, and set the copy's Event to Updating an entry. Name it and save.
  5. In the library's Permissions for scripts, enable Network.
  6. Repeat in every library the task syncs.

Why two triggers

A trigger on Creating an entry fires only for new entries. Without the second one on Updating an entry, later edits would never be sent. This one fails silently, so it is worth checking first.

Why the Network permission

Without it, Memento blocks the call the moment the trigger fires and shows “Script does not have permission to send http requests”. Nothing is wrong with the script: switch the permission on.

Why the delay

The trigger runs on the phone, right after the save. The service on the other end reads the entry from the Memento cloud, so the entry has to get there first. The delay parameter tells MQSync to wait that many seconds before it reads (up to 300).

Ten seconds is usually enough. Sync the library in the app right after saving (pull down on the list) to make sure the entry has left the phone. If a run ever arrives too early, it simply finds nothing new: save the entry again, or press Run Now on the task. If it keeps happening, raise the delay to 15–20 seconds.

The same applies to a Memento Private Server: the entry must be stored on the server before anyone can read it.

What MQSync answers

The endpoint returns JSON: 202 with "status": "accepted" when the run is queued. Errors also come back as JSON with an error code: not_found (wrong or rotated URL), task_paused, plan_limit, rate_limited (more than 60 calls a minute). A trigger that ignores the response is fine; this is for debugging.

Adapting it to another service

Replace the URL, and build the body from the entry if the service needs data:

var e = entry();
http().post("https://example.com/hook",
            JSON.stringify({ id: e.id, name: e.field("Name") }));

Keep the two triggers and the Network permission. Whether you need a delay depends on whether the service reads from the Memento cloud.

New to MQSync? Start with the setup guide, or see how work orders become invoices.

Related guides

Try it on your own data

Create your MQSync account and start a 7-day free trial, no credit card required.