Apache Airflow Client

3.2.0 · active · verified Mon Apr 13

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

Install

Imports

Quickstart

Initializes the Airflow API client with basic authentication using environment variables for host, username, and password. It then attempts to connect to the Airflow instance and list all available DAGs, demonstrating a fundamental API interaction.

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.")

view raw JSON →