Skip to content

Webhook Alerts

Create Alert

alert = client.post("https://api.foursec.xyz/price-alert", json={
    "symbol": "ETH",
    "condition": "below",
    "target_price": 2400,
    "webhook_url": "https://your-server.com/webhook/price-alert",
    "duration_hours": 24
}).json()

print(f"Alert created: {alert['alert_id']}")

Webhook Handler Examples

Python (Flask)

from flask import Flask, request, jsonify
import hmac, hashlib

app = Flask(__name__)
WEBHOOK_SECRET = "your_secret_key"

@app.route('/webhook/price-alert', methods=['POST'])
def handle_alert():
    # Verify signature
    signature = request.headers.get('X-4SEC-Signature', '')
    payload = request.get_data()
    if not verify_signature(payload, signature, WEBHOOK_SECRET):
        return jsonify({"error": "invalid signature"}), 403

    data = request.json
    print(f"ALERT: {data['symbol']} hit ${data['current_price']}")
    print(f"Condition: {data['condition']} {data['target_price']}")

    # Execute trading logic
    if data['condition_met']:
        execute_trade(data['symbol'], data['current_price'])

    return 'OK', 200

def verify_signature(payload, signature, secret):
    expected = hmac.new(
        secret.encode(), payload, hashlib.sha256
    ).hexdigest()
    return hmac.compare_digest(expected, signature)

Node.js (Express)

const express = require('express');
const crypto = require('crypto');
const app = express();
app.use(express.json());

const SECRET = 'your_secret_key';

app.post('/webhook/price-alert', (req, res) => {
    const signature = req.headers['x-4sec-signature'] || '';
    const payload = JSON.stringify(req.body);

    const expected = crypto
        .createHmac('sha256', SECRET)
        .update(payload)
        .digest('hex');

    if (signature !== expected) {
        return res.status(403).json({ error: 'invalid signature' });
    }

    const { symbol, current_price, condition } = req.body;
    console.log('ALERT: ' + symbol + ' hit $' + current_price);

    // Execute trading logic here
    res.status(200).send('OK');
});

app.listen(3000);

Webhook Payload

{
  "alert_id": "alt_abc123",
  "event": "price_alert_triggered",
  "symbol": "ETH",
  "condition": "below",
  "target_price": 2400,
  "current_price": 2399.50,
  "condition_met": true,
  "timestamp": "2026-07-05T03:15:00Z",
  "sources": [
    {"dex": "Uniswap V3", "price": 2399.50},
    {"dex": "BaseSwap", "price": 2399.55},
    {"dex": "SushiSwap", "price": 2399.45}
  ]
}

Alert Lifecycle

StatusDescription
createdAlert registered and monitoring
activePrice being checked every 30s
triggeredCondition met, webhook called
expiredDuration elapsed without trigger

TIP

Alerts are one-time use. Once triggered, create a new alert for continued monitoring.

Testing Webhooks Locally

Use ngrok to expose local server:

# Terminal 1: Run your webhook handler
python app.py  # listening on port 3000

# Terminal 2: Expose with ngrok
ngrok http 3000
# Gives you: https://abc123.n.io

# Create alert with ngrok URL
alert = client.post("https://api.foursec.xyz/price-alert", json={
    "symbol": "ETH",
    "condition": "below",
    "target_price": 2400,
    "webhook_url": "https://abc123.ngrok.io/webhook/price-alert"
})

Released under the MIT License.