JavaScript / Node.js Integration
Send notifications using the Fetch API or any HTTP library.
Basic Usage
const WEBHOOK_URL = 'YOUR_WEBHOOK_URL';
async function sendNotification(title, body, priority = 'normal') {
const response = await fetch(WEBHOOK_URL, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ title, body, priority })
});
return response.json();
}
// Usage
await sendNotification('Build Complete', 'All tests passed');
await sendNotification('Error', 'Database connection failed', 'high');With Actions
await fetch(WEBHOOK_URL, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
title: 'PR Review Requested',
body: 'alice requested your review on #42',
actions: [
{ type: 'url', label: 'View PR', value: 'https://github.com/org/repo/pull/42' }
]
})
});With Error Handling
async function notify(title, body, options = {}) {
try {
const response = await fetch(process.env.PING_WEBHOOK_URL, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
title,
body,
priority: options.priority || 'normal',
actions: options.actions
})
});
if (!response.ok) {
const error = await response.json();
console.error('Failed to send notification:', error);
return null;
}
return response.json();
} catch (error) {
console.error('Network error:', error.message);
return null;
}
}TypeScript Types
interface NotificationPayload {
title: string;
body?: string;
priority?: 'low' | 'normal' | 'high';
callback_url?: string;
actions?: Action[];
}
interface Action {
type: 'url' | 'button' | 'select' | 'text' | 'number' | 'toggle';
label: string;
key?: string;
value?: string;
style?: 'primary' | 'secondary' | 'destructive';
options?: { label: string; value: string }[];
}
interface NotificationResponse {
ok: boolean;
id: string;
muted?: boolean;
}
async function sendNotification(payload: NotificationPayload): Promise<NotificationResponse> {
const response = await fetch(WEBHOOK_URL, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(payload)
});
return response.json();
}Express.js Middleware
const express = require('express');
const app = express();
// Notify on errors
app.use((err, req, res, next) => {
fetch(process.env.PING_WEBHOOK_URL, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
title: 'Server Error',
body: `${err.name}: ${err.message}\n${req.method} ${req.path}`,
priority: 'high'
})
}).catch(console.error);
next(err);
});