const express = require('express');const AWS = require('aws-sdk');const app = express();const sqs = new AWS.SQS();app.post('/webhooks/smartcar', async (req, res) => { try { // 1. Get the raw payload const payload = req.body; // 2. Queue for processing await sqs.sendMessage({ QueueUrl: process.env.WEBHOOK_QUEUE_URL, MessageBody: JSON.stringify(payload) }).promise(); // 3. Return immediately res.status(200).json({ status: 'received' }); } catch (error) { console.error('Failed to queue webhook:', error); res.status(500).json({ error: 'Internal error' }); }});// Separate worker processes the queueasync function processWebhook(payload) { const { eventType } = payload; if (eventType === 'VEHICLE_STATE') { await updateVehicleState(payload); } else if (eventType === 'VEHICLE_ERROR') { await handleVehicleError(payload); }}
Don’t do this: If you perform heavy processing before returning a response, your endpoint may timeout and Smartcar will retry, creating duplicate processing work.
Bad Example
@app.post("/webhooks/smartcar")def webhook_handler(): payload = request.get_json() # DON'T DO THIS: These operations might take too long update_database(payload) call_external_api(payload) send_notifications(payload) # Might timeout before reaching this line return {"status": "received"}, 200