12

I have a twilio account for sending SMS. However, i need guidance on how to see a list of my account phone numbers by calling its API. I have tried to navigate through the different menus in API but i dont see any option to get list of my account phone numbers. Thank you in advance for your guidance.

Snow Fox
  • 389
  • 6
  • 15

2 Answers2

12

Twilio developer evangelist here.

If you want to list the phone numbers you have in your Twilio account through the API you need the Incoming Phone Numbers list resource.

You can access this easily with the Twilio rubygem like so:

require 'twilio-ruby'

@client = Twilio::REST::Client.new YOUR_ACCOUNT_SID, YOUR_AUTH_TOKEN
numbers = @client.account.incoming_phone_numbers.list
numbers.each do |number|
  # do something with the number
  puts number.phone_number
end
philnash
  • 70,667
  • 10
  • 60
  • 88
  • hey @philnash. Is there a way I can specify the number of records the `.list` endpoint will return? Right now it always gives 50. I would like to fetch _all_ of my accounts phone numbers, and this is just a subset – Alex Neigher May 19 '16 at 23:00
  • answer to my previous question: the ruby API is actually mis-documented. The proper syntax is: `[...].incoming_phone_numbers.list(page_size:1000)` – Alex Neigher May 19 '16 at 23:03
  • Hell yeah you can! Just use the `PageSize` attribute. You can get a maximum of 1000 items per page. Check out this page for a bit more detail: https://www.twilio.com/docs/api/rest/response – philnash May 19 '16 at 23:06
  • @philnash Hey, How can I acheive this in PHP SDK? I can see on docs that only create method available. – Code Cooker Apr 09 '20 at 02:03
  • @CodeCooker Here's a link to the PHP sample for listing phone numbers: https://www.twilio.com/docs/phone-numbers/api/incomingphonenumber-resource?code-sample=code-list-all-incomingphonenumber-resources-for-your-account&code-language=PHP&code-sdk-version=6.x – philnash Apr 09 '20 at 02:04
  • @philnash Wow, Hey thank you so much for super fast reply, and it worked! I'm searching all the way but didn't see this page :) – Code Cooker Apr 09 '20 at 02:08
  • 1
    @CodeCooker Notifications on Stack Overflow get my attention sometimes Sorry it was hard to find, but glad it's working for you now! – philnash Apr 09 '20 at 02:09
  • @philnash Hey, Hopefully you doing good. I've set to work on a feature called conversation of messsages. that means I will have to show messages that sent by sender/reciver like a chat between two peoples. is there any way Twilio can help on it or i need a complete custom build? I see this https://www.twilio.com/docs/sms/tutorials/how-to-create-sms-conversations-php but not that much helpful – Code Cooker Apr 10 '20 at 13:44
  • @CodeCooker not sure what you mean about how Twilio can help? Are you looking for UI components? Or just need a jumping off point for the API requests? – philnash Apr 11 '20 at 00:41
  • This needs to be updated to `@client.api.account.incoming_phone_numbers.list` because of the following error: `undefined method `account'` – danielsmith1789 Jun 23 '23 at 18:29
1

Here is a PHP version...

<?php
namespace App\Test;
use Twilio\Rest\Client;
use Twilio\Rest\API\V2010\AccountContext;
use Twilio\Rest\Api\V2010\Account\IncomingPhoneNumberList;
use Twilio\Rest\Api\V2010\Account\IncomingPhoneNumberInstance;

class TestClient {
    private $client = null;
    public function __construct() {
        $sid = $_ENV['TWILIO_SID'];
        $token = $_ENV['TWILIO_TOKEN'];
        $this->client = new Client($sid, $token);
    }
    public function getActiveNumbers()
    {
        $context = $this->client->getAccount();
        $activeNumbers = $context->incomingPhoneNumbers;
        $activeNumberArray = $activeNumbers->read();
        $numbers = [];
        foreach($activeNumberArray as $activeNumber) {
            error_log('active number = '.$activeNumber->phoneNumber);
            $numbers[] = (object)[
                'number' => $activeNumber->phoneNumber,
                'name' => $activeNumber->friendlyName,
                'sid' => $activeNumber->sid
            ];
       }
       var_dump($numbers);
       return $numbers;
    }
}
AQuirky
  • 4,691
  • 2
  • 32
  • 51