# POST /event-schedules

**Resource:** [Event Schedules](./event-schedules.md)  
**Scopes:** `schedules:write`  
**Write operation:** yes

Create a new event schedule. Required fields: name, cron_expression, audience_type, automation_id.

**Cron expression** is standard 5-field syntax: "min hour day-of-month month day-of-week".
Common patterns:
- "*/15 * * * *" -- every 15 minutes
- "0 * * * *" -- every hour
- "0 9 * * *" -- daily at 9am
- "0 9 * * 1-5" -- weekdays at 9am
- "0 9 * * 1" -- every Monday at 9am
- "0 9,17 * * *" -- 9am and 5pm daily

**audience_type** controls who receives a run:
- "users" -- one run per staff member (with optional role/user_ids filter)
- "tasks_by_assignee" -- one run per assignee who has open tasks (digest pattern)
- "contacts" -- one run per matching contact
- "deals" -- one run per matching deal

**audience_filter** is type-specific JSON:
- users: { "roles": ["client_editor"], "user_ids": ["uuid"] }
- tasks_by_assignee: { "statuses": ["open"], "include_overdue_only": true, "max_tasks_per_user": 20 }
- contacts: { "contact_type": "lead", "has_email": true, "limit": 500 }
- deals: { "status": "open", "stale_days": 14, "pipeline_id": "uuid", "stage_ids": ["uuid"], "limit": 200 }

Optionally set end_at (ISO timestamp) to stop firing after a date, or max_runs (integer) to auto-deactivate after N fires.

## Parameters

| Name | In | Type | Required | Description |
|------|----|------|----------|-------------|
| `name` | body | string | yes | Schedule name |
| `cron_expression` | body | string | yes | Standard 5-field cron expression |
| `audience_type` | body | string | yes | One of: users, tasks_by_assignee, contacts, deals |
| `automation_id` | body | uuid | yes | UUID of the automation to fire. Must belong to this workspace. |
| `description` | body | string | no | Optional description |
| `is_active` | body | boolean | no | Whether the schedule fires automatically (default true) |
| `timezone` | body | string | no | IANA timezone, e.g. "Australia/Sydney" (default) |
| `audience_filter` | body | object | no | Audience-specific filter JSON (see description above) |
| `end_at` | body | string | no | ISO timestamp after which the schedule stops firing |
| `max_runs` | body | number | no | Auto-deactivate after firing this many times |

## Request example

```bash
curl -X POST \
  "https://api.trustpager.com/functions/v1/api/v1/event-schedules" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: unique-key-here" \
  -d '{
    "name": "Daily Staff Digest",
    "cron_expression": "0 9 * * 1-5",
    "timezone": "Australia/Sydney",
    "audience_type": "users",
    "automation_id": "YOUR_AUTOMATION_ID"
  }'
```

## Response example

```json
{
  "data": {
    "id": "a1b2c3d4-...",
    "name": "Daily Staff Digest",
    "is_active": true,
    "cron_expression": "0 9 * * 1-5",
    "timezone": "Australia/Sydney",
    "audience_type": "users",
    "audience_filter": {},
    "automation_id": "b2c3d4e5-...",
    "next_run_at": "2026-04-21T23:00:00+00:00",
    "last_run_at": null,
    "run_count": 0,
    "max_runs": null,
    "end_at": null,
    "created_at": "2026-04-19T10:00:00Z",
    "updated_at": "2026-04-19T10:00:00Z"
  },
  "meta": { "credits_remaining": 9988 }
}
```

---
Base URL: `https://api.trustpager.com/functions/v1/api/v1` — Auth: `Authorization: Bearer YOUR_API_KEY`