SDK
HTTP Reference
eventTrigger()
eventTrigger()
is set as a Job’s trigger to subscribe to an event a Job from a sent event.
//this Job subscribes to an event called new.user
client.defineJob({
id: "job-2",
name: "Second job",
version: "0.0.1",
//subscribes to the "new.user" event
trigger: eventTrigger({
//sent events with this name will trigger this Job
name: "new.user",
//the expected payload shape
schema: z.object({
userId: z.string(),
tier: z.union([z.literal("free"), z.literal("pro")]),
}),
//only events with the tier: "pro" will trigger this Job
filter: {
tier: ["pro"],
},
//(optional) example event object
examples: [
{
id: "issue.opened",
name: "Issue opened",
payload: {
userId: "1234",
tier: "free",
},
//optional
icon: "github",
},
],
}),
run: async (payload, io, ctx) => {
await io.logger.log("New pro user created", { userId: payload.userId });
//do stuff
},
});
There are two way to send an event that will trigger eventTrigger()
:
- Use client.sendEvent() from anywhere in your codebase.
- Use io.sendEvent() from inside a Job's
run()
function.
You can have multiple Jobs that subscribe to the same event, they will all trigger when the event gets delivered.
Parameters
The name of the event you are subscribing to. Must be an exact match (case sensitive).
A Zod schema that defines the shape of the
event payload. The default is z.any()
which is any
.
You can use this to filter events based on the source.
Used to filter which events trigger the Job.
An example:
{
name: ["John", "Jane"],
age: [18, 21]
}
This filter would match against an event with the following data:
{
"name": "Jane",
"age": 18,
"location": "San Francisco"
}
Used to provide example payloads that are accepted by the job.
This will be available in the dashboard and can be used to trigger test runs.
//this Job subscribes to an event called new.user
client.defineJob({
id: "job-2",
name: "Second job",
version: "0.0.1",
//subscribes to the "new.user" event
trigger: eventTrigger({
//sent events with this name will trigger this Job
name: "new.user",
//the expected payload shape
schema: z.object({
userId: z.string(),
tier: z.union([z.literal("free"), z.literal("pro")]),
}),
//only events with the tier: "pro" will trigger this Job
filter: {
tier: ["pro"],
},
//(optional) example event object
examples: [
{
id: "issue.opened",
name: "Issue opened",
payload: {
userId: "1234",
tier: "free",
},
//optional
icon: "github",
},
],
}),
run: async (payload, io, ctx) => {
await io.logger.log("New pro user created", { userId: payload.userId });
//do stuff
},
});
Was this page helpful?
//this Job subscribes to an event called new.user
client.defineJob({
id: "job-2",
name: "Second job",
version: "0.0.1",
//subscribes to the "new.user" event
trigger: eventTrigger({
//sent events with this name will trigger this Job
name: "new.user",
//the expected payload shape
schema: z.object({
userId: z.string(),
tier: z.union([z.literal("free"), z.literal("pro")]),
}),
//only events with the tier: "pro" will trigger this Job
filter: {
tier: ["pro"],
},
//(optional) example event object
examples: [
{
id: "issue.opened",
name: "Issue opened",
payload: {
userId: "1234",
tier: "free",
},
//optional
icon: "github",
},
],
}),
run: async (payload, io, ctx) => {
await io.logger.log("New pro user created", { userId: payload.userId });
//do stuff
},
});