Skip to main content

Webhook Implementation

To receive event notifications from MOUNTAIN Events, you need to implement a Webhook receiver server. Below is an implementation example using @kyuzan/mountain-webhook-sdk:

// server.js
import express from 'express';
import { initializeSDK } from '@kyuzan/mountain-webhook-sdk';

const app = express();
app.use(express.json());

// In production, manage this securely, e.g., retrieve from environment variables
const WEBHOOK_SECRET = 'mtn_whsec_WFhvLG3FWsEtVLJsSyT+sXO5pTDEltL+iCtRuhDZamE=';

// Initialize the SDK
const sdk = initializeSDK();

app.post('/webhook', (req, res) => {
try {
// Validate and get the webhook event from the request
const result = sdk.getEventFromRequest(req, WEBHOOK_SECRET);

// If validation fails
if (!result.isValid) {
console.error(`Webhook validation error: ${result.error}`);
return res.status(400).json({ error: result.error });
}

// If validation succeeds, process the payload
const payload = result.payload;
console.log('Received verified event:', payload);

// Process based on event type
if (payload.event.name === 'Transfer') {
const { from, to, value } = payload.event.args;
console.log(`Transfer: ${from} -> ${to}, value: ${value}`);

// Implement your business logic here
// Examples: Save to database, send notifications, etc.
}

// Return success response
res.json({ received: true });
} catch (error) {
console.error('Webhook processing error:', error.message);
res.status(500).json({ error: 'Internal server error' });
}
});

const PORT = process.env.PORT || 3011;
app.listen(PORT, () => {
console.log(`Webhook server started: http://localhost:${PORT}`);
});

Next Steps​