Apache Airflow SendGrid Provider
The `apache-airflow-providers-sendgrid` package integrates Apache Airflow with SendGrid, a popular email delivery service, allowing users to send emails programmatically from Airflow DAGs. It includes operators and hooks to interact with the SendGrid API. The provider is actively maintained, with version 4.2.2 being the latest, and releases frequently in alignment with Apache Airflow's provider release cycle.
Common errors
-
ModuleNotFoundError: No module named 'airflow.providers.sendgrid'
cause The 'apache-airflow-providers-sendgrid' package is not installed in the Airflow environment.fixInstall the package using pip: 'pip install apache-airflow-providers-sendgrid'. -
ImportError: cannot import name 'SendGridHook' from 'airflow.providers.sendgrid.hooks.sendgrid'
cause The 'SendGridHook' class has been moved or renamed in the 'apache-airflow-providers-sendgrid' package.fixUpdate the import statement to: 'from airflow.providers.sendgrid.hooks.sendgrid import SendGridHook'. -
AttributeError: module 'airflow.providers.sendgrid' has no attribute 'SendGridHook'
cause The 'SendGridHook' class is not directly accessible from the 'airflow.providers.sendgrid' module.fixImport 'SendGridHook' from its specific module: 'from airflow.providers.sendgrid.hooks.sendgrid import SendGridHook'. -
ImportError: cannot import name 'SendGridOperator' from 'airflow.providers.sendgrid.operators.sendgrid'
cause The 'SendGridOperator' class has been moved or renamed in the 'apache-airflow-providers-sendgrid' package.fixUpdate the import statement to: 'from airflow.providers.sendgrid.operators.sendgrid import SendGridOperator'. -
ModuleNotFoundError: No module named 'sendgrid'
cause The 'sendgrid' Python package, a dependency of 'apache-airflow-providers-sendgrid', is not installed.fixInstall the 'sendgrid' package using pip: 'pip install sendgrid'.
Warnings
- breaking Provider version compatibility with Apache Airflow: Different provider versions require specific minimum Airflow versions. Installing a provider version incompatible with your Airflow setup might lead to automatic Airflow upgrades or `ModuleNotFoundError` if dependencies are not met. For example, provider 4.2.2 requires Airflow >= 2.11.0, while 3.0.0 required Airflow >= 2.2.0.
- deprecated Fetching SendGrid credentials from environment variables (e.g., `SENDGRID_API_KEY`) directly is deprecated. It's recommended to configure SendGrid connections via Airflow's connection mechanism for better security and management.
- gotcha SendGrid connection type might not appear in the Airflow UI dropdown for older Airflow versions or if the provider is not correctly registered. This can lead to confusion when setting up connections.
- gotcha Incorrect `ModuleNotFoundError` when DAGs try to import `airflow.providers.sendgrid.operators` or `hooks`. This usually indicates that the provider package was not installed correctly or there are Python environment issues in the Airflow worker context.
Install
-
pip install apache-airflow-providers-sendgrid
Imports
- SendGridOperator
from airflow.providers.sendgrid.operators.sendgrid import SendGridOperator
- SendGridHook
from airflow.providers.sendgrid.hooks.sendgrid import SendGridHook
Quickstart
import os
from datetime import datetime
from airflow import DAG
from airflow.providers.sendgrid.operators.sendgrid import SendGridOperator
# Ensure you have a SendGrid API Key configured in Airflow Connections
# named 'sendgrid_default'. For a quick start, you can set an environment variable:
# export AIRFLOW_CONN_SENDGRID_DEFAULT='sendgrid://:YOUR_SENDGRID_API_KEY@'
# Replace 'YOUR_SENDGRID_API_KEY' with your actual key (starting with SG.)
with DAG(
dag_id='sendgrid_email_example',
start_date=datetime(2023, 1, 1),
schedule_interval=None,
catchup=False,
tags=['sendgrid', 'email'],
) as dag:
send_email_task = SendGridOperator(
task_id='send_test_email',
sendgrid_conn_id='sendgrid_default',
to='recipient@example.com', # Replace with actual recipient
subject='Airflow SendGrid Test Email',
html_content='<strong>This is a test email from Airflow via SendGrid!</strong>',
from_email='sender@example.com', # Replace with actual sender (verified in SendGrid)
)