SUPPORT.TWILIO.COM END OF LIFE NOTICE: This site, support.twilio.com, is scheduled to go End of Life on February 27, 2024. All Twilio Support content has been migrated to help.twilio.com, where you can continue to find helpful Support articles, API docs, and Twilio blog content, and escalate your issues to our Support team. We encourage you to update your bookmarks and begin using the new site today for all your Twilio Support needs.

Reject Incoming Calls with a Phone Number Block List

The easiest way to reject incoming phone calls from specific phone numbers is to create a "virtual block list". The block list can then be used to filter incoming calls; automatically hanging up on any blocked numbers, and forward other callers as normal. This guide walks you through the process of setting up a virtual block list with sample code for Twilio Functions.

For alternative call blocking options, please see How Can I Stop Receiving or Block Incoming Phone Calls.

Notice: Twilio Functions is currently in beta, and has not yet been finalized. Support requests for beta and pre-release products are generally handled by our engineering team, and as such, response times are not guaranteed. For more information on Support limitations for beta and pre-release products, please see Twilio Beta Product Support.

Block list sample code

Here's our phone number block list sample code:

exports.handler = function(context, event, callback) {
  // List all blocked phone numbers in quotes and E.164 formatting, separated by a comma
  let numberBlock = event.block || [ "+12125551234", "+17025556789" ];  
  let twiml = new Twilio.twiml.VoiceResponse();
  let blocked = true;
  if (numberBlock.length > 0) {
if (numberBlock.indexOf(event.From) === -1) {
blocked = false;
}
} if (blocked) {
twiml.reject();
}
else { // if the caller's number is not blocked, redirect to your existing webhook twiml.redirect("https://demo.twilio.com/docs/voice.xml"); }
callback(null, twiml);
};

When your Twilio phone number responds to incoming calls with this Functions script, the code looks at the phone number detected in our incoming call request, and compares it to the list of blocked numbers in Line 3. If the incoming call is from a blocked phone number, the call is rejected. If the incoming call is not from a blocked phone number, the script redirects to another URL for TwiML.

Here's what you'll need to modify:

Line 3: Update [ "+12125551234", "+17025556789" ] with the phone numbers you want to block calls from (in E.164 formatting).

Line 16: Update ("https://demo.twilio.com/docs/voice.xml")with your phone number's current routing (webhook URL, TwiML bin URL, etc.) for handling valid calls.

Notice: The implementation described here demonstrates how to block inbound calls from specific individual numbers, but you may certainly modify the code to block calls based on phone number's prefix or country code if you wish.  An example of blocking by the caller's country can be found here.

Create a block list Function

  1. Access the Manage Functions page in Console.
  2. Click Create a Function, or the red plus + sign button.
  3. Select the Blank template, and then click Create
  4. Edit the Function's Properties fields: 
    • FUNCTION NAME: Enter the desired name of your Function - we suggest using "Block list" or something similar that gives an idea of what the code does.
    • PATH: Enter the desired Function path - we suggest using the FUNCTION NAME or something similar.
  5. Edit the Function's Configuration fields: 
    • ACCESS CONTROL: Click and check the box for Check for valid Twilio signature.
    • EVENT: Click the drop-down menu, and then select Incoming Voice Calls.
    • CODE: Copy and paste your modified code (see the Block list sample code section above).
  6. Once your Function's fields are correct, click Save

Set your phone number to use your block list function

  1. Access the Active Numbers page in Console.
  2. Click the desired phone number to modify.
  3. Update your phone number’s routing:
    • ACCEPT INCOMING: Voice Calls
    • CONFIGURE WITH: Webhooks, TwiML Bins, Functions, Studio, or Proxy
    • A CALL COMES IN: Function
    • DROP-DOWN MENU: Select your block list function.
  4. Click Save when finished.

For full details, see Configure a Twilio Phone Number to Receive and Respond to Voice Calls.

Have more questions? Submit a request
Powered by Zendesk