HumanLayer Flask Example
This example demonstrates how to use HumanLayer with Flask in two ways:
- Simple approvals with
app.py - Webhook-based approvals with
app-webhooks.py
Table of Contents
Setup
-
Create a virtual environment and activate it:
python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate -
Install dependencies:
pip install -r requirements.txt -
Copy
.env.exampleto.envand fill in your API keys:cp .env.example .env # Add your keys to .env: # HUMANLAYER_API_KEY=your_key_here
Simple Approvals
The app.py example shows basic HumanLayer integration where the API waits for approval before returning a response.
Running Simple
Start the server:
python app.py
Testing Simple
Test the agent with a very basic prompt:
curl "http://localhost:8000/run?prompt=yo"
{
"status": "success",
"message": "how can I help you?"
}
You can send a prompt that uses math to trigger the approval logic
# Test the agent with a prompt
curl "http://localhost:8000/run?prompt=what+is+2+plus+2"
Responses Simple
The endpoint will wait for approval and then return:
{
"status": "success",
"result": "2 plus 2 equals 4"
}
Webhook Approvals
The app-webhooks.py example demonstrates a more advanced pattern using webhooks for asynchronous approvals.
Setup Webhooks
-
Get an ngrok.com account and claim your free static subdomain
-
Set up response webhooks in the HumanLayer dashboard:
- Go to Settings > Webhooks
- Add webhook:
https://your-subdomain.ngrok-free.app/webhook/inbound - Save the webhook configuration
-
Start ngrok pointing to your Flask app:
ngrok http 8000 --domain=your-subdomain.ngrok-free.app
Running the app
Start the server in a separate terminal:
python app-webhooks.py
Testing Webhooks
-
Make a request:
curl "http://localhost:8000/run?prompt=calculate%202%20plus%202" -
Note the function call ID in the response:
{ "status": "pending", "call_id": "abc123" } -
Check the status using the call_id in the url path
curl "http://localhost:8000/run/abc123" -
Approve the action in HumanLayer (via Slack or dashboard)
-
The webhook will receive the approval and update the status
-
Check the status again to see the result:
curl "http://localhost:8000/run/abc123"
Responses Webhooks
Initial request:
{
"status": "pending",
"call_id": "abc123"
}
Checking status before approval:
{
"status": "pending",
"message": "Waiting for approval, refresh to check status"
}
After approval:
{
"status": "success",
"result": "2 plus 2 equals 4"
}
View all function calls:
curl "http://localhost:8000/function-calls"
Response:
{
"function_calls": [
{
"call_id": "abc123",
"status": "approved",
"result": "2 plus 2 equals 4"
}
]
}

