Apache Airflow Client
The `apache-airflow-client` library provides a Pythonic way to interact with the Apache Airflow REST API. It is an auto-generated client based on Airflow's OpenAPI specification, ensuring it remains up-to-date with the latest Airflow API versions. This library allows programmatic control and querying of Airflow DAGs, tasks, and other components. It primarily follows Airflow's release cadence, regenerating with each new Airflow release, currently at version 3.2.0.
Warnings
- breaking Major version updates (e.g., v1.x to v2.x, or v2.x to v3.x) of `apache-airflow-client` may introduce breaking changes due to client regeneration from updated OpenAPI specifications or internal changes in the client generator. This can alter module paths, class structures, or method signatures.
- gotcha The `apache-airflow-client` must be compatible with the Airflow API version it's connecting to. Using a client generated for an older Airflow API with a newer Airflow instance (or vice-versa) can lead to unexpected errors, missing endpoints, or incorrect data models.
- gotcha The client primarily supports Basic Authentication. If your Airflow instance is configured with a different authentication mechanism (e.g., Google SSO, OAuth, Keycloak), direct usage of the `Configuration(username=..., password=...)` may not work.
Install
-
pip install apache-airflow-client
Imports
- APIClient
from airflow_client.client.api_client import APIClient
- Configuration
from airflow_client.client.configuration import Configuration
- DagApi
from airflow_client.client.api.dag_api import DagApi
Quickstart
import os
from airflow_client.client.configuration import Configuration
from airflow_client.client.api_client import APIClient
from airflow_client.client.api.dag_api import DagApi
# Configure API client with Airflow host, username, and password
# Using environment variables for sensitive data is recommended.
configuration = Configuration(
host=os.environ.get("AIRFLOW_HOST", "http://localhost:8080/api/v1"),
username=os.environ.get("AIRFLOW_USERNAME", "admin"),
password=os.environ.get("AIRFLOW_PASSWORD", "admin")
)
# Create an instance of the API client
# The 'with' statement ensures proper connection handling
with APIClient(configuration) as api_client:
# Create an instance of a specific API endpoint, e.g., DagApi
api_instance = DagApi(api_client=api_client)
try:
# Make an API call, e.g., list DAGs
api_response = api_instance.get_dags()
print("Successfully connected to Airflow API and fetched DAGs:")
if api_response.dags:
for dag in api_response.dags:
print(f"- {dag.dag_id}")
else:
print("No DAGs found.")
except Exception as e:
print(f"An error occurred: {e}")
print("Ensure Airflow is running and accessible at the specified host, and credentials are correct.")