How to Integrate Slack with Twilio
In this article
You can integrate Slack with Twilio by creating a Twilio Function that receives an incoming webhook and posts a message to Slack via a Slack Incoming Webhook URL. This lets you send SMS messages, voice call alerts, or other Twilio events directly into a Slack channel. Before you start, you need a Twilio account with a phone number, a Slack workspace where you can add integrations, and basic familiarity with webhooks.
What You'll Need
- A Twilio account (free trial works) with an active phone number that can send SMS or receive calls.
- A Slack workspace where you have permission to add apps and integrations.
- A Slack Incoming Webhook URL (created in Slack).
- Access to the Twilio Console to create a Function and configure a phone number webhook.
- Optional: a Twilio phone number for two-way messaging.
Step-by-Step
- Create a Slack Incoming Webhook. In Slack, go to the channel where you want messages to appear. Click the channel name, then Integrations (or Apps & Integrations), and search for Incoming Webhooks. Add the app, choose the channel, and copy the generated Webhook URL. Save it securely; you'll need it in Twilio.
- Log in to the Twilio Console. Go to Develop > Functions > Services (or Functions > Classic if using the older interface). Create a new Service and give it a name like SlackNotifier.
- Add a Function. Inside the Service, click Add > Function. Name it sendToSlack. Set the visibility to Public so it can be triggered by a webhook.
- Write the Function code. Replace the default code with a handler that reads the incoming request (e.g.,
event.Bodyfor SMS) and sends an HTTP POST to your Slack Webhook URL. Use thegotoraxiosmodule available in Twilio Functions. Example (Node.js):
Store your Slack Webhook URL as an environment variable in the Service (e.g.,exports.handler = function(context, event, callback) { const axios = require('axios'); const slackUrl = context.SLACK_WEBHOOK_URL; const message = event.Body || 'New Twilio event'; axios.post(slackUrl, { text: message }) .then(() => callback(null, 'OK')) .catch(err => callback(err)); };SLACK_WEBHOOK_URL) rather than hardcoding it. - Deploy the Function. Click Deploy All. After deployment, copy the Function URL (it looks like
https://your-service.twil.io/sendToSlack). - Configure your Twilio phone number. Go to Phone Numbers > Manage > Active Numbers, select your number. Under Messaging, set A message comes in to Webhook and paste the Function URL. For voice alerts, set the A call comes in webhook similarly. Save.
- Test it. Send an SMS to your Twilio number from your phone. The message should appear in your Slack channel within seconds. If not, check the Twilio Debugger for errors.
Common Mistakes
- Using the wrong Slack Webhook URL (e.g., from a different channel or a revoked integration).
- Forgetting to deploy the Twilio Function after editing code.
- Hardcoding the Slack URL in the Function instead of using environment variables (security risk).
- Not setting the phone number's webhook to the correct Function URL.
- Ignoring Twilio Debugger alerts; they often show the exact error (e.g., 11200 HTTP retrieval failure).
Tips
- Use Slack message formatting (e.g.,
*bold*,_italic_) in the Function to make alerts readable. - Create separate Slack channels for different Twilio events (SMS vs. voice) by using multiple webhooks.
- Add a fallback in your Function to handle missing or empty message bodies.
- Keep your Slack Webhook URL secret; anyone with it can post to your channel.
- Test with Twilio's built-in request inspector to see the exact payload sent to your Function.
Troubleshooting
If messages don't appear in Slack, first check the Twilio Debugger for errors. Common issues include: the Function URL is incorrect (re-copy from the Twilio Console), the Slack Webhook URL is invalid (regenerate in Slack), or the Function code has a syntax error (check logs in Twilio Console > Functions > Logs). If the Function runs but Slack doesn't receive the message, verify the Slack Webhook URL is active and the channel still exists. For voice calls, ensure the webhook is set under the correct voice configuration. If you see a 403 error from Slack, the webhook may have been revoked; create a new one. Always redeploy after code changes.
Frequently Asked Questions
Can I send messages from Slack to Twilio?
Yes, but it requires a different setup: use Slack's outgoing webhooks or a Slack app with a slash command to trigger a Twilio Function that sends an SMS. This guide focuses on Twilio-to-Slack.
Do I need a paid Twilio account?
A free trial account works for testing, but trial accounts have limitations (e.g., verified numbers only). For production, you'll need a paid account.
Can I use this with Twilio Studio instead of Functions?
Yes, Twilio Studio has an HTTP Request widget that can post to Slack. However, Functions give more control over the payload.
What if my Slack workspace restricts adding integrations?
You'll need admin approval. Contact your Slack workspace owner to enable Incoming Webhooks or install the app.
Is there a limit to how many messages I can send?
Slack Incoming Webhooks have rate limits (approximately 1 message per second per webhook). Twilio Functions have usage limits based on your plan.
Can I include Twilio message details like sender number in the Slack message?
Yes, the Twilio request to your Function includes parameters like From, To, and Body. Use these in your Slack message text.