> ## Documentation Index
> Fetch the complete documentation index at: https://astronomer.io/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Create and use params in Airflow

Params are arguments which you can pass to an Airflow DAG or task at runtime and are stored in the [Airflow context dictionary](/docs/learn/airflow-context) for each DAG run. You can pass DAG and task-level params by using the `params` parameter.

Params are ideal to store information that is specific to individual DAG runs like changing dates, file paths or ML model configurations. Params aren't encrypted and therefore not suitable to pass secrets. See also [Best practices for storing information in Airflow](/docs/learn/airflow-variables#best-practices-for-storing-information-in-airflow).

This guide covers:

* How to pass params to a DAG at runtime.
* How to define DAG-level param defaults which are rendered in the **Trigger DAG** UI.
* How to access params in an Airflow task.
* The hierarchy of params in Airflow.

## Assumed knowledge

To get the most out of this guide, you should have an understanding of:

* Airflow DAGs. See [Introduction to Airflow DAGs](/docs/learn/dags).
* Airflow operators. See [Operators 101](/docs/learn/what-is-an-operator).
* Airflow context. See [Access the Apache Airflow context](/docs/learn/airflow-context).

## Pass params to a DAG run at runtime

Params can be passed to a DAG at runtime in four different ways:

* In the Airflow UI by using the **Trigger DAG** form. This form appears when you click the **Trigger DAG** (**Play**) button in the Airflow UI.
* Running a DAG with the `--conf` flag using the Airflow CLI ([`airflow dags trigger`](https://airflow.apache.org/docs/apache-airflow/stable/cli-and-env-variables-ref.html#trigger)).
* Using the `TriggerDagRunOperator` with the `conf` parameter.
* Making a `POST` request to the Airflow REST APIs [Trigger Dag Run](https://airflow.apache.org/docs/apache-airflow/stable/stable-rest-api-ref.html#operation/trigger_dag_run) endpoint and using the `conf` parameter.

Param values passed to a DAG by any of these methods will override existing default values for the same key as long as the [Airflow core config `dag_run_conf_overrides_params`](https://airflow.apache.org/docs/apache-airflow/stable/configurations-ref.html#dag-run-conf-overrides-params) is set to `True` (which is the default setting).

<Info>
  You can only pass params to a DAG that are JSON-serializable. Any params that aren't JSON-serializable will cause a DAG Import Error (`ParamValidationError`) upon DAG parsing.
</Info>

### Trigger DAG form

You can pass params to DAGs from the Airflow UI by clicking the **Play** button in the DAGs overview or the blue **Trigger** button on an individual DAG page.

<Frame>
  <img src="https://mintcdn.com/astronomer/1osHxgou1ANrjAnz/images/img/guides/3_0_airflow-params_play_button.png?fit=max&auto=format&n=1osHxgou1ANrjAnz&q=85&s=d7460f17baa8f10186fe2cae4c087c3c" alt="Play button" width="1903" height="964" data-path="images/img/guides/3_0_airflow-params_play_button.png" />
</Frame>

This button opens a form in which you can specify details for the DAG run. If the DAG has any params with defaults values defined, the form will render a field for each those params under **Run Parameters**. Additional params can be added under **Advanced Options** -> **Configuration JSON**.

<Frame>
  <img src="https://mintcdn.com/astronomer/1osHxgou1ANrjAnz/images/img/guides/3_0_airflow-params_trigger_dag_ui.png?fit=max&auto=format&n=1osHxgou1ANrjAnz&q=85&s=3eaaa5e7d17272d90d93165ca48bbeec" alt="Trigger DAG form" width="1111" height="497" data-path="images/img/guides/3_0_airflow-params_trigger_dag_ui.png" />
</Frame>

Under **Advanced Options**, you can also set the **Logical Date**, define a **Run ID**, and add a **Dag Run Note** for the manual DAG run. Note that any params already defined under **Run Parameters** will be included in the **Configuration JSON** by default.

<Frame>
  <img src="https://mintcdn.com/astronomer/1osHxgou1ANrjAnz/images/img/guides/3_0_airflow-params_advanced_options.png?fit=max&auto=format&n=1osHxgou1ANrjAnz&q=85&s=dbc0cadebdcad666ad12e20e81f537b6" alt="Advanced options trigger form" width="1100" height="655" data-path="images/img/guides/3_0_airflow-params_advanced_options.png" />
</Frame>

In the **Trigger DAG** form:

* You can change any default values for params that have been defined in the DAG file under **Run Parameters** or in the **Configuration JSON**.
* You can add new params under **Advanced Options** -> **Configuration JSON**.
* You can set the **Logical Date** of the DAG run to any date. Note that you can also provide no logical date, by clearing it in the calendar picker.
* You can set the **Run ID** to any string. If no Run ID is specified, Airflow generates one based on the run after date.
* You can add a **Dag Run Note** to the DAG run.

After setting the configuration, you can start the DAG run with the **Trigger** button.

### CLI

When you run an [Airflow DAG from the CLI](https://airflow.apache.org/docs/apache-airflow/stable/cli-and-env-variables-ref.html#dags), you can pass params to the DAG run by providing a JSON string to the `--conf` flag. For example, to trigger the `params_default_example` DAG with the value of `Hello from the CLI` for `param1`, run:

<details open>
  <summary>Astro</summary>

  Run Airflow commands from the Astro CLI using `astro dev run`:

  ```sh wrap theme={null}
  astro dev run dags trigger params_defaults_example --conf '{"param1" : "Hello from the CLI"}'
  ```
</details>

<details>
  <summary>Airflow</summary>

  ```sh wrap theme={null}
  airflow dags trigger params_defaults_example --conf '{"param1" : "Hello from the CLI"}'
  ```
</details>

The CLI prints the configuration for the triggered run to the command line:

<Frame>
  <img src="https://mintcdn.com/astronomer/fQ8p8i5zkzM6GzR0/images/img/guides/airflow-params_cli_param_output.png?fit=max&auto=format&n=fQ8p8i5zkzM6GzR0&q=85&s=32c751686ac9ca65a95c6657f6d008cf" alt="CLI output" width="1418" height="98" data-path="images/img/guides/airflow-params_cli_param_output.png" />
</Frame>

You can use a `--conf` flag with the following Airflow CLI sub-commands:

* `airflow dags backfill`
* `airflow dags test`
* `airflow dags trigger`

### `TriggerDagRunOperator`

The [`TriggerDagRunOperator`](/docs/learn/cross-dag-dependencies#triggerdagrunoperator) is a core Airflow operator that allows you to start a DAG run from within another DAG. You can use the `TriggerDAGRunOperator` `conf` param to trigger the dependent DAG with a specific configuration.

The DAG below uses the `TriggerDagRunOperator` to trigger the `tdro_example_downstream` DAG while passing a dynamic value for the `upstream_color` param using the `conf` parameter. The value for `upstream_color` is passed using a [Jinja template](/docs/learn/templating) pulling the return value of an upstream task using [XCom](/docs/learn/airflow-passing-data-between-tasks#xcom).

<details open>
  <summary>TaskFlow</summary>

  ```python wrap theme={null}
  from pendulum import datetime
  from airflow.decorators import dag, task
  from airflow.operators.trigger_dagrun import TriggerDagRunOperator
  import random


  @dag(
      start_date=datetime(2023, 6, 1),
      schedule="@daily",
      catchup=False,
  )
  def tdro_example_upstream():
      @task
      def choose_color():
          color = random.choice(["blue", "red", "green", "yellow"])
          return color

      tdro = TriggerDagRunOperator(
          task_id="tdro",
          trigger_dag_id="tdro_example_downstream",
          conf={"upstream_color": "{{ ti.xcom_pull(task_ids='choose_color')}}"},
      )

      choose_color() >> tdro


  tdro_example_upstream()
  ```
</details>

<details>
  <summary>Traditional</summary>

  ```python expandable wrap theme={null}
  from pendulum import datetime
  from airflow.decorators import dag, task
  from airflow.operators.trigger_dagrun import TriggerDagRunOperator
  from airflow.operators.python import PythonOperator
  import random


  def choose_color_func():
      color = random.choice(["blue", "red", "green", "yellow"])
      return color


  @dag(
      start_date=datetime(2023, 6, 1),
      schedule="@daily",
      catchup=False,
  )
  def tdro_example_upstream_traditional():
      choose_color = PythonOperator(
          task_id="choose_color",
          python_callable=choose_color_func,
      )

      tdro = TriggerDagRunOperator(
          task_id="tdro",
          trigger_dag_id="tdro_example_downstream",
          conf={"upstream_color": "{{ ti.xcom_pull(task_ids='choose_color')}}"},
      )

      choose_color >> tdro


  tdro_example_upstream_traditional()
  ```
</details>

Runs of the `tdro_example_downstream` DAG that are triggered by this upstream DAG will override the default value of the `upstream_color` param with the value passed using the `conf` parameter, which leads to the `print_color` task to print either `red`, `green`, `blue` or `yellow`.

<details open>
  <summary>TaskFlow</summary>

  ```python wrap theme={null}
  from pendulum import datetime
  from airflow.decorators import dag, task


  @dag(
      start_date=datetime(2023, 6, 1),
      schedule=None,
      catchup=False,
      params={"upstream_color": "Manual run, no upstream color available."},
  )
  def tdro_example_downstream():
      @task
      def print_color(**context):
          print(context["params"]["upstream_color"])

      print_color()


  tdro_example_downstream()
  ```
</details>

<details>
  <summary>Traditional</summary>

  ```python wrap theme={null}
  from pendulum import datetime
  from airflow.decorators import dag
  from airflow.operators.python import PythonOperator


  def print_color_func(**context):
      print(context["params"]["upstream_color"])


  @dag(
      start_date=datetime(2023, 6, 1),
      schedule=None,
      catchup=False,
      params={"upstream_color": "Manual run, no upstream color available."},
  )
  def tdro_example_downstream_traditional():
      PythonOperator(
          task_id="print_color",
          python_callable=print_color_func,
      )


  tdro_example_downstream_traditional()
  ```
</details>

## Define DAG-level param defaults

To specify params for all runs of a given DAG, pass default values to the `param` parameter of the `@dag` decorator or the `DAG` class in your DAG file. You can directly specify a default value or use the `Param` class to define a default value with additional attributes.

The DAG below has two DAG-level params with defaults: `param1` and `param2`, the latter only accepting integers.

<details open>
  <summary>TaskFlow</summary>

  ```python wrap theme={null}
  from pendulum import datetime
  from airflow.decorators import dag, task
  from airflow.models.param import Param


  @dag(
      start_date=datetime(2023, 6, 1),
      schedule=None,
      catchup=False,
      params={
          "param1": "Hello!",
          "param2": Param(
              23,
              type="integer",
          ),
      },
  )
  def simple_param_dag():
      @task
      def print_all_params(**context):
          print(context["params"]["param1"] * 3)
          print(context["params"]["param2"])

      print_all_params()


  simple_param_dag()
  ```
</details>

<details>
  <summary>Traditional</summary>

  ```python wrap theme={null}
  from pendulum import datetime
  from airflow import DAG
  from airflow.operators.python import PythonOperator
  from airflow.models.param import Param


  def print_all_params_func(**context):
      print(context["params"]["param1"] * 3)
      print(context["params"]["param2"])


  with DAG(
      dag_id="simple_param_dag",
      start_date=datetime(2023, 6, 1),
      schedule=None,
      catchup=False,
      params={
          "param1": "Hello!",
          "param2": Param(
              23,
              type="integer",
          ),
      },
  ):
      PythonOperator(
          task_id="print_all_params",
          python_callable=print_all_params_func,
      )
  ```
</details>

If you define DAG-level param defaults, the **Trigger DAG** form renders a field for each param. From this UI, you can then override your defaults for individual DAG runs. A param with a red asterisk is a required param.

<Frame>
  <img src="https://mintcdn.com/astronomer/1osHxgou1ANrjAnz/images/img/guides/3_0_airflow-params_simple_param_dag_trigger_ui.png?fit=max&auto=format&n=1osHxgou1ANrjAnz&q=85&s=ec1dbd12ad35bb1a780667d6d99bdf41" alt="Trigger DAG with simple defaults" width="1105" height="514" data-path="images/img/guides/3_0_airflow-params_simple_param_dag_trigger_ui.png" />
</Frame>

<Info>
  When you specify a required `type` for a param, the field will be a required input by default because of [JSON validation](https://json-schema.org/draft/2020-12/json-schema-validation.html#name-dates-times-and-duration). To make a field optional but still require a specific input type, allow NULL values by setting the type to `["null", "<my_type>"]`.
</Info>

<Info>
  If you don't specify a type for your param, Airflow will infer it based on the default value you provide.
</Info>

### Param types

The following param types are supported:

* `string`: A string. This is the default type.
* `null`: Allows the param to be None by being left empty.
* `integer`: An integer.
* `number`: A float (or integer).
* `boolean`: `True` or `False`.
* `array`: An HTML multi line text field, every line edited will be made into a string array as the value.
* `object`: A JSON entry field.

### Param attributes

Aside from the `type` attribute, the `Param` class has several other attributes that you can use to define how users interact with the param:

* `title`: The title of the param that appears in the **Trigger DAG** UI.
* `description`: A description of the param.
* `section`: Creates a section under which the param will appear in the **Trigger DAG** UI. All params with no specified section will appear under the default section **DAG conf Parameters**.
* `format`: A [JSON format](https://json-schema.org/draft/2020-12/json-schema-validation.html#name-dates-times-and-duration) that Airflow will validate a user's input against. Airflow 3.3+ allows you to specify `format="duration"` to pass an ISO 8601 duration.
* `enum`: A list of valid values for a param. Setting this attribute creates a dropdown menu in the UI.
* `const`: Defines a permanent default value and hides the param from the **Trigger DAG** UI. Note that you still need to provide a `default` value for the param.

All `Param` attributes are optional to set. For string type params, you can additionally set `minLength` and `maxLength` to define the minimum and maximum length of the input. Similarly, integer and number type params can have a `minimum` and `maximum` value.

### Param examples in the Airflow UI

This section presents a few examples of params and how they are rendered in the **Trigger DAG** UI.

The code snippet below defines a mandatory string param with a few UI elements to help users input a value.

```python wrap theme={null}
from airflow.sdk import Param

"my_string_param": Param(
    "Airflow is awesome!",
    type="string",
    title="Favorite orchestrator:",
    description="Enter your favorite data orchestration tool.",
    section="Important params",
    minLength=1,
    maxLength=200,
)
```

<Frame>
  <img src="https://mintcdn.com/astronomer/1osHxgou1ANrjAnz/images/img/guides/3_0_airflow-params_string_param_example.png?fit=max&auto=format&n=1osHxgou1ANrjAnz&q=85&s=4fed1505c1d84133f4c38b684de71afe" alt="String param example" width="1109" height="522" data-path="images/img/guides/3_0_airflow-params_string_param_example.png" />
</Frame>

When you define [date, datetime, or time param](https://datatracker.ietf.org/doc/html/rfc3339#section-5.6), a calendar picker appears in the **Trigger DAG** UI.

```python wrap theme={null}
from airflow.sdk import Param

"my_datetime_param": Param(
    "2016-10-18T14:00:00+00:00",
    type="string",
    format="date-time",
),
```

<Frame>
  <img src="https://mintcdn.com/astronomer/1osHxgou1ANrjAnz/images/img/guides/3_0_airflow-params_datetime_picker.png?fit=max&auto=format&n=1osHxgou1ANrjAnz&q=85&s=01c32c5ad7371d6c24aab2cf79316b67" alt="Datetime param example" width="1034" height="436" data-path="images/img/guides/3_0_airflow-params_datetime_picker.png" />
</Frame>

Providing a list of values to the `enum` attribute will create a dropdown menu in the **Trigger DAG** UI. Note that the default value must also be in the list of valid values provided to `enum`. Due to JSON validation rules, a value has to be selected.

```python wrap theme={null}
from airflow.sdk import Param

"my_enum_param": Param(
    "Hi :)", type="string", enum=["Hola :)", "Hei :)", "Bonjour :)", "Hi :)"]
),
```

<Frame>
  <img src="https://mintcdn.com/astronomer/1osHxgou1ANrjAnz/images/img/guides/3_0_airflow-params_enum_example.png?fit=max&auto=format&n=1osHxgou1ANrjAnz&q=85&s=5bb72dd626ddbcd5b8b888d16469a890" alt="Enum param example" width="1039" height="232" data-path="images/img/guides/3_0_airflow-params_enum_example.png" />
</Frame>

A boolean type param will create a toggle in the **Trigger DAG** UI.

```python wrap theme={null}
from airflow.sdk import Param

"my_bool_param": Param(True, type="boolean"),
```

<Frame>
  <img src="https://mintcdn.com/astronomer/1osHxgou1ANrjAnz/images/img/guides/3_0_airflow-params_bool.png?fit=max&auto=format&n=1osHxgou1ANrjAnz&q=85&s=f5768e9a9f84ceae0163f0cca2c501bf" alt="Bool param example" width="1022" height="178" data-path="images/img/guides/3_0_airflow-params_bool.png" />
</Frame>

An array type param will create a multi-line text field in the **Trigger DAG** UI. Each line will be converted to an item in the array.

```python wrap theme={null}
from airflow.sdk import Param

"my_array_param": Param(["Hello Airflow", ":)"], type="array"),
```

<Frame>
  <img src="https://mintcdn.com/astronomer/1osHxgou1ANrjAnz/images/img/guides/3_0_airflow-params_array_example.png?fit=max&auto=format&n=1osHxgou1ANrjAnz&q=85&s=1644e369b16e67797bfecf0f6f9cf862" alt="Array param example" width="1034" height="209" data-path="images/img/guides/3_0_airflow-params_array_example.png" />
</Frame>

An object type param will create a JSON entry field in the **Trigger DAG** UI. The value of the param must be a valid JSON object.

```python wrap theme={null}
from airflow.sdk import Param

"my_object_param": Param({"a": 1, "b": 2}, type="object"),
```

<Frame>
  <img src="https://mintcdn.com/astronomer/1osHxgou1ANrjAnz/images/img/guides/3_0_airflow-params_object_example.png?fit=max&auto=format&n=1osHxgou1ANrjAnz&q=85&s=2e5993c212e03a321b27900612e5dd4b" alt="Object param example" width="1013" height="272" data-path="images/img/guides/3_0_airflow-params_object_example.png" />
</Frame>

## Define task-level param defaults

You can set task-level param defaults in the same way as for DAG-level params. If a param of the same key is specified at both the DAG and task level, the DAG-level param will take precedence.

<details open>
  <summary>TaskFlow</summary>

  ```python wrap theme={null}
  @task(params={"param1": "Hello World!"})
  def t1(**context):
      print(context["params"]["param1"])
  ```
</details>

<details>
  <summary>Traditional</summary>

  ```python wrap theme={null}
  t1 = BashOperator(
      task_id="t1",
      bash_command="echo {{ params.param1 }}",
      params={"param1": "Hello World!"},
  )
  ```
</details>

## Access params in a task

You can access params in an Airflow task like you can with other elements in the [Airflow context](/docs/learn/airflow-context).

<details open>
  <summary>TaskFlow</summary>

  ```python wrap theme={null}
  from airflow.sdk import task

  @task
  def t1(**context):
      print(context["params"]["my_param"])
  ```
</details>

<details>
  <summary>Traditional</summary>

  ```python wrap theme={null}
  from airflow.providers.standard.operators.python import PythonOperator

  def t1_func(**context):
      print(context["params"]["my_param"])

  t1 = PythonOperator(
      task_id="t1",
      python_callable=t1_func,
  )
  ```
</details>

Params are also accessible as a [Jinja template](/docs/learn/templating) using the `{{ params.my_param }}` syntax.

If you try to access a param that hasn't been specified for a specific DAG run, the task will fail with an exception.

## Param precedence

The order of precedence for params, with the first item taking most precedence, is as follows:

* Params that have been provided for a specific DAG run by a method detailed in [pass params to a DAG run at runtime](#pass-params-to-a-dag-run-at-runtime) as long as the [Airflow config core.`dag_run_conf_overrides_params`](https://airflow.apache.org/docs/apache-airflow/stable/configurations-ref.html#dag-run-conf-overrides-params) is set to `True`.
* Param defaults that have been defined at the DAG level.
* Param defaults that have been defined at the task level.
