Receive inbound SMS and MMS messages via webhooks using the Telnyx Messaging API — with auto-reply, MMS media handling, and webhook signature verification.
Receive inbound SMS and MMS messages via webhooks. When someone texts your Telnyx number, Telnyx sends an HTTP POST request with the message details to your configured webhook URL.
Send a text message from your phone to your Telnyx number. You should see the message details logged in your server console.You can also test locally without a phone:
Your webhook must return a 2xx response within 2 seconds (API v2) or 5 seconds (API v1). If delivery fails, Telnyx retries up to 2 times per URL. With a failover URL configured, that’s up to 4 total attempts.
A common pattern is sending an automatic reply when a message arrives. Combine your webhook handler with the Send Message API:
Report incorrect code
Copy
Ask AI
import express from 'express';import Telnyx from 'telnyx';const app = express();app.use(express.json());const client = new Telnyx({ apiKey: process.env.TELNYX_API_KEY });app.post('/webhooks', async (req, res) => { const { data } = req.body; if (data.event_type === 'message.received') { const { payload } = data; const from = payload.from.phone_number; const to = payload.to[0].phone_number; // Send auto-reply await client.messages.send({ from: to, // Reply from the number that received the message to: from, // Reply to the sender text: `Thanks for your message! We received: "${payload.text}"` }); console.log(`Auto-reply sent to ${from}`); } res.sendStatus(200);});app.listen(5000);
Avoid reply loops. If both sides auto-reply, they’ll ping each other forever. Guard against this by checking the sender isn’t one of your own numbers, or by tracking recently replied conversations.
In production, verify webhook signatures to confirm requests are from Telnyx. All webhooks are signed using ED25519 public key cryptography.Each webhook includes two headers:
Duplicates happen when your server doesn’t respond with 2xx within the timeout (2 seconds for API v2). Telnyx retries up to 2 times.Fix: Return 200 immediately, then process the message asynchronously. Use the payload.id field to deduplicate.
Report incorrect code
Copy
Ask AI
// Deduplicate using a Set (use Redis in production)const processed = new Set();app.post('/webhooks', (req, res) => { res.sendStatus(200); // Respond immediately const messageId = req.body.data.payload?.id; if (messageId && processed.has(messageId)) return; processed.add(messageId); // Process message asynchronously handleMessage(req.body.data);});
Webhook timeout errors
Your server must respond within 2 seconds (API v2) or 5 seconds (API v1).Solutions:
Return 200 immediately, process in background
Use a message queue (Redis, SQS, RabbitMQ) for heavy processing