dify-task-scheduler
is a lightweight task scheduling tool designed to automatically trigger Dify workflows via API at predefined times.
It simplifies integration with Dify by enabling scheduled execution of agents or workflows using cron-like configurations.
π Features
- Cron-based scheduling using GitHub Actions (or self-hosted Node runtime)
- Multiple API tokens support with automatic loop execution
- Flexible token configuration using semicolon-separated format
- Environment variable configuration for secure API credentials
- Clean code structure with future extensibility in mind
- Error handling and logging for reliable execution
β Ideal For
- Automating your Dify agents or workflows on a schedule
- Integrating Dify with CI/CD pipelines or automated routines
- Laying the foundation for more advanced task automation tools
- A Dify account with API access
- GitHub repository (for GitHub Actions scheduling)
- Node.js 18+ (only for local development)
This is the easiest way to get started - no local setup required!
- Go to https://github.com/Simoon-F/dify-task-scheduler
- Click the Fork button in the top right corner
- This creates a copy of the repository in your GitHub account
Alternatively, you can clone the repository directly:
git clone https://github.com/Simoon-F/dify-task-scheduler.git
cd dify-task-scheduler
- Go to your repository's Settings β Secrets and variables β Actions -> Repository secrets
- Add the following secrets:
DIFY_BASE_URL
(optional, defaults tohttps://api.dify.ai/v1
)DIFY_TOKEN
(required - your Dify workflow token)DIFY_INPUTS
(optional, JSON string like{"key":"value"}
)DIFY_RESPONSE_MODE
(optional, defaults toblocking
)DIFY_USER
(optional, defaults toscheduler-user
)
The workflow will automatically run daily at 9:00 AM Beijing Time (1:00 AM UTC). You can also trigger it manually:
- Go to Actions tab in your repository
- Select "Dify Task Scheduler"
- Click "Run workflow"
Edit .github/workflows/scheduler.yml
to change the schedule:
schedule:
# Run at 1:00 AM UTC (9:00 AM Beijing Time)
- cron: '0 1 * * *'
For testing and development purposes:
npm install
# or
yarn install
Create a .env
file in the root directory:
DIFY_BASE_URL=https://api.dify.ai/v1
# Single token
DIFY_TOKEN=your-dify-workflow-token
# Multiple tokens separated by semicolons
# DIFY_TOKEN=token1;token2;token3
DIFY_INPUTS={"key":"value"}
DIFY_RESPONSE_MODE=blocking
DIFY_USER=scheduler-user
yarn dev
Variable | Description | Default | Required |
---|---|---|---|
DIFY_BASE_URL |
Dify API base URL | https://api.dify.ai/v1 |
No |
DIFY_TOKEN |
Your Dify workflow token(s). Support single token or multiple tokens separated by semicolons | - | Yes |
DIFY_INPUTS |
JSON string of workflow inputs | {} |
No |
DIFY_RESPONSE_MODE |
Response mode (blocking or streaming ) |
blocking |
No |
DIFY_USER |
User identifier for the workflow | scheduler-user |
No |
You can configure multiple API tokens for batch execution:
# Multiple tokens separated by semicolons
DIFY_TOKEN=app-token1;app-token2;app-token3
The scheduler will automatically loop through all tokens:
$ yarn dev
Found 3 API tokens, starting loop execution...
Using token 1/3: app-token1...
β
Token 1 execution successful
Using token 2/3: app-token2...
β
Token 2 execution successful
Using token 3/3: app-token3...
β
Token 3 execution successful
dify-task-scheduler/
βββ .github/
β βββ workflows/
β βββ scheduler.yml # GitHub Actions workflow
βββ src/
β βββ config.js # Environment configuration
β βββ dify-api.js # Dify API client
β βββ dify-workflow.js # Workflow task class
β βββ index.js # Main entry point
β βββ scheduler.js # Scheduler logic
β βββ utils.js # Utility functions
βββ package.json
βββ README.md
βββ .env.example # Environment variables template
Once configured, your workflows will run automatically according to the schedule. You can monitor execution:
- Go to Actions tab in your repository
- View workflow runs and logs
- Check execution results and any errors
You can manually trigger the workflow anytime:
- Go to Actions β Dify Task Scheduler
- Click Run workflow
- Select branch and click Run workflow
For development and testing:
# Test single execution
node src/index.js
# Run with development mode
yarn dev
const { startScheduler } = require('./src/scheduler');
async function runWorkflow() {
try {
await startScheduler();
console.log('Workflow completed successfully');
} catch (error) {
console.error('Workflow failed:', error);
}
}
runWorkflow();
For contributors and advanced users:
-
Copy environment variables:
cp .env.example .env
-
Edit
.env
with your Dify credentials -
Run the scheduler:
yarn dev
# Test workflow execution
node src/index.js
# Run with development logging
DEBUG=* yarn dev
-
GitHub Actions not running
- Check that your repository has Actions enabled in Settings
- Verify the cron schedule syntax in
.github/workflows/scheduler.yml
- Ensure you have the necessary permissions to run Actions
-
"DIFY_TOKEN environment variable is required"
- Go to repository Settings β Secrets and variables β Actions
- Add
DIFY_TOKEN
secret with your Dify workflow token - Make sure the secret name matches exactly
-
Workflow fails with API errors
- Verify your Dify token is valid and has correct permissions
- Check if your Dify workflow/agent is active
- Review the Actions logs for detailed error messages
-
"Cannot find module" errors
- Run
yarn install
ornpm install
to install dependencies - Ensure you're in the correct project directory
- Run
-
Environment variable issues
- Copy
.env.example
to.env
and fill in your values - Ensure
DIFY_TOKEN
is set correctly in your.env
file
- Copy
-
API authentication errors
- Verify your Dify token is valid and has the correct permissions
- Check if the
DIFY_BASE_URL
is correct for your Dify instance
- Fork the repository
- Create a feature branch:
git checkout -b feature/amazing-feature
- Commit your changes:
git commit -m 'Add amazing feature'
- Push to the branch:
git push origin feature/amazing-feature
- Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
If you encounter any issues or have questions:
- Check the Issues page
- Create a new issue with detailed information