Triggers

Execute workflows via API, webhooks, or schedules.

Trigger Types

πŸ”Œ

API

Execute on-demand from your application code.

πŸͺ

Webhook

Trigger from external services (Slack, GitHub, etc.).

⏰

Schedule

Run automatically on a cron schedule.

API Execution

Execute workflows directly from your application:

TypeScript
// Execute a workflow via API
const response = await fetch('https://api.llmhub.dev/v1/workflow/execute', {
  method: 'POST',
  headers: {
    'Authorization': 'Bearer your-api-key',
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    workflow_id: 'wf_abc123',
    input: {
      message: 'Hello, how can I help you?',
      user_id: 'user_xyz'
    },
    stream: false  // Set true for SSE streaming
  })
});

const result = await response.json();
console.log(result);
// {
//   "execution_id": "exec_def456",
//   "status": "completed",
//   "output": { "response": "I'm here to help! What do you need?" },
//   "usage": { "prompt_tokens": 150, "completion_tokens": 25 },
//   "duration_ms": 1234
// }

Streaming Execution

Stream workflow progress and outputs in real-time using Server-Sent Events:

TypeScript
// Execute with SSE streaming
const response = await fetch('https://api.llmhub.dev/v1/workflow/execute', {
  method: 'POST',
  headers: {
    'Authorization': 'Bearer your-api-key',
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    workflow_id: 'wf_abc123',
    input: { message: 'Tell me a story' },
    stream: true
  })
});

const reader = response.body?.getReader();
const decoder = new TextDecoder();

while (reader) {
  const { done, value } = await reader.read();
  if (done) break;
  
  const chunk = decoder.decode(value);
  const lines = chunk.split('\n');
  
  for (const line of lines) {
    if (line.startsWith('event: ')) {
      const eventType = line.slice(7);
      console.log('Event:', eventType);
    }
    if (line.startsWith('data: ')) {
      const data = JSON.parse(line.slice(6));
      console.log('Data:', data);
    }
  }
}

// Events:
// event: node_start
// data: {"nodeId":"llm_1","timestamp":"..."}
//
// event: node_output
// data: {"nodeId":"llm_1","chunk":"Once upon a time..."}
//
// event: node_complete
// data: {"nodeId":"llm_1","duration":1234}
//
// event: workflow_complete
// data: {"output":{...},"usage":{...}}
EventDescription
node_startNode begins executing
node_outputPartial output from a streaming node
node_completeNode finished with final output
node_errorNode encountered an error
workflow_completeWorkflow finished successfully
workflow_errorWorkflow failed

Webhook Triggers

Create webhook endpoints that trigger your workflow when called:

TypeScript
// Create a webhook trigger for a workflow
const response = await fetch('https://api.llmhub.dev/v1/webhooks', {
  method: 'POST',
  headers: {
    'Authorization': 'Bearer your-api-key',
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    workflow_id: 'wf_abc123',
    name: 'Customer Support Webhook',
    path: 'support-bot',  // Becomes /v1/hooks/support-bot
    method: 'POST',
    secret: 'your-hmac-secret'  // For signature verification
  })
});

const webhook = await response.json();
// {
//   "id": "wh_xyz789",
//   "url": "https://api.llmhub.dev/v1/hooks/support-bot",
//   "secret": "your-hmac-secret"
// }

Triggering the Webhook

External services can call your webhook URL:

Bash
// Trigger the workflow via webhook (from external service)
curl -X POST https://api.llmhub.dev/v1/hooks/support-bot \
  -H "Content-Type: application/json" \
  -H "X-Webhook-Signature: sha256=abc123..." \
  -d '{"customer_id": "c_123", "message": "I need help with my order"}'

# Response:
# {
#   "execution_id": "exec_def456",
#   "status": "completed",
#   "output": { "response": "I'd be happy to help..." }
# }

Signature Verification

Include the X-Webhook-Signature header with an HMAC-SHA256 signature of the request body using your secret. We verify this before executing the workflow.

Input Mapping

The webhook body is passed as the workflow input. Use the Start node to define the expected schema.

Scheduled Triggers

Run workflows automatically on a schedule using cron expressions:

TypeScript
// Create a scheduled trigger
const response = await fetch('https://api.llmhub.dev/v1/schedules', {
  method: 'POST',
  headers: {
    'Authorization': 'Bearer your-api-key',
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    workflow_id: 'wf_abc123',
    name: 'Daily Report Generator',
    cron: '0 9 * * *',  // Every day at 9 AM
    timezone: 'America/New_York',
    input: {
      report_type: 'daily_summary'
    },
    enabled: true
  })
});

const schedule = await response.json();
// {
//   "id": "sch_abc123",
//   "next_run": "2024-01-16T09:00:00-05:00",
//   "cron": "0 9 * * *"
// }

Cron Expression Examples

text
// Common cron expressions
"0 * * * *"       // Every hour at minute 0
"*/15 * * * *"    // Every 15 minutes
"0 9 * * *"       // Daily at 9:00 AM
"0 9 * * 1-5"     // Weekdays at 9:00 AM
"0 0 * * 0"       // Weekly on Sunday at midnight
"0 0 1 * *"       // Monthly on the 1st at midnight
"0 9,17 * * *"    // Daily at 9 AM and 5 PM

Cron Format

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ minute (0 - 59)
β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ hour (0 - 23)
β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ day of month (1 - 31)
β”‚ β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ month (1 - 12)
β”‚ β”‚ β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ day of week (0 - 6, Sunday = 0)
β”‚ β”‚ β”‚ β”‚ β”‚
* * * * *

Managing Triggers

List Triggers

GET /v1/workflows/{id}/triggers

Enable/Disable

PATCH /v1/webhooks/{id} { "enabled": false }

Delete Trigger

DELETE /v1/schedules/{id}

View Execution History

GET /v1/workflow/executions?workflow_id=wf_abc123

Best Practices

Always Verify Webhook Signatures

Don't skip signature verification in productionβ€”it prevents unauthorized workflow execution.

Use Streaming for Long Workflows

For workflows that take more than a few seconds, use streaming to avoid timeouts and show progress.

Set Appropriate Timeouts

Configure timeouts on your HTTP client. Workflow execution has a maximum of 5 minutes.

Handle Retries for Schedules

Scheduled workflows that fail are retried up to 3 times with exponential backoff. Design your workflow to be idempotent.

Next Steps