Webhook Examples (Advanced)
Python (Flask)
from flask import Flask, request, jsonify
import requests
app = Flask(__name__)
@app.route('/webhook/user-activity', methods=['POST'])
def user_activity_webhook():
data = request.json
# Process the webhook data and decide if an email should be sent
if data['activity_type'] == 'workout_completed':
response = requests.post(
'https://app.xn--lemn-sqa.com/api/transactional/send',
headers={
'Content-Type': 'application/json',
'X-Auth-APIKey': 'your_lemon_api_key'
},
json={
"fromname": "FitnessSaaS",
"fromemail": "noreply@fitnesssaas.com",
"to": data['user_email'],
"subject": "Great job on your workout!",
"body": f"<html><body>You completed a {data['workout_type']} workout. Keep it up!</body></html>"
}
)
if response.status_code == 200:
return jsonify({"status": "success", "message": "Email sent"}), 200
else:
return jsonify({"status": "error", "message": "Failed to send email"}), 500
return jsonify({"status": "success", "message": "Webhook received"}), 200
if __name__ == '__main__':
app.run(port=5000)
Node.js (Express)
const express = require('express');
const axios = require('axios');
const app = express();
app.use(express.json());
app.post('/webhook/user-activity', async (req, res) => {
const data = req.body;
// Process the webhook data and decide if an email should be sent
if (data.activity_type === 'workout_completed') {
try {
const response = await axios.post('https://app.xn--lemn-sqa.com/api/transactional/send', {
fromname: "FitnessSaaS",
fromemail: "noreply@fitnesssaas.com",
to: data.user_email,
subject: "Great job on your workout!",
body: `<html><body>You completed a ${data.workout_type} workout. Keep it up!</body></html>`
}, {
headers: {
'Content-Type': 'application/json',
'X-Auth-APIKey': 'your_lemon_api_key'
}
});
if (response.status === 200) {
res.json({ status: "success", message: "Email sent" });
} else {
res.status(500).json({ status: "error", message: "Failed to send email" });
}
} catch (error) {
console.error('Error sending email:', error);
res.status(500).json({ status: "error", message: "Failed to send email" });
}
} else {
res.json({ status: "success", message: "Webhook received" });
}
});
app.listen(5000, () => console.log('Server running on port 5000'));