← All lessons/Automation
38
Automation

Triggers

Fire an action whenever a matching record is written to an agent.

Prerequisite: Lessons 5 and 13 complete

What you'll learn

  • POST /v1/agents/:id/triggers — name, on_write, filter, action
  • action types: write to another agent, call a webhook
  • Filter applies before trigger fires — only matching records trigger
  • GET /v1/agents/:id/triggers — list
  • Use case: payment → notification, signup → onboarding event
Challenge

Trigger on users when plan='enterprise'. Write a regular user, then an enterprise user. Confirm enterprise_alerts received a record only for the enterprise user.

Interactive Walkthrough

What you'll learn

Fire an action whenever a record is written to an agent — event-driven automation within SapixDB.

Create a trigger

curl -s -X POST http://localhost:7475/v1/agents/orders/triggers \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer spx_root_YOUR_ROOT_KEY" \
  -d '{
    "name":      "order-notification",
    "on_write":  true,
    "filter":    {"field": "status", "op": "eq", "value": "paid"},
    "action":    {
      "type":     "write",
      "agent_id": "notifications",
      "payload":  {"type": "order_paid", "source": "trigger"}
    }
  }' | python3 -m json.tool

Now whenever a record with status = "paid" is written to orders, SapixDB automatically writes a notification record to the notifications agent.

List triggers

curl -s http://localhost:7475/v1/agents/orders/triggers \
  -H "Authorization: Bearer spx_root_YOUR_ROOT_KEY" \
  | python3 -m json.tool

Trigger with HTTP webhook action

curl -s -X POST http://localhost:7475/v1/agents/orders/triggers \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer spx_root_YOUR_ROOT_KEY" \
  -d '{
    "name":     "webhook-on-paid",
    "on_write": true,
    "filter":   {"field": "status", "op": "eq", "value": "paid"},
    "action":   {
      "type":    "webhook",
      "url":     "https://your-service.com/webhooks/order-paid",
      "headers": {"X-Source": "sapixdb"}
    }
  }' | python3 -m json.tool

Challenge

Set up a trigger on users that fires when plan = "enterprise". Write a regular user, then an enterprise user. Check that the enterprise_alerts agent received a record only for the enterprise user.

---

← Previous
Lesson 37: Cron Jobs
Next →
Lesson 39: Mutants (AI Schema Evolution)