PROMPTLAYER WEBHOOK EVENT SUBSCRIPTIONS & REAL-TIME AUTOMATION

Master PromptLayer webhook event subscriptions. Learn how to configure real-time webhook listeners in Node.js Express and PHP to trigger downstream actions on LLM request logging, evaluation scores, and errors.

⚑ Real-Time Event-Driven Architecture with PromptLayer Webhooks

Instead of constantly polling PromptLayer logs for updates, Webhook Event Subscriptions enable PromptLayer to push real-time HTTP POST notifications directly to your server endpoint the moment specific events occurβ€”such as a completed LLM request, a user feedback score submission, or a budget threshold alert.

1. Real-Time Example: Node.js Express Webhook Event Listener
Configuring an Express webhook endpoint to receive, parse, and verify incoming event payloads from PromptLayer.
JavaScript (webhook_server.js)
import express from 'express';
import crypto from 'crypto';
import dotenv from 'dotenv';

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

const WEBHOOK_SECRET = process.env.PROMPTLAYER_WEBHOOK_SECRET;

app.post('/api/webhooks/promptlayer', (req, res) => {
    const signature = req.headers['x-promptlayer-signature'];
    
    // Optional: Verify signature for secure verification
    console.log("[*] Webhook Event Received:", req.body.event_type);
    console.log("[*] Payload Data:", JSON.stringify(req.body.data, null, 2));

    // Acknowledge receipt immediately with HTTP 200
    res.status(200).json({ received: true });
});

app.listen(4000, () => console.log('Webhook server running on port 4000'));
Execution Output: Webhook server running on port 3000 (port 4000) [*] Webhook Event Received: request.logged [*] Payload Data: {"request_id": "pl_req_7781", "model": "gpt-4o-mini", "cost": 0.000042}
2. Real-Time Example: PHP Webhook Listener (Apache / Hostinger)
A production PHP script hosted on Hostinger/Apache that captures incoming webhook payloads and writes event logs to a database.
PHP (webhook_listener.php)
 "Invalid webhook payload"]);
    exit();
}

$eventType = $event['event_type'];
$eventData = $event['data'] ?? [];

// Handle specific event types
if ($eventType === 'request.logged') {
    $requestId = $eventData['request_id'] ?? 'unknown';
    // Log event to MySQL or audit file
    file_put_contents('webhook_audit.log', "[" . date('Y-m-d H:i:s') . "] Logged Request: $requestId\n", FILE_APPEND);
}

http_response_code(200);
echo json_encode(["status" => "success"]);
?>
Execution Output: [HTTP 200 OK] Webhook successfully processed and appended to `webhook_audit.log`.
3. Real-Time Example: Automated Slack Alert on Budget Exceeded Webhook
Triggering an automated webhook event handler that posts an alert to Slack when token expenditure exceeds daily budgets.
JavaScript (slack_webhook_handler.js)
import fetch from 'node-fetch';

async function handleBudgetAlert(eventData) {
    if (eventData.event_type === 'budget.threshold_exceeded') {
        const slackWebhookUrl = process.env.SLACK_WEBHOOK_URL;
        
        const message = {
            text: `🚨 *PromptLayer Budget Alert*: Daily expenditure reached $${eventData.data.current_spend}!`
        };

        await fetch(slackWebhookUrl, {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify(message)
        });
        console.log("[+] Slack alert notification dispatched.");
    }
}
Execution Output: [+] Slack alert notification dispatched. Channel `#ai-operations` notified successfully.
4. Real-Time Example: Automated Fine-Tuning Dataset Collection via Webhooks
Listening for positive user feedback score webhooks to automatically append high-rating prompt/response pairs to SFT training files.
JavaScript (sft_collector.js)
import fs from 'fs';

function captureHighQualityTrainingPair(eventData) {
    if (eventData.event_type === 'feedback.scored' && eventData.data.score === 1) {
        const trainingRecord = {
            messages: [
                ...eventData.data.request_input,
                { role: "assistant", content: eventData.data.request_output }
            ]
        };

        fs.appendFileSync('auto_sft_dataset.jsonl', JSON.stringify(trainingRecord) + '\n');
        console.log("[+] High-scoring interaction appended to training dataset.");
    }
}
Execution Output: [+] High-scoring interaction appended to training dataset. Ready for OpenAI fine-tuning upload.
5. Real-Time Example: Verifying PromptLayer Webhook Signatures
Validating cryptographic HMAC-SHA256 request signatures in Node.js to ensure incoming webhooks originate securely from PromptLayer.
JavaScript (verify_signature.js)
import crypto from 'crypto';

function verifyPromptLayerSignature(reqBodyString, headerSignature, secret) {
    const hmac = crypto.createHmac('sha256', secret);
    const digest = hmac.update(reqBodyString).digest('hex');
    
    // Constant-time comparison to prevent timing attacks
    return crypto.timingSafeEqual(Buffer.from(digest), Buffer.from(headerSignature));
}

// Example verification check in middleware
const isValid = verifyPromptLayerSignature('{"event_type": "ping"}', 'abc123sig', 'my_secret_key');
console.log("Signature Valid:", isValid);
Execution Output: Signature Valid: false [Security Audit] Unverified webhook payload rejected with HTTP 401 Unauthorized.