Search code examples
pythonpython-3.xvonage

How do I retrieve available phone numbers in Nexmo?


I want to retrieve all available phone voice phone numbers (Phone Numbers only) using a pattern search without all the parameters.

I have tried the api code given by nexmo. It works, but I only get a limited amount of phone numbers and I am also getting a bunch of other parameters, I don't want. here are the 2 api calls I am using:

phnumbers = client.get_available_numbers("US", {"features": "VOICE"})
phnumbers = client.get_available_numbers("US", {"pattern": "007", "search_pattern": 2})

I just want to have a list of available numbers. I don;t care if it's 1000. Not sure if there is a way to limit the number it brings back. Currently getting a limited amount of number with parameters like the following:

{'count': 394773, 'numbers': [{'country': 'US', 'msisdn': '12014790696', 'cost': '0.90', 'type': 'mobile-lvn', 'features': ['VOICE', 'SMS']}

That's one number. I only want to tell it give me all the voice numbers and get them in a list...Thank you in advance for your help.


Solution

  • I looked at the docs and I don't think it's possible to only get the phone number (also called msisdn) back.

    Instead, for each number, you'll get a thing which includes country, cost, type, etc... , part of, as the docs say, "A paginated array of available numbers and their details".

    If you look at the response, you can see that you get count as the first key/value pair, in your example the count is 394773, and this is the total count of numbers available for the search condition you specified when you made the request.

    Now, I don't know all the reasons but to send back one response with a payload of 394773 numbers would probably be taxing too much the system.


    What you can do:


    From my tests, if you specify a size of 100, then you'll get a response with 100 records per page and you have the index parameter which you can use to paginate (anything above 100 for size and you get only 10 records).

    So, if the count is 394773 for your search query, with size = 100, we have 3947 + 1 pages (the last page (index = 3948) only has 73 records) and you would have to get them one by one with a total of 3948 requests passing the appropriate index value.

    Of course you can reduce the count if you pass a more specific search query.

    I understand what you want, and I don't work for Nexmo, and again, after reading the docs I don't think it's possible to get everything back in just one request. You'll just need to be more specific in your search query.


    Docs:


    Retrieve inbound numbers that are available for the specified country.