/functions/{name}/triggersCreate Function Trigger
Attaches an EVENT trigger to the named function: it fires on writes to a collection.
Body (camelCase):
collection(required)events(one or more ofinsert,update,delete,upsert)- an optional
filterin the JSON-policy grammar (for example{"eq": ["resource.status", "active"]}) limits invocations to matching records;nullfires on every matching write enabled(default true)logOkInvocations(default false)
Scheduled triggers are not created here: use POST /@{projectSlug}/forge/schedules.
Errors:
404 K9O_FUNCTION_NOT_FOUNDwhen the function does not exist or is retired404 K9O_COLLECTION_NOT_FOUNDwhen the collection does not exist400 K9O_POLICY_UNKNOWN_OPERATORfor a filter operator outside the grammar
Requires PROJECT_ADMIN.
| name * | path | The function's name. A module's operation is named |
| projectSlug * | path | The project's slug: the path segment after It is unique within the org. |
| One of: object | ||
| collection * | string | The collection whose writes fire the trigger. It must exist. |
| events * | "insert" | "update" | "delete" | "upsert"[] | The kinds of write that fire the trigger, 1 to 4 of |
| filter | any | A condition on the written record, as a JSON predicate in the policy grammar: The trigger fires only when it matches. Null or absent fires on every matching event. |
| One of: object | ||
| or: null | ||
| enabled | boolean | Whether the trigger fires. Default true. |
| logOkInvocations | boolean | Whether successful invocations are logged as well as failed ones. Default false. |
| or: object | ||
| bucket * | string | The bucket whose objects fire the trigger. It must exist and must not be a system bucket, and you must be able to read it. |
| events * | "object.created" | "object.deleted" | "object.expired"[] | The object events that fire the trigger, 1 to 3 of A function that receives |
| filter | any | Conditions on the object event, all of which must hold: Null or absent fires on every matching event. |
| One of: object | ||
| pathPrefix | string | Fire only for objects whose path starts with this, compared literally. |
| contentType | string | Fire only for this content type, or this family when it ends in |
| sizeBytes | object | Fire only for objects whose size is in this inclusive range, in bytes. |
| min | integer | The smallest size that fires, in bytes, inclusive. |
| max | integer | The largest size that fires, in bytes, inclusive. |
| or: null | ||
| enabled | boolean | Whether the trigger fires. Default true. |
| logOkInvocations | boolean | Whether successful invocations are logged as well as failed ones. Default false. |
| HTTP | K9O Code | Description |
|---|---|---|
| 201 | Created | |
| 400 | K9O_INVALID_FIELD_VALUE | The project slug, the function name or the collection name is not a valid identifier. |
| 400 | K9O_POLICY_UNKNOWN_OPERATOR | The filter uses an operator outside the JSON-policy grammar. |
| 401 | K9O_UNAUTHENTICATED | The caller presented no authenticated session. |
| 403 | K9O_ACCESS_DENIED | The caller has no access to this project. |
| 403 | K9O_IAM_POLICY_DENIED | The bound IAM policy ceiling denies creating function triggers; the default floor is PROJECT_ADMIN. |
| 404 | K9O_FUNCTION_NOT_FOUND | No function with that name exists in this project, or it is retired. |
| 404 | K9O_COLLECTION_NOT_FOUND | The collection named for the trigger does not exist in this project. |
| data * | object | A function trigger: the function runs when a matching write lands in the collection, or a matching object event happens in the bucket. |
| id * | string <uuid> | The trigger's id, a UUID. |
| functionName * | string | The function the trigger runs. |
| collection * | any | The collection whose writes fire the trigger; null on a bucket trigger. |
| One of: string | ||
| or: null | ||
| bucket * | any | The bucket whose object events fire the trigger; null on a collection trigger. |
| One of: string | ||
| or: null | ||
| events * | any[] | The events that fire the trigger: A function that receives |
| One of: "insert" | "update" | "delete" | "upsert" | ||
| or: "object.created" | "object.deleted" | "object.expired" | ||
| filter * | any | The condition an event must match: a record predicate on a collection trigger, object conditions on a bucket trigger. Null when the trigger fires on every matching event. |
| One of: object | ||
| or: null | ||
| enabled * | boolean | Whether the trigger fires. |
| logOkInvocations * | boolean | Whether successful invocations are logged as well as failed ones. |
| createdAt * | string <date-time> | When the trigger was created. |
| meta * | any | |
// const k9o = new K9o({ orgSlug: 'playground', projectSlug: 'storefront', apiKey: 'k9o_…' })
const { data } = await k9o.functions.triggers.create('greet', {
collection: 'products',
events: ['insert', 'update'],
filter: {
eq: ['resource.status', 'active']
},
enabled: true,
logOkInvocations: false
})Showing example in TypeScript
{
"collection": "products",
"events": [
"insert",
"update"
],
"filter": {
"eq": [
"resource.status",
"active"
]
},
"enabled": true,
"logOkInvocations": false
}{
"data": {
"id": "string",
"functionName": "string",
"collection": "string",
"bucket": "string",
"events": [
"insert"
],
"enabled": false,
"logOkInvocations": false,
"createdAt": "string"
}
}