Webhooks provide real-time notifications about changes to your shipments and containers, letting you build responsive applications that update as soon as new information becomes available.
First, you need to create an endpoint on your server that can receive POST requests. Here’s an example using Node.js with Express:
Webhook Endpoint
Copy
Ask AI
const express = require('express');const app = express();app.use(express.json());app.post('/terminal49-webhook', (req, res) => { // Extract the event data const webhookData = req.body; // Process the webhook (recommended to do this asynchronously) processWebhook(webhookData); // Always return a 200 status code quickly to acknowledge receipt res.status(200).send('Webhook received');});function processWebhook(webhookData) { // Extract the event type const event = webhookData.data.attributes.event; // Extract the reference object (what the event is about) const referenceType = webhookData.data.relationships.reference_object.data.type; const referenceId = webhookData.data.relationships.reference_object.data.id; console.log(`Processing ${event} for ${referenceType} ${referenceId}`); // Handle different event types switch (event) { case 'container.transport.vessel_arrived': handleVesselArrival(webhookData); break; case 'container.available_for_pickup': handleContainerAvailable(webhookData); break; case 'container.pickup_lfd.changed': handleLastFreeDayChanged(webhookData); break; // Add cases for other events you're interested in default: console.log(`Unhandled event type: ${event}`); }}app.listen(3000, () => { console.log('Webhook server running on port 3000');});
Your webhook endpoint must be publicly accessible over the internet for Terminal49 to send notifications to it. During development, you can use tools like ngrok to create a temporary public URL.
Your webhook endpoint should acknowledge receipt of a webhook by returning a 2xx HTTP status code (200, 201, 202, or 204) as quickly as possible. Process the webhook data asynchronously after acknowledging receipt.
2. Implement Idempotency
Design your webhook handler to be idempotent, meaning it handles duplicate webhook deliveries gracefully. Terminal49 may retry webhook delivery if your server doesn’t respond with a success code.
3. Secure Your Endpoint
Use HTTPS for your webhook endpoint
Consider implementing webhook signature validation if available
Only accept webhooks from Terminal49’s IP ranges if possible
4. Implement Error Handling
Handle errors gracefully and set up monitoring for your webhook endpoint. If webhooks aren’t being received as expected, check your server logs and Terminal49 dashboard.
5. Add Fallback Mechanisms
Implement a polling fallback that periodically checks the API directly in case webhooks are missed or delayed. This ensures your data stays up-to-date even if webhook delivery fails.
Check if your webhook URL is accessible from the internet
Verify your server is returning 2xx status codes
Check if the webhook is registered correctly
Look for any firewalls or security measures blocking incoming requests
Duplicate Notifications
Terminal49 may retry webhook delivery if your server doesn’t respond or returns an error. Implement idempotency in your handler to prevent processing the same event multiple times.
Webhook Testing
Use Terminal49’s test tracking numbers to simulate events and test your webhook handling without waiting for real events to occur.