Search code examples
pythontwiliotwilio-api

Fetch all active fone numbers for a twilio account with python library


i am trying to get all active phone numbers for an account using twilio python library. i tried the proposed in this solution but is outdated. Thanks.


Solution

  • To get all your active phone numbers in your account, you can request the incoming phone numbers API and iterate through the results which will automatically page through the API. To make fewer requests, you can change the page size up to 1000.

    import os
    from twilio.rest import Client
    
    account_sid = os.environ['TWILIO_ACCOUNT_SID']
    auth_token = os.environ['TWILIO_AUTH_TOKEN']
    client = Client(account_sid, auth_token)
    
    incoming_phone_numbers = client.incoming_phone_numbers.list(page_size=1000)
    
    for phone_number in incoming_phone_numbers:
        print(phone_number.sid)