docs/src/course/04-workflows/08-running-workflows-programmatically.md
Learn how to execute your workflows from code, which is essential for integrating them into applications.
Create a new file to test programmatic execution:
// src/run-workflow.ts
import { mastra } from './mastra'
async function runContentWorkflow() {
console.log('🚀 Running workflow programmatically...\n')
try {
// Get the workflow instance
const workflow = mastra.getWorkflow('contentWorkflow')
if (!workflow) {
throw new Error('Workflow not found')
}
// Create a run instance
const run = await workflow.createRun()
// Execute with test data
const result = await run.start({
inputData: {
content:
'Climate change is one of the most pressing challenges of our time, requiring immediate action from governments, businesses, and individuals worldwide.',
type: 'blog',
},
})
if (result.status === 'success') {
console.log('✅ Success!')
console.log('📊 Reading time:', result.result.metadata.readingTime, 'minutes')
console.log('🎯 Difficulty:', result.result.metadata.difficulty)
console.log('📅 Processed at:', result.result.metadata.processedAt)
}
} catch (error) {
console.error('❌ Error:', (error as Error).message)
}
}
// Run the workflow
runContentWorkflow()
Execute your workflow runner:
npx tsx src/run-workflow.ts
mastra.getWorkflow(id): Gets a registered workflow by IDworkflow.createRun(): Creates a new execution instancerun.start(inputData): Executes the workflow with provided dataThe start() method returns:
success: Boolean indicating if workflow completed successfullyresult: The final output from the workflowexecutionTime: How long the workflow took to runYour workflow can now be run from anywhere in your application! Next, you'll learn about error handling.