-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy path07_setup_callbacks.py
More file actions
70 lines (53 loc) · 2.17 KB
/
07_setup_callbacks.py
File metadata and controls
70 lines (53 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
"""
Tutorial 07 — Setup Callbacks (Webhooks)
Registers a callback URL to receive real-time notifications when
payments or mutations happen on your account.
To test locally, use a tool like ngrok to expose a local endpoint:
ngrok http 8080
Then use the ngrok HTTPS URL as your callback target.
Endpoints used:
POST /v1/user/{userId}/notification-filter-url
GET /v1/user/{userId}/notification-filter-url
"""
import os
from dotenv import load_dotenv
from bunq_client import BunqClient
load_dotenv()
# Replace with your actual webhook URL (e.g. from ngrok)
CALLBACK_URL = os.getenv("BUNQ_CALLBACK_URL", "https://your-webhook-url.example.com/callback")
def main() -> None:
api_key = os.getenv("BUNQ_API_KEY", "").strip()
if not api_key:
print("No BUNQ_API_KEY found — creating a sandbox user...")
api_key = BunqClient.create_sandbox_user()
print(f" API key: {api_key}\n")
client = BunqClient(api_key=api_key, sandbox=True)
client.authenticate()
print(f"Authenticated — user {client.user_id}\n")
# ---- Register callback filters ----
print(f"Registering callback URL: {CALLBACK_URL}")
print(" Categories: PAYMENT, MUTATION\n")
client.post(f"user/{client.user_id}/notification-filter-url", {
"notification_filters": [
{"category": "PAYMENT", "notification_target": CALLBACK_URL},
{"category": "MUTATION", "notification_target": CALLBACK_URL},
],
})
print(" Callbacks registered!\n")
# ---- List active callback filters ----
print("Active notification filters:")
print("-" * 70)
filters = client.get(f"user/{client.user_id}/notification-filter-url")
for item in filters:
nf = item.get("NotificationFilterUrl", {})
for f in nf.get("notification_filters", []):
print(
f" category={f.get('category')} "
f"target={f.get('notification_target')}"
)
if not filters:
print(" (no filters found)")
print("\nWhen a payment or mutation occurs, bunq will POST a JSON")
print("notification to your callback URL. See doc.bunq.com for the payload format.")
if __name__ == "__main__":
main()