# Create function triggers

> Subscribe a function to one or more event types, optionally scoped to specific senders. Provide eventTypes and senderIds (use null in senderIds for all senders); a trigger is created for each event type and sender combination.

`POST https://api.zavu.dev/v1/functions/{functionId}/triggers`

Subscribe a function to one or more event types, optionally scoped to specific senders. Provide eventTypes and senderIds (use null in senderIds for all senders); a trigger is created for each event type and sender combination.

The special event type `cron` runs the function on a schedule instead of a messaging event: include a `cron` field with a 5-field UTC cron expression (minimum granularity one minute). A cron trigger ignores the sender axis, and a function may hold several cron triggers with different expressions. The function receives an event with `type: "cron"` and `data.cron`.

## Parameters

- `functionId` (path, string, required): Zavu Function ID.

## Body

- `eventTypes` (string[], required): Event types to subscribe to.
- `senderIds` (string[], required): Senders to scope the triggers to. Use null for all senders.
- `cron` (string): Required when eventTypes includes `cron`: a 5-field cron expression (minute hour day-of-month month day-of-week), evaluated in UTC.

## Responses

### 201

Triggers created.

- `added` (integer, required)
- `skipped` (integer, required): Number of triggers that already existed.
- `triggers` (object[], required)
  - `id` (string, required)
  - `functionId` (string, required)
  - `eventType` (string, required): Event type that fires the function. See GET /v1/functions/event-types for the supported list. The special type `cron` fires on a schedule instead of a messaging event and carries a `cron` expression.
  - `senderId` (string, nullable): Restrict the trigger to a single sender. Null means all senders in the project.
  - `active` (boolean, required)
  - `createdAt` (string<date-time>, required)
  - `updatedAt` (string<date-time>, required)
  - `cron` (string, nullable): 5-field cron expression (minute hour day-of-month month day-of-week), evaluated in UTC. Present only on `cron` triggers.
  - `nextRunAt` (string<date-time>, nullable): Next scheduled fire time. Present only on `cron` triggers.
  - `lastRunAt` (string<date-time>, nullable): Last time the schedule fired. Null until the first fire.

### 400

Invalid request.

- `code` (string, required)
- `message` (string, required)
- `details` (object)

### 401

Unauthorized.

- `code` (string, required)
- `message` (string, required)
- `details` (object)

### 404

Function not found.

- `code` (string, required)
- `message` (string, required)
- `details` (object)

## Example

```bash
curl -X POST "https://api.zavu.dev/v1/functions/function_abc123/triggers" \
  -H "Authorization: Bearer $ZAVUDEV_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "eventTypes": [
    "message.inbound"
  ],
  "senderIds": [
    null
  ]
}'
```

```typescript
import Zavudev from "@zavudev/sdk";

const zavu = new Zavudev({ apiKey: process.env.ZAVUDEV_API_KEY });

const result = await zavu.functions.triggers.create("function_abc123", {
  eventTypes: [
    "message.inbound",
  ],
  senderIds: [
    null,
  ],
});

console.log(result);
```
