Implementation:Apache Airflow BaseHook Interface
Appearance
| Knowledge Sources | |
|---|---|
| Domains | Integration, Python_API |
| Last Updated | 2026-02-08 00:00 GMT |
Overview
Concrete tool for managing external service connections provided by the BaseHook class and provider discovery system.
Description
The BaseHook class is the abstract base for all hooks. It provides class methods for connection retrieval (get_connection), hook resolution (get_hook), and UI customization (get_connection_form_widgets, get_ui_field_behaviour). The discover_all_providers_from_packages function scans installed packages for provider metadata.
Usage
Subclass BaseHook to create hooks for new external services. Implement get_conn() to return the actual connection client.
Code Reference
Source Location
- Repository: Apache Airflow
- File: task-sdk/src/airflow/sdk/bases/hook.py
- Lines: L30-101
Signature
class BaseHook(LoggingMixin):
def __init__(self, logger_name: str | None = None):
super().__init__()
self._log_config_logger_name = "airflow.task.hooks"
self._logger_name = logger_name
@classmethod
def get_connection(cls, conn_id: str) -> Connection: ...
@classmethod
async def aget_connection(cls, conn_id: str) -> Connection: ...
@classmethod
def get_hook(cls, conn_id: str, hook_params: dict | None = None): ...
def get_conn(self) -> Any: ...
@classmethod
def get_connection_form_widgets(cls) -> dict[str, Any]: ...
@classmethod
def get_ui_field_behaviour(cls) -> dict[str, Any]: ...
Import
from airflow.sdk.bases.hook import BaseHook
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| conn_id | str | Yes | Airflow connection identifier |
| logger_name | str or None | No | Custom logger name |
| hook_params | dict or None | No | Additional hook init parameters |
Outputs
| Name | Type | Description |
|---|---|---|
| Connection | Connection | Connection object with credentials |
| Client | Any | External service client from get_conn() |
| Form widgets | dict | Custom UI form fields |
Usage Examples
Custom Hook Implementation
from airflow.sdk.bases.hook import BaseHook
import my_service_sdk
class MyServiceHook(BaseHook):
conn_name_attr = "my_service_conn_id"
default_conn_name = "my_service_default"
conn_type = "my_service"
hook_name = "My Service"
def __init__(self, my_service_conn_id: str = default_conn_name):
super().__init__()
self.conn_id = my_service_conn_id
def get_conn(self):
conn = self.get_connection(self.conn_id)
return my_service_sdk.Client(
host=conn.host,
token=conn.password,
)
Related Pages
Implements Principle
Requires Environment
Page Connections
Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment