// ----------------------------
// Description: Create web server and listen to CAD push events locally
// ----------------------------
// Use the JS HTTP library to create a new webserver
// Pass any traffic to the 'handler' function
let server = require('http').createServer(handler);
// Sonoran CAD community API key
// Used to authenticate traffic received
const API_KEY = "YOUR_API_KEY";
// Start the webserver on a specific port
// Ensure you have this port OPEN on your external IP
// https://portchecker.co/
// Webserver traffic handler
async function handler (req, res) {
let data = ''; // Parsed JSON data
let success = false; // Successful parsing/handling
let responseMessage = ""; // Response text back to server
// Only handle POST requests
// Only handle requests sent to /sonorancad/event (CAD push events)
if (req.method === "POST" && req.url === '/sonorancad/event') {
req.on('data', function(chunk) {
// Read and piece each part of data together
// Once we've fully read the request
req.on('end', async function() {
// Parse data string to a local JSON object
// Sonoran CAD sends your API KEY with the POST data
// Check this here to prevent someone random sending you requests
if (data.key === API_KEY) {
// Switch Case: handle each event type
// Format: https://info.sonorancad.com/sonoran-cad/api-integration/push-events/event-911
console.log(`New 911 from ${data.caller}`);
// Push event type isn't handled here
responseMessage = "Invalid Type";
// Server traffic didn't have the proper API key
responseMessage = "Authentication Key Failed!";
// Send response back to server
function sendResponse(success) {
// Return an HTTP 200 or 400 based on `success` boolean
const statusCode = success ? 200 : 400;
res.writeHead(statusCode, {'Content-Type': 'text/plain'});
res.end(responseMessage);