Example of a FastAPI application that exposes a simple API endpoint and a corresponding Airflow DAG that uses the SimpleHttpOperator to call the API endpoint.
Step 1: FastAPI Application
First, create a FastAPI application with a simple endpoint. Save this code in a file named main.py.
To run the FastAPI app, save this file and run:
This starts the FastAPI server on http://localhost:8000. The endpoint /items/ accepts POST requests with JSON data containing name, description, price, and tax.
Step 2: Airflow DAG with SimpleHttpOperator
In your Airflow DAG, use SimpleHttpOperator to make a POST request to the FastAPI endpoint. Below is a sample DAG that calls the FastAPI endpoint and logs the response. Save this as call_fastapi_dag.py.
Step 3: Configure the Airflow Connection
- Go to the Airflow UI, then to Admin > Connections.
- Add a new connection with the following details:
- Connection Id:
fastapi_connection - Connection Type:
HTTP - Host:
http://localhost:8000
- Connection Id:
Explanation
- FastAPI app: The
/items/endpoint calculates the total price based onpriceandtaxin the request. - Airflow DAG: The
SimpleHttpOperatorsends a POST request to the FastAPI app, with the response being logged in the Airflow task logs.

No comments:
Post a Comment