Airflow REST API & CLI Automation
Running Airflow Like a Production Platform π€βοΈβ
The Story: When Clicking Is No Longer an Optionβ
At the beginning, everything happens in the Airflow UI:
- Trigger a DAG manually
- Clear failed tasks
- Pause a DAG
- Check logs
It works⦠until:
- You have hundreds of DAGs
- Multiple environments (dev, stage, prod)
- CI/CD pipelines deploying DAGs daily
- External systems needing to trigger workflows
At this stage, clicking buttons is no longer professional.
Production Airflow is automated Airflow.
Thatβs where the REST API and CLI become essential.
Why REST API & CLI Matter in Airflowβ
Using API and CLI enables:
β
CI/CD-driven workflows
β
Event-based DAG triggering
β
Automated backfills
β
Platform governance
β
Integration with external systems
In short: Airflow becomes controllable by code.
Airflow REST API Overviewβ
Apache Airflow provides a stable REST API (v1) that allows you to:
- Trigger DAG runs
- Pause / unpause DAGs
- Fetch DAG and task status
- Clear task instances
- Retrieve logs and metadata
π Available from Airflow 2.x onward.
Authentication Basics (High Level)β
Airflow REST API typically uses:
- Basic Auth
- JWT / OAuth (with RBAC)
- Cloud provider IAM (managed Airflow)
Authentication depends on your Airflow deployment β but API usage remains consistent.
Triggering a DAG Using REST APIβ
Example: Trigger DAG Runβ
curl -X POST "http://localhost:8080/api/v1/dags/sales_report_dag/dagRuns" \
-H "Content-Type: application/json" \
-u admin:admin \
-d '{
"conf": {
"region": "EU",
"run_type": "manual"
}
}'
Inputβ
| Input |
|---|
DAG ID: sales_report_dag |
Config: { region: "EU" } |
Outputβ
{
"dag_id": "sales_report_dag",
"dag_run_id": "manual__2024-01-10T12:00:00",
"state": "queued"
}
Using DAG Run Configuration in Codeβ
def generate_report(**context):
conf = context["dag_run"].conf
region = conf.get("region", "US")
print(f"Generating report for {region}")
π This enables parameterized, event-driven DAGs.
Pause & Unpause DAGs via REST APIβ
curl -X PATCH "http://localhost:8080/api/v1/dags/sales_report_dag" \
-H "Content-Type: application/json" \
-u admin:admin \
-d '{ "is_paused": true }'
Outputβ
| Output |
|---|
| DAG paused successfully |
Fetch DAG Run Statusβ
curl -X GET "http://localhost:8080/api/v1/dags/sales_report_dag/dagRuns" \
-u admin:admin
Outputβ
{
"total_entries": 1,
"dag_runs": [
{
"state": "success",
"execution_date": "2024-01-10T00:00:00"
}
]
}
Airflow CLI Overviewβ
The Airflow CLI is ideal for:
- Local development
- Debugging
- Operational scripts
- Admin automation
Commonly Used Airflow CLI Commandsβ
Trigger a DAGβ
airflow dags trigger sales_report_dag \
--conf '{"region":"US"}'
Inputβ
| Input |
|---|
| DAG ID |
| JSON config |
Outputβ
| Output |
|---|
| DAG triggered successfully |
Pause / Unpause DAGβ
airflow dags pause sales_report_dag
airflow dags unpause sales_report_dag
Clear Task Instancesβ
airflow tasks clear sales_report_dag \
--start-date 2024-01-01 \
--end-date 2024-01-05 \
--yes
π Extremely useful for automated recovery.
REST API vs CLI (When to Use What)β
| Use Case | REST API | CLI |
|---|---|---|
| CI/CD pipelines | β | β |
| External system triggers | β | β |
| Local debugging | β | β |
| Admin scripts | β οΈ | β |
| Cloud automation | β | β |
Real-World Automation Use Casesβ
β
Trigger DAGs from web applications
β
Auto-pause failing DAGs
β
Release pipelines triggering backfills
β
Monitoring systems reacting to failures
β
Multi-environment orchestration
Common Mistakes in Airflow Automationβ
β Hardcoding credentials
β No RBAC restrictions
β Overusing CLI in production automation
β Missing idempotency in API triggers
Best Practices for REST API & CLI Automationβ
β
Use REST API for external automation
β
Secure API with RBAC & least privilege
β
Log and monitor API-triggered DAG runs
β
Version-control automation scripts
β
Prefer API over database-level hacks
Summary π§ β
- Airflow UI is for humans β APIs are for systems.
- REST API enables scalable, secure automation.
- CLI is perfect for debugging and admin tasks.
- Automation is mandatory for production-grade Airflow.
Key Takeawaysβ
- Manual operations donβt scale.
- REST API unlocks event-driven workflows.
- CLI simplifies operational tasks.
- Professional Airflow platforms are automated by design.
Whatβs Next?β
β‘οΈ Airflow Governance & Code Review Standards (Enterprise-Ready Airflow)