Reject Incoming Calls with a Phone Number Blacklist

The easiest way to reject incoming phone calls from specific phone numbers is to create a "virtual blacklist". The blacklist can then be used to filter incoming calls; automatically hang up on any blacklisted numbers, and forward other callers as normal. This guide is intended to help you setup a virtual blacklist with sample code for Twilio Functions (Beta).

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.

Blacklist Sample Code

Here's our phone number blacklisting script sample:

exports.handler = function(context, event, callback) {
  // List all blocked phone numbers in quotes and E.164 formatting, separated by a comma
  let blacklist = event.blacklist || [ "+12125551234", "+17025556789" ];  
  let twiml = new Twilio.twiml.VoiceResponse();
  let blocked = true;
  if (blacklist.length > 0) {
if (blacklist.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 blacklisted numbers in Line 3. If the incoming call is from a blacklisted phone number, the call is rejected. If the incoming call is not from a blacklisted 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, Twimlet, TwiML bin URL, etc.) for handling valid calls.

Create a Blacklist 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.
    Select the 'Blank' template, and then click 'Create'. 
  4. Edit the Function's Properties fields:
    Blacklist_02_400px.png 
    • FUNCTION NAME: Enter the desired name of your Function - we suggest using "Blacklist" 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:
    Blacklist_03_400px.png 
    • 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 Blacklist Sample Code section above).
  6. Once your Function's fields are correct, click Save.
    Blacklist_04_400px.png 

Set your Phone Number to use your Blacklist 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 Blacklist function.
  4. Click Save when finished.

For more details, please see Configuring Phone Numbers to Receive Voice Calls - Functions.

Have more questions? Submit a request
Powered by Zendesk