Potential Support Response Time Delays Due to Surge in Toll-Free Queries: We’re experiencing a surge in inquiries due to the November 8, 2023 Toll-Free Restricted Traffic Shutdown Deadline. We’re committed to helping you; please bear with us as there might be some delays. For more details, see Toll-Free Message Verification for US/Canada.

Reject Incoming Calls with a Country Block List

The easiest way to reject incoming phone calls from specific countries is to create a "virtual block list". The block list can then be used to filter incoming calls; automatically hanging up on any calls originating in countries on the list, and forward calls from other countries as normal. This guide walks you through the process of setting up a virtual country 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.

Country block list sample code

Here's our country block list code sample:

exports.handler = function(context, event, callback) {
  // List all blocked countries in quotes by 2-letter ISO-3166 code, separated by a comma
  let countryBlock = event.block || [ "RU", "CN", "IN" ];
  let twiml = new Twilio.twiml.VoiceResponse();
  let blocked = true;
  if (countryBlock.length > 0) {
if (countryBlock.indexOf(event.CallerCountry) === -1) {
blocked = false;
}
} if (blocked) {
twiml.reject();
}
else { // if the caller's country 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 for the country detected in our incoming call request, and compares it to the list of blocked countries in Line 3. If the incoming call is from a blocked country, the call is rejected. If the incoming call is not from a blocked country, the script redirects to another URL for TwiML.

Here's what you'll need to modify:

Line 3: Update [ "RU", "CN", "IN" ] with the 2-letter ISO-3166 country code you want to block calls from.

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.

Create a country 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 Country block list sample code section above).
  6. Once your Function's fields are correct, click Save

Set your phone number to use your country 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