Trigger APIs
Viewing:
Overview
You can create APIs to initiate workflows, data processing jobs, or other automated processes.
Basic Usage
app/apis/trigger_workflow.ts
import { ConsumptionApi } from "@514labs/moose-lib";
interface WorkflowParams {
inputValue: string;
priority?: string;
}
interface WorkflowResponse {
workflowId: string;
status: string;
}
const triggerApi = new ConsumptionApi<WorkflowParams, WorkflowResponse>(
"trigger-workflow",
async ({ inputValue, priority = "normal" }: WorkflowParams, { client }) => {
// Trigger the workflow with input parameters
const workflowExecution = await client.workflow.execute("data-processing", {
inputValue,
priority,
triggeredAt: new Date().toISOString()
}
);
return {
workflowId: workflowExecution.id,
status: "started"
};
}
);
export default triggerApi;
app/apis/trigger_workflow.py
from moose_lib import MooseClient, ConsumptionApi
from pydantic import BaseModel, Field
from datetime import datetime
class WorkflowParams(BaseModel):
input_value: str
priority: str = Field(default="normal")
class WorkflowResponse(BaseModel):
workflow_id: str
status: str
def run(params: WorkflowParams, client: MooseClient) -> WorkflowResponse:
# Trigger the workflow with input parameters
workflow_execution = client.workflow.execute(
workflow="data-processing",
params={
"input_value": params.input_value,
"priority": params.priority,
"triggered_at": datetime.now().isoformat()
}
)
return WorkflowResponse(
workflow_id=workflow_execution.id,
status="started"
)
api = ConsumptionApi[WorkflowParams, WorkflowResponse]("trigger-workflow", run)
Using the Trigger API
Once deployed, you can trigger workflows via HTTP requests:
Terminal
curl "http://localhost:4000/consumption/trigger-workflow?inputValue=process-user-data&priority=high"
Response:
{
"workflowId": "workflow-12345",
"status": "started"
}