---
title: "Send a Slack message from an Airflow DAG"
description: "How to use the SlackAPIPostOperator to send a templated message to a Slack channel"
author: "Bartosz Mikulski"
author_bio: "Principal AI Engineer & MLOps Architect. I bridge the gap between \"it works in a notebook\" and \"it works for 200 million users.\""
author_url: https://mikulskibartosz.name
author_linkedin: https://www.linkedin.com/in/mikulskibartosz/
author_github: https://github.com/mikulskibartosz
canonical_url: https://mikulskibartosz.name/send-slack-message-from-airflow
---

What to do when we want to send a Slack notification telling us how many values have we processed in our DAG? I suggest doing it like this:

* First, we have to store the value in the XCom. We can do it by returning the value from the PythonOperator. In this case, Airflow will automatically store it in XCom using the `return_value` as the key.

* In the following step, we can define a `SlackAPIPostOperator` that uses a template to retrieve the value from XCom and puts it in the message sent to a Slack channel:

```python
from airflow.operators.slack_operator import SlackAPIPostOperator

send_to_slack = SlackAPIPostOperator(
    task_id='notify_slack',
    channel='#airflow',
    username='Airflow',
    {% raw %} text="DAG run {{ execution_date }} processed {{ task_instance.xcom_pull('other_task', key='return_value') }} values." {% endraw %}
    slack_conn_id='slack_connection_id'
)

