Your First Airflow DAG: A Hands-On Tutorial for Airflow 3 (TaskFlow API)
Short answer: an Airflow DAG is a Python file that lists tasks and the order they run in. In Airflow 3 you import `DAG` from `airflow.sdk`, create tasks with operators or the `@task` decorator, and connect them with `>>`. This guide gives you two working DAGs, the classic style and the TaskFlow style, and you can run both in a free Docker lab.
What a DAG is
DAG stands for directed acyclic graph: tasks (the nodes) with an order (the arrows) and no loops. The Python file only describes the workflow. Airflow reads the file, then the scheduler runs each task when its upstream tasks are done.
DAG 1: two tasks with operators
import pendulum
from airflow.sdk import DAG
from airflow.providers.standard.operators.bash import BashOperator
with DAG(
dag_id="first_dag",
start_date=pendulum.datetime(2026, 1, 1, tz="UTC"),
schedule=None, # None: it only runs when you trigger it
catchup=False,
tags=["lesson"],
) as dag:
say_hello = BashOperator(task_id="say_hello", bash_command="echo 'Hello from Airflow'")
print_date = BashOperator(task_id="print_date", bash_command="date")
say_hello >> print_date # >> means "then"- `dag_id` is the name you see in the UI. It must be unique.
- `schedule=None` means it runs only when you trigger it. Use `"@daily"` for a daily run.
- `catchup=False` stops Airflow from creating runs for every past interval. Schedules and catchup explains this in full.
- `say_hello >> print_date` sets the order: `print_date` waits for `say_hello`.
DAG 2: the TaskFlow API
With TaskFlow, plain Python functions become tasks and a returned value reaches the next task. Airflow passes it for you through an XCom (a small message stored between tasks).
import pendulum
from airflow.sdk import dag, task
@dag(start_date=pendulum.datetime(2026, 1, 1, tz="UTC"), schedule=None, catchup=False)
def etl_taskflow():
@task
def extract():
return [{"order_id": 1, "amount": 120.5}, {"order_id": 2, "amount": 80.0}, {"order_id": 3, "amount": 45.25}]
@task
def transform(orders):
total = sum(o["amount"] for o in orders)
return {"orders": len(orders), "revenue": round(total, 2)}
@task
def load(summary):
print(f"Loaded {summary['orders']} orders, revenue {summary['revenue']}")
load(transform(extract()))
etl_taskflow()The last line `load(transform(extract()))` is the whole dependency chain. Airflow works out the order from how you call the functions.
Run it
1. Start the Airflow Lab (Docker Desktop and two commands).
2. Open lesson 1, Your first DAG, and press **Test DAG**. Both tasks run once and the output appears in the terminal.
3. Change the `echo` text and press Test DAG again.
4. Open lesson 3, TaskFlow and XCom, and look for `Loaded 3 orders` in the output.
Airflow 2 or Airflow 3?
Old tutorials use the Airflow 2 imports. If a copied example fails with an import error, this is usually why.
| | Airflow 2 | Airflow 3 | |---|---|---| | DAG | `from airflow import DAG` | `from airflow.sdk import DAG` | | Decorators | `from airflow.decorators import dag, task` | `from airflow.sdk import dag, task` | | BashOperator | `from airflow.operators.bash import BashOperator` | `from airflow.providers.standard.operators.bash import BashOperator` |
Match the docs to the version you run. The [official Airflow 101 tutorial](https://airflow.apache.org/docs/apache-airflow/stable/tutorial/fundamentals.html) is written for the current version.
XCom: what it is for
XComs are for small values: a count, a file name, a status. Do not pass a whole dataset through them. Write big data to a file or a table and pass the path.
Related tool pages
- Learn Apache Airflow Free: Hands-On Airflow Lab: Practise Apache Airflow hands-on, free: DAGs, dependencies, TaskFlow, schedules, retries, branching and mapping, with 11 lessons and the real Airflow UI on your laptop.
Also try
How to use the Airflow Lab: step by step
- Start the Airflow Lab
- Open Your first DAG and press Test DAG
- Change the echo text and run again
- Open TaskFlow and XCom and read the output
When to use the Airflow Lab
- Writing your first DAG
- Moving old Airflow 2 examples to Airflow 3
- Learning the TaskFlow API
- Showing a colleague how tasks pass data
Example
Input: a DAG that cleans data, runs a Spark job and lists the result
Output: Task states in the lab, the DAG in the Airflow UI, the job in the Spark UI
Frequently asked questions
How do I write my first Airflow DAG?
Create a Python file in the dags folder, import DAG from airflow.sdk, add tasks with operators or the @task decorator, and connect them with >>. Test it with a Test DAG run before you trigger it.
What is the difference between an operator and the TaskFlow API?
An operator such as BashOperator is a ready-made task class. The TaskFlow API turns your own Python functions into tasks with @task and passes return values between them as XComs. You can mix both in one DAG.
Why does my Airflow example fail with an import error?
It was probably written for Airflow 2. In Airflow 3 DAG, dag and task come from airflow.sdk, and operators such as BashOperator come from airflow.providers.standard.
Can I run a DAG without installing Airflow?
Yes. The Airflow Lab runs Airflow 3 in Docker on your computer, so the only thing to install is Docker Desktop.
Tip: Press Test DAG while you write. It runs the whole DAG once without the scheduler, so you see errors in seconds.