Skip to content

Hooks ​

A hook is something Frege does by itself when an event happens in an environment: call a tool, update the customer's record, send a signed webhook to your system, notify your team, or run an agent that messages the customer. Hooks run from a queue after their event, so a failing hook never reaches whoever caused the event; its runs are the report.

An example ​

When createOrder succeeds, send the order to your CRM:

bash
curl -X POST "https://api.frege.io/v1/projects/$PROJECT/hooks" \
  -H "Authorization: Bearer $TOKEN" -H "X-Frege-Stage: live" \
  -H "Content-Type: application/json" -d @hook.json
json
{
  "name": "Orders to the CRM",
  "trigger": "tool_call",
  "tools": ["createOrder"],
  "on": "success",
  "filter": [{"field": "client.id", "op": "exists"}],
  "steps": [
    {"id": "send", "type": "webhook", "params": {
      "url": "https://crm.example.com/frege/orders",
      "body": {"order": "{{event.response.id}}", "customer": "{{event.client.id}}"}}}
  ]
}

When: the trigger ​

triggerRuns
tool_call (the default)After any of its tools is called, up to 20. on is success (the default), failure or any.
sign_inWhen a customer signs in. The event's source is api for your sign-in, docs for a reader of your docs.
credential_renewedWhen a customer's credential is renewed.
credential_expiredWhen it expires and cannot be renewed.
scheduleOn schedule: {"cron": "0 9 * * 1-5", "time_zone": "Asia/Tashkent"}: five cron fields, UTC when no zone is named.
webhookOn each verified delivery to a webhook source (see Webhooks from your system).

GET /v1/projects/{id}/hooks/triggers lists them with the fields each event carries; GET .../hooks/tools the tools a hook may use and their fields.

  • delay_seconds starts each run that long after its event, up to seven days (not on a schedule).
  • max_attempts is how many times a failed run is tried, 1 to 10 (3 by default), 30 seconds apart and doubling. A retry resumes at the failed step; steps that succeeded are not run again.
  • Calls made by hooks, the playground and evaluations never start a hook.

If: conditions ​

filter is a list of conditions that must all hold: {"field", "op", "value"}, where field is a path in the event (client.id, body.order.status) and op is equals, not_equals, in or exists. condition is a full expression over the roots event and now, for anything more:

json
{"any": [
  {"field": "event.source", "op": "equals", "value": "docs"},
  {"field": "event.client.email", "op": "matches", "value": "@example\\.com$"}
]}
FormHolds when
{"all": [...]}every one holds (an empty list holds)
{"any": [...]}at least one holds
{"not": c}c does not
{"field": "event.x", "op": "...", "value": ...}the field compares as asked
{"left": v, "op": "...", "right": v}two values compare as asked

The operators are equals, not_equals, in, exists, lt, lte, gt, gte, contains, matches (an RE2 pattern), before, after, older_than and newer_than (a duration such as 7d, 2w, 36h, 90m). A missing field makes every comparison false except not_equals. GET /v1/projects/{id}/hooks/expressions describes the language as the server checks it.

Then: steps ​

Up to ten steps run in order. Each has an id, a type and params. A string in params may reference the event or an earlier step's output:

text
{{event.response.id}}      a field of the event
{{steps.me.user.phone}}    the output of the earlier step "me"

A reference that finds nothing is empty, never an error: inside text it writes nothing, standing alone as a value it leaves that key out. An object or list inside text is written as JSON.

typeparamsWhat it does
call_tooltool, argumentsCalls a tool as the customer. One that changes something needs the hook's allow_writes.
update_clientidentifier, identitiesRecords who the customer is: identities is a map (phone, email, any key) merged into theirs.
webhookurl, method, headers, bodySends signed JSON to your system. See Webhooks to your system.
notifychannel (email or telegram), to, subject, text, topicTells project members, or the customer in your Telegram bot (to: "customer").
run_agentagent, prompt, customer, deliver, conversation, toolsRuns an agent for the customer and sends its reply to them in Telegram.

A Telegram notify with a topic (such as orders) carries a button that stops that topic's alerts. An alert that was stopped fails with the reason muted, one over the bot's daily cap with capped.

run_agent runs the agent as the customer ({{event.client.id}} by default), with only the tools you list (up to 50, or none), and delivers the reply to the customer's chat with your bot. A customer who never started the bot, or blocked it, gets nothing.

Testing and runs ​

  • POST /v1/projects/{id}/hooks/test runs the hook in the body, not a stored one, on a made-up event. Nothing is written: webhooks, notifications and tools that change data are skipped. Reads are real, so it is for organization admins only.
  • GET /v1/projects/{id}/hooks/{hookID}/runs lists a hook's runs, newest first, kept for 30 days.
  • POST .../runs/{runID}/retry runs a failed run again.