How to route inbound calls based on the calling number's state code

Dear all,

I want to route inbound calls depending on the calling number’s state code. Assuming I have 10 offices based in 10 different states and the client calls my toll free number showing on my website 888-xxx-xxx. I want their call to come to the state office where they called from.

If let’s say Alex dialed my toll free number in New York, I want to route the call to get routed to the DID which is assigned to Newyork Office. Currently I have exchange Auto Attendant set with multiple numbers for each of those offices.

Is this doable on FreePBX? Can anyone guide me to do this.

Thank you

It is doable. You would have to code this because there is nothing for this. You are looking at 100s of NPAs (area codes)

1 Like

If I understand, you want to route the call based on the caller ID? You should be able to do that with Inbound routes. Look at CID routing and CID Priority Routing.

1 Like

These two answers from @comtech and @BlazeStudios are proof of how awesome the system is.

They are completely different and both completely true.

You can write a custom context for your 800 number inbound route that looks up all of the 1000 (theoretically) possible NPAs and routes the call based on those. If, on the other hand, you only have a handful of “agent” phone numbers (as destinations) you can do it using inbound routes and CID Priority routing.

I’d start with CID Priority Routing. When that gets too painful, write a database based context that you can manage more easily.

1 Like

Well here is the issue with using Inbound Routes. All the calls are going to 1 DID, a toll free DID, which means it would require a Inbound Route for each NPA (area code) to route the calls to the proper destination. That would mean upwards of 800+ routes to one DID just so it could go to 10 different internal destinations. There is also the case of the 10 states limit. What if someone calls from a state where there is no local office? Where do those go? What about those that do not present a CallerID?

Basically you can write something that checks the CallerID via a script to a backend. What type of backend is your choice. Or you can maintain a large csv file and use the Bulk Handler to add/update/remote Inbound Routes if you go with using individual inbound routes.

1 Like

A list such as could be easily parsed by a script and converted to a .csv for import by Bulk Handler.

However, you may want to include time-of-day and other inputs, especially if your demographic is young professionals. A growing trend is to keep (on your mobile) the number from your college town. For example, an MIT graduate who took a job with Google in Mountain View calls in at 4 pm PST and gets routed to your Boston office. He hears the ‘sorry, we’re closed’ message and calls a competitor instead.


just download at:-

convert by whatever means to .csv

Create a database key on NPA with value = state

while IFS=, read -r NPA STATENAME STATE; do echo "rasterisk -x 'database put NPA $NPA $STATE'"; done < area_codes_by_state.csv|bash

Create a database key on state with value = destination

for i in $(cat area_codes_by_state.csv |cut -d ',' -f3|sort -u);do echo "rasterisk -x 'database put STATE $i \${EXTEN}'";done|bash

update ${EXTEN} to the destination for any states you cover for example:-

rasterisk -x 'database put STATE 4566'

Then something like (pseudo code)

Exten => _NXXNXXXXXX,1,DBGET state from CID
Exten => _NXXNXXXXXX,n.Goto(DBGETdestination from state


Hi Blaze, I have got the NPAs already. I would appreciate if you have an example of how can I route these based on the following criteria

I have Toll free number e.g. 888-xxx-xxx along with another 10 other toll free numbers for the states where I have offices in, I want that whoever dials the main toll free number to be routed to their own state office’s toll free number.

thank you

You shouldn’t be wanting that. Not only does it degrade latency and possibly introduce quality issues, you’d be paying per minute for both inbound legs. Whoever dials the main number should be routed to wherever their own office’s toll free number would be routed. If the offices all have IP PBXes, intracompany trunks (SIP or IAX) are what’s wanted.

I have all the DIDs from on the same SIP Trunk provider, I am not worried about that.
All what concerns me is the dial plan and how to route inbound calls
Thank you

Not knowing who the provider is, I can’t dispute that, but in my experience it’s not typical.

You could test this by calling the toll free number of one office from another. Then look at the CDRs on the provider’s portal and confirm that both the outgoing and incoming calls were billed at zero. Even if that is the case, there is still a possible issue with channel limitations (if your plan is limited to a specific number of concurrent calls).

But if that’s what you really want to do, create a Misc Destination for each office and point each Inbound Route for the ‘main’ toll free number to the appropriate one.

What phone systems do you have in the branch offices?

Thank you for your comment Stewart, This is a hosted system we are using Skype for Business and all offices are using the Skypefb Client to make/receive calls.

I have big number of codes for each state, and I think instead of creating inbound routes which will take a lot of time and routes, I guess going with Dicko’s solution will be most convenient for me however I am new to FreePBX (Yes I did some integrations before) but still I never did this kind of complicated routing.

Could you or Dicko please assist me with this in more detail, I downloaded the XLS file and converted it to csv already. I replaced codes for Detroit with the ones that I have which I want to route to Detroit’s office from the Toll Free Number.

After I have the file ready, where do I place the file in the server and where do I type these commands that Dicko wrote? In SSH?

How do I create a database key on NXX and state?
Is there any article that write these in detail?

Thank you so much

I gave you the basics on making the NPA and STATE families , you run those from bash. Detroit is in Michigan (MI) which has area codes (NPA) 231,248,269,313,517,586,616,734,810,906,947 and 989

rasterisk -x 'database show NPA'|grep MI

You need to extract the NPA from with ${CALLERID(number):0:3} then lookup the state for that , when you have the sate you look up the destination and ‘goto’ there.

rasterisk -x ‘database show NPA’|grep MI

Being new is always a problem, but reading through,%204th%20Edition.pdf

will get you familiar, when familiar you can hone-in on ‘contexts’ (for call control) ‘substrings’ (to extract the NPA from ${CallerID(Number)} and database usage (for the ‘puts’ and ‘gets’)

I didn’t withdraw anything. how can I keep this post?
I would like to ask if when I create new inbound routes, do I have to write the toll free Inbound in the DID number section or leave it empty?

I have done a test and created two routes with the same DID number, it worked but I am getting a warning saying the following:

Inbound Route: +1248number (1248number/_320NXXXXXX)

I would appreciate any comments

I withdrew my post because it appeared that my suggestion had already been made and rejected.

Hi Dicko,

I have uploaded the csv file after I edited it with the codes that I have previously however, I can’t see the codes when I write type “rasterisk -x ‘database show’” I get state abbreviations with no codes on the left.

What are the columns headers that is expected from the script that you given me? I would appreciate if you could walk me through this step by step.

Thank you


I don’t think you did anything like what I suggested. I certainly never suggested editing anything

Show each step you did

(and explain what you mean by ‘UPLOADED’ )

I placed the csv document in home/asterisk/ and then ran the scripts you have mentioned earlier:

I didn’t change the columns header, Kept them as they are in the xls document but after I got the result I noticed its not showing state code so I replaced column headers with NPA,STATENAME,STATE.

Then re-run the commands and it said its updated them.

Create a database key on NPA with value = state

while IFS=, read -r NPA STATENAME STATE; do echo "rasterisk -x 'database put NPA $NPA $STATE'"; done < area_codes_by_state.csv|bash

Create a database key on state with value = destination

for i in $(cat area_codes_by_state.csv |cut -d ',' -f3|sort -u);do echo "rasterisk -x 'database put STATE $i \${EXTEN}'";done|bashCreate a database key on NPA with value = state

I have no idea about this, but I have the same cell number I had in 90’s when I lived in St. Louis. Now I live in Chicago. This will break your entire thing unless Missouri and Illinois gonto the same place.

On top of that fully half of the parents of my chidren’s friends have non-Chicago area codes on their cell phones.

Oh and my children just got phones. I got St. Louis numbers for them for the hell of it.

As @Stewart1 and others have said, you can certainly make this work like you want, it is increasingly pointless.

Show the output of each without the pipe through bash at the end