Triggers Overview
Triggers
Section titled “Triggers”Microfn functions can be invoked through multiple trigger mechanisms, allowing you to execute your serverless code in response to various events and conditions. Each trigger type serves different use cases and integration patterns.
Available Triggers
Section titled “Available Triggers”Microfn supports three primary trigger types:
Trigger Type | Description | Use Cases |
---|---|---|
HTTP Webhooks | Direct invocation via HTTP POST requests | APIs, integrations, real-time processing |
Scheduled (Cron) | Time-based automatic execution | Batch jobs, maintenance, reports |
Trigger functions by sending emails | Email automation, notifications, workflows |
Quick Comparison
Section titled “Quick Comparison”Feature | Webhooks | Cron | |
---|---|---|---|
Invocation | HTTP POST | Automatic schedule | Send email |
Input | JSON payload | Empty {} or custom | Email data |
Authentication | Bearer token / Public | N/A (internal) | Email address |
Response | Synchronous | N/A (async) | N/A (async) |
Rate Limiting | Yes | Schedule-based | Yes |
Setup | Automatic | Configure schedule | Generate address |
Webhook Triggers
Section titled “Webhook Triggers”HTTP webhooks provide direct, synchronous function execution:
curl -X POST https://microfn.dev/run/username/function-name \ -H "Content-Type: application/json" \ -d '{"input": "data"}'
Best for:
- REST APIs
- Third-party integrations
- Real-time processing
- Synchronous workflows
Scheduled Triggers (Cron)
Section titled “Scheduled Triggers (Cron)”Cron triggers execute functions automatically on a schedule:
// Configured in workspace settings// Cron expression: "0 9 * * *" (daily at 9 AM)
export default async function main() { // Function receives empty input {} await performDailyTasks(); return { success: true };}
Best for:
- Scheduled reports
- Data synchronization
- Cleanup tasks
- Recurring workflows
Email Triggers
Section titled “Email Triggers”Email triggers execute functions when emails are received:
// Email sent to: ws-abc123xyz@mail.microfn.dev
export default async function main(input) { // input contains email data const { from, subject, body } = input;
await processEmail(from, subject, body); return { processed: true };}
Best for:
- Email automation
- Support tickets
- Notification processing
- Email-to-task conversion
Choosing the Right Trigger
Section titled “Choosing the Right Trigger”Use Webhooks When:
Section titled “Use Webhooks When:”- You need immediate, synchronous responses
- Integrating with external services
- Building REST APIs
- Handling user-initiated actions
Use Cron When:
Section titled “Use Cron When:”- Tasks run on a regular schedule
- No external input is needed
- Performing batch operations
- Running maintenance tasks
Use Email When:
Section titled “Use Email When:”- Integrating with email workflows
- Processing incoming notifications
- Converting emails to actions
- Building email-based automation
Combining Triggers
Section titled “Combining Triggers”Functions can be triggered through multiple mechanisms:
export default async function main(input) { // Determine trigger source const triggerSource = input.triggerSource || "webhook";
switch (triggerSource) { case "cron": // Scheduled execution logic return await handleScheduledRun();
case "email": // Email trigger logic return await processEmail(input);
default: // Webhook logic return await handleWebhook(input); }}
Security Considerations
Section titled “Security Considerations”Each trigger type has different security implications:
Webhook Security
Section titled “Webhook Security”- Use authentication tokens for private functions
- Validate input data
- Implement rate limiting
- Use HTTPS always
Cron Security
Section titled “Cron Security”- Runs with workspace permissions
- No external access needed
- Secure by default
Email Security
Section titled “Email Security”- Unique, unguessable email addresses
- Validate sender if needed
- Process untrusted content safely
- Implement spam protection
Monitoring and Logging
Section titled “Monitoring and Logging”All trigger executions are logged with:
- Trigger type and source
- Execution time and duration
- Input parameters
- Output and errors
- Usage metrics
Access logs through:
- Microfn dashboard
- Execution history API
- Real-time monitoring
Limits and Quotas
Section titled “Limits and Quotas”Trigger limits vary by subscription tier:
Limit | Free | Pro | Enterprise |
---|---|---|---|
Webhook calls/month | 10,000 | 100,000 | Unlimited |
Cron jobs | 5 | 50 | Unlimited |
Email triggers | 1 | 10 | Unlimited |
Min cron interval | 1 hour | 5 minutes | 1 minute |
Best Practices
Section titled “Best Practices”- Choose the right trigger for your use case
- Implement error handling for all trigger types
- Log trigger source for debugging
- Set up monitoring for critical triggers
- Test thoroughly with each trigger type
- Document trigger requirements in your function
- Use appropriate timeouts for long-running tasks
Next Steps
Section titled “Next Steps”Explore each trigger type in detail:
- HTTP Webhooks - Learn about webhook configuration and authentication
- Scheduled Execution (Cron) - Set up time-based triggers
- Email Triggers - Configure email-based function invocation