- Functions
- Send event
Functions
Send event
Send events to trigger your custom events workflows.
To trigger your custom event workflows, you’ll need to send an event to our API. You can do this via our @trigger.dev/sdk
package or via our HTTP API.
Sending events via @trigger.dev/sdk
If you are using our Node.js SDK, you can send events via the sendEvent
function, like so:
import { sendEvent } from "@trigger.dev/sdk";
import { randomUUID } from "node:crypto";
//send an event with the name "user.created"
//and the payload
await sendEvent(randomUUID(), {
name: "user.created",
payload: { userId: "123456" },
});
The first parameter is a unique ID for the event. This is used to deduplicate events, so if you send the same event twice, the second event will be ignored. You can use the randomUUID
function from Node.js to generate a unique ID.
The second parameter is the event itself. It must have a name
and a payload
property. The name
is the name of the event you want to trigger, and the payload
is the data you want to send as the event in the Trigger.run
function.
API Key
It’s not accepted as a parameter, but the sendEvent
function will automatically use the TRIGGER_API_KEY
environment variable as the API key. The API key determines which environment will receive the event. If you want to send events to a different environment, you can set the TRIGGER_API_KEY
environment variable to the API key of the environment you want to send events to. See our Environments & API Keys Guide for more info.
Sending events as a step in a workflow
You can also send events as a step in a workflow, by calling sendEvent
inside the Trigger.run
function, like so:
import { Trigger, scheduleEvent, sendEvent } from "@trigger.dev/sdk";
new Trigger({
id: "check-scheduler",
name: "Check Scheduler",
on: scheduleEvent({ rateOf: { minutes: 10 } }),
run: async (event, context) => {
await sendEvent("send-event", {
name: "health.check",
payload: {
url: "https://trigger.dev",
host: "trigger.dev",
},
});
},
}).listen();
Notice that the first parameter is no longer the event ID, but rather the key
. The key is a string that is unique across all actions in the workflow run. See our Keys and Resumability Guide for more info.
The API key will be automatically set to the API key of the environment that the workflow is running in.
Sending events via HTTP API
You can also send events via our HTTP API. We have comprehensive documentation for the events
endpoint here, but here are some examples in various languages:
curl
ruby
python
go
curl --request POST \
--url https://app.trigger.dev/api/v1/events \
--header 'Authorization: Bearer <your api key here>' \
--header 'Content-Type: application/json' \
--data '{
"id": "<unique event id>",
"event": {
"name": "user.created",
"payload": {
"userId": "123456"
}
}
}'
Notice that the API key is set in the Authorization
header, which will determine which environment will receive the event. If you want to send events to a different environment, you can set the Authorization
header to the API key of the environment you want to send events to. See our Environments & API Keys Guide for more info.
Delaying event delivery
Through both our Node.js SDK and our HTTP API, you can delay the delivery of an event. This is useful if you want to send an event to trigger a workflow, but you want to wait for a certain amount of time before the event is delivered.
Using the health check example above, we can delay the delivery of the health.check
event in various ways:
new Trigger({
id: "check-scheduler",
name: "Check Scheduler",
on: scheduleEvent({ rateOf: { minutes: 10 } }),
run: async (event, context) => {
await sendEvent("health.check trigger.dev", {
name: "health.check",
payload: {
url: "https://trigger.dev",
host: "trigger.dev",
},
delay: { until: new Date(Date.now() + 1000 * 60 * 5) }, // Delay until a specific date
});
await sendEvent("health.check docs.trigger.dev", {
name: "health.check",
payload: {
url: "https://docs.trigger.dev",
host: "docs.trigger.dev",
},
delay: { minutes: 5 }, // Delay for a specific amount of time
});
await sendEvent("health.check app.trigger.dev", {
name: "health.check",
payload: {
url: "https://app.trigger.dev/healthcheck",
host: "app.trigger.dev",
},
delay: { seconds: 30 }, // Delay for a specific amount of time
});
},
}).listen();
When sending events in the context of a workflow run, adding a delay DOES NOT add a delay to the workflow run, as sending a custom event is a fire-and-forget action.
You can add delays via the HTTP API by adding a delay
object to the event:
curl --request POST \
--url https://app.trigger.dev/api/v1/events \
--header 'Authorization: Bearer <insert_api_key_here>' \
--header 'Content-Type: application/json' \
--data '{
"id": "<unique event id>",
"event": {
"name": "user.created",
"payload": {
"userId": "123456"
},
"delay": {
"seconds": 30
}
}
}'