In modern software development, automation is king. We use cron jobs for scheduled tasks and webhooks to react to external events. But what about the tasks you need to run right now, on-demand? Whether it's a user clicking a button in your UI or an internal script needing to kick off a complex process, you need a way to say, "Go!" This is where the power of the API trigger comes in.
An API trigger is a direct, programmatic way to initiate a workflow. It’s the "Go" button for your backend, giving you precise control over when your automations run. This guide will walk you through what API triggers are, why they are essential for any developer's toolkit, and how you can implement them effortlessly with trigger.do.
At its core, a workflow trigger is any event that starts an automated process. An API trigger is a specific type of trigger that is initiated by a direct API call.
Let's compare it to other common trigger types to understand its unique value:
With trigger.do, creating a robust, on-demand workflow is incredibly simple. You define your workflow in code, and we handle the boilerplate of creating a secure endpoint, managing retries, and providing detailed logs.
Let's build a practical example: a workflow to generate a data report for a user and email it to them. This is a classic on-demand task that might be initiated from a user's dashboard.
First, you define the event and the job it should run. Using the trigger.do SDK, your code is clean, readable, and lives right alongside your application logic.
import { trigger } from "@do/sdk";
import { generateReport, storeInS3 } from "./services";
// Define a trigger that can be called via an API endpoint
const onDemandReport = trigger.on("report.generate", {
name: "On-Demand User Report",
run: async (event, context) => {
// The payload comes directly from your API call
const { userId, format } = event.payload;
context.logger.info(`Starting ${format} report for user ${userId}...`);
// 1. Run your complex logic, like generating a file
const reportFile = await generateReport({ userId, format });
// 2. Upload it to a secure location like S3
const reportUrl = await storeInS3(reportFile);
// 3. Complete the workflow by sending a notification
await send.email({
to: event.user.email, // Assuming user info is available
subject: "Your requested report is ready!",
body: `You can download your report here: ${reportUrl}`,
});
context.logger.info("Successfully sent report to user.", { reportUrl });
return { success: true, url: reportUrl };
},
});
In this code, we've defined a trigger for the event report.generate. The run function contains the entire workflow logic.
trigger.do automatically creates a secure HTTP endpoint for your custom event. Now, any application—be it your frontend, a mobile app, or an internal script—can start this workflow with a simple POST request.
curl -X POST "https://api.trigger.do/v1/events/report.generate" \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"payload": {
"userId": "user_1a2b3c",
"format": "pdf"
}
}'
And that's it! You've successfully triggered a complex, multi-step backend process from a single, secure API call. trigger.do takes care of queuing the job, executing it, and handling any potential failures with automatic retries.
The simplicity of API triggers unlocks a massive range of possibilities:
API triggers bridge the gap between user actions and backend processes, providing the glue for modern, event-driven systems.
Q: What is a workflow trigger?
A: A trigger is a specific event that initiates a predefined workflow or agentic action. It's the starting point for any automated process, like receiving a webhook, a scheduled time, or a custom API call.
Q: What types of triggers does trigger.do support?
A: trigger.do supports a wide range of event sources, including HTTP webhooks from services like Stripe or GitHub, cron-based schedules for recurring tasks, direct API calls, and custom events from your own applications.
Q: Can I chain multiple workflows together?
A: Yes. The completion of one workflow can be configured as a custom event to trigger another, allowing you to build complex, multi-step automations and stateful agentic processes.
Q: How does trigger.do handle trigger failures or retries?
A: Our platform is built for reliability. We offer configurable automatic retries with exponential backoff for transient failures, detailed logging for debugging, and real-time monitoring to ensure your event-driven workflows run successfully.
By adding API triggers to your development process, you gain the power to orchestrate complex operations on-demand, creating more responsive, efficient, and powerful applications. You are no longer limited by fixed schedules or reactive webhooks.
With trigger.do, you can focus on writing the workflow logic you care about, while we handle the infrastructure that makes it all possible.
Ready to build your first on-demand automation? Get started with trigger.do today!