Created
March 24, 2016 18:13
-
-
Save choppen5/3e32d7fd6bce3213c5b4 to your computer and use it in GitHub Desktop.
Use paging with the Twilio API to cycle through all of your phone numbers. Filter the results by the app sid associated with it.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Cycle through all phone numbers, and filter on appsid | |
//you can't query by Appsid via the API, so getting all numbers and iterating is the way to do it. | |
//Module | |
//npm install request async | |
var request = require('request'); | |
var async = require('async'); | |
//Twilio account variables | |
var sid = 'ACxxxx'; | |
var auth = '52xxxxxx'; | |
var appSid = 'APxxxxxx'; //appsid for a phone number you are looking for. | |
var baseUrl = 'https://api.twilio.com/2010-04-01/Accounts/' + sid + '/IncomingPhoneNumbers.json?PageSize=1000&Page=0'; | |
var auth = { | |
user: sid, | |
pass: auth | |
} | |
var parameters = {} | |
var count = 0; | |
pullNumbers(baseUrl); | |
function pullNumbers(uri) { | |
parameters = { | |
url: uri, | |
auth: auth, | |
} | |
request.get(parameters, function(error, httpResponse, body){ | |
//don't really need async, but if you call pullNumbers with | |
async.forEachOf(JSON.parse(body).incoming_phone_numbers, function(item,key){ | |
count = count +1; | |
if (item.voice_application_sid == appSid) { | |
console.log( count + " found appsid " + item.phone_number + " appsid = " + item.voice_application_sid ) ; | |
} | |
}); | |
if(JSON.parse(body).next_page_uri){ | |
console.log("getting nextpage, count = " + count); | |
pullNumbers('https://api.twilio.com' + JSON.parse(body).next_page_uri); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment