✨ Trigger.dev Basic Starter
This repo is a very simple starting point for creating your Trigger.dev workflows.
Currently this repo only has a single customEvent trigger:
import { Trigger, customEvent } from "@trigger.dev/sdk";
import { z } from "zod";
new Trigger({
// Give your Trigger a stable ID
id: "basic-starter",
name: "Basic Starter",
// Trigger on a custom event, see https://docs.trigger.dev/triggers/custom-events
on: customEvent({
name: "basic.starter",
// Use zod to verify event payload. See https://docs.trigger.dev/guides/zod
schema: z.object({ id: z.string() }),
}),
// The run functions gets called once per "basic.starter" event
async run(event, ctx) {
// Call external services, add delays, and more here.
await ctx.logger.info("Hello world from inside trigger.dev");
// Returned data will become the run "output" and is optional
return event;
},
}).listen();✍️ Customize
You can easily adapt this workflow to a different event relevant to your app. For example, we have a workflow that runs when a user is created and it looks like this:
import { Trigger, customEvent } from "@trigger.dev/sdk";
import * as slack from "@trigger.dev/slack";
import { z } from "zod";
new Trigger({
id: "new-user",
name: "New user",
on: customEvent({
name: "user.created",
schema: z.object({ id: z.string() }),
}),
async run(event, ctx) {
const user = await prisma.user.find({
where: { id: event.id },
});
await slack.postMessage("🚨", {
channelName: "new-users",
text: `New user signed up: ${user.email}`,
});
},
}).listen();Be sure to check out more over on our docs
🚀 Deploy
We've made it really easy to deploy this repo to Render.com, if you don't already have a Node.js server to host your triggers.
Render.com is a super-fast way to deploy webapps and servers (think of it like a modern Heroku)
Note Make sure you use your "live" trigger.dev API Key when deploying to a server
💻 Run locally
First, in your terminal of choice, clone the repo and install dependencies:
git clone https://github.com/triggerdotdev/basic-starter.git
cd basic-starter
npm installThen execute the following command to create a .env file with your development Trigger.dev API Key:
echo "TRIGGER_API_KEY=<APIKEY>" >> .envAnd finally you are ready to run the process:
npm run devYou should see a message output in your terminal like the following:
[trigger.dev] ✨ Connected and listening for events [basic-starter]
🧪 Test it
After successfully running this template locally, head over to your Trigger.dev Dashboard and you should see your newly created workflow:
Click on the workflow in the list and you should come to the Workflow overview page:
Click on the "Test your workflow" button and fill in the JSON needed for this workflow's customEvent Trigger:
After click "Run Test" you'll be redirected to the Run Details page:
📺 Go Live
After you are happy with your campaign and deploy it live to Render.com (or some other hosting service), you can send custom events that Trigger your workflow using the sendEvent function from the @trigger.dev/sdk, or simply by making requests to our events API endpoint.
Here is an example of sending the custom event to trigger the workflow contained in this repo using fetch:
const eventId = ulid(); // Generate a unique event ID
const event = {
name: "basic.starter",
payload: {
// This should match the zod schema provided in the `customEvent.schema` option
id: "user_1234",
},
};
const response = await fetch("https://app.trigger.dev/api/v1/events", {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${process.env.TRIGGER_API_KEY}`,
},
body: JSON.stringify({
id: eventId,
event,
}),
});