OpenCNAM for Asterisk cidnam lookup

Long ago, in a world much different from today, one could check, anywho, and even google for Caller ID names. Those days are gone my friends. It’s slowly becoming a pay to play world…

I just played with a solution from OpenCNAM that was pretty freaking simple. There’s a negativeit ain’t free. The cost is nominal… $0.004/lookup. That means that $10 will get you 2500 lookups.

Before you get all crazy on me — yes, there is a free “hobbyist” version. I think it’s worthless, since all the cellphones I tried returned the cidnam value of “currently unavailable for Hobbyist Tier users.”

I digress.

I chose Perl to integrate opencnam into my Asterisk dialplan. Why? Because I like Perl. Yes, I’ve heard of Python, and Lua, and Ruby, and when it comes down to it… I like Perl.

Here’s how I chose to integrate the lookup:

Asterisk Dialplan

exten => _X.,1,Verbose(3,Look an incoming call. Yay.)
exten => _X.,n,AGI(,${CALLERID(num):-10})
exten => _X.,n,Verbose(3,result: ${OPENCNAM})

#!/usr/bin/perl -w
use strict;
my ($phone, $url, $apikey, $authkey, $result);

while(<STDIN>) {
    last unless length($_);

if ($ARGV[0]) {
    $phone = &URLEncode($ARGV[0]);
} else {
    &setvar("OPENCNAM", "No Phone");
    &setvar("CALLERID(name)", "Unknown");
    &printverbose("OPENCNAM: No CALLFROM received.",2);

#Get the cid
$apikey = "APIKEY";
$authkey = "AUTHKEY";
$url = "";

$result = qx(curl -m 2 -s https://$apikey:$authkey@$url+1$phone?format=text);

#or free version would be...

#$result = qx(curl -m 2 -s$phone);

if ($result) {
    &setvar("OPENCNAM", "$result");
    &setvar("CALLERID(name)", "$result");
    &printverbose("OPENCNAM: $result.",2);
} else {
    &setvar("OPENCNAM", "FAIL");
    &setvar("CALLERID(name)", "Unknown");
    &printverbose("OPENCNAM: Timeout or error",2);

sub URLEncode {
   my $theURL = $_[0];
   $theURL =~ s/([W])/"%" . uc(sprintf("%2.2x",ord($1)))/eg;
   return $theURL;

sub setvar {
    my ($var, $val) = @_;
    print STDOUT "SET VARIABLE $var "$val" n";
    while(<STDIN>) {
        m/200 result=1/ && last;

sub printverbose {
    my ($var, $val) = @_;
    print STDOUT "VERBOSE "$var" $valn";
    while(<STDIN>) {
        m/200 result=1/ && last;

The beauty is you can choose when to run this… considerations (for you to save money) would be:

  • If cnam already exists, skip the check.
  • Run a local check first… for example, using a local phonebook or db lookup
  • Only running calls from the PSTN
  • etc.

Happy coding.


Randall Degges 2013-07-18 Reply

Hey Fred,

I’m the CTO at OpenCNAM, love your post! Just wanted to let you know that we’ve actually got an Asterisk module that’s open source you can use (you can see it on GitHub here:

Also: our Hobbyist Tier is getting some awesome improvements right now. We currently have slghtly over 100 million CNAM entries available for free (but different companies imposes different rules for caching on us) — so it’s becoming quite a respectable product for causal users 🙂

Lastly, I’d love to hook you up with some funding for your account! Please shoot me an email:

Fred Posner 2013-07-18 Reply

Thank you! I’ll reach out to you shortly.

Keith Muenze 2013-10-07 Reply

Hi Fred,

I have integrated with OpenCNAM as well. I found it to be completely easy although I find that I get a lot of responses like “Cell Phone, OH” or “Cell Phone, NY”.

Have you found a good way to get actual caller names for cell phones?

@Randall: Perhaps you may know as well since it is at the heart of your business.


Brent 2015-02-17 Reply

Hey everyone,

Over the last several months we have enhanced the accuracy, coverage and speed of OpenCNAM. Please let me know if you have any questions or would like to test the Professional Tier. I would be happy to issue a bit of test credit so you can see the results for yourself.


Robert 2016-03-16 Reply

This looks worth trying. For the database inquiry I was wondering if anyone already knows a way to try something like seeing if the incoming call’s number is already in a user’s Google Voice contact database – particularly useful for folks using their Google Voice credentials within Asterisk. For extra credit, if the caller is not already there perhaps prompt with call screening.

Fred Posner 2016-03-17 Reply

Hi Robert,

Haven’t really looked at this code in a couple years, the version on github was last updated 2 years ago.

The google contacts sounds like a nightmare as you’d have to download the entire contact list and have credentials for each user. Looks like there’s no way to query google contacts via phone number.

This being said, a database lookup isn’t a crazy idea and then you can always run a weekly/whenever sync of the database to google contacts.

Telo 2016-08-04 Reply

Hi Fred,

This is Brent from OpenCNAM by Telo. Since you created your repo, we have launched OpenCNAM V3. There are some really powerful new benefits available through the OpenCNAM Plus service level with V3 that include international support (better than 50% coverage outside of North America) and greatly improved North American coverage (up to 87% coverage including toll-free and the elusive wireless and VoIP numbers that have been gaps in traditional CNAM coverage). New accounts get $0.50 to test with, but anyone requires a little more, just use live chat or the toll-free number and let one of us know.

It’s still the same simple API, but with a lot more power! Let us know if we can be of any assistance to you. We are huge fans of open source, so if there is any way we can help get the word out or let devs play with our products please let us know!


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.