0

I'm attempting to use the Twilio Lookup service, purely the validate and format numbers into international format (even without having the original country code)

This is the Twilio example, with a slight modification, seen in their docs:

    TwilioClient.Init(accountSid, authToken);

    var type = new List<string> {
   "national_format"
    };

    var phoneNumber = PhoneNumberResource.Fetch(
        type: type,
        pathPhoneNumber: new Twilio.Types.PhoneNumber("07738629111")
    );

    Console.WriteLine(phoneNumber.NationalFormat);
    return phoneNumber.NationalFormat;

Errors, with the following

{"The requested resource /PhoneNumbers/07738629111 was not found"}

Yet, this is the same as Twilio docs... furthermore, my accountSid and authToken are correct, as I'm using the API doing something else.

Could this possibly be a coding issue, and does anybody know how to solve it?

philnash
  • 70,667
  • 10
  • 60
  • 88
MattHodson
  • 736
  • 7
  • 22
  • How would Twilio know what country the number is from to format it correctly? You need the country code when using national format. – Alan Apr 28 '21 at 00:44

1 Answers1

0

Twilio developer evangelist here.

To lookup a number using the Twilio Lookup API you need to provide the number you are looking up in e.164 format (or international format).

In this case, it appears you are looking up a UK mobile number. So your lookup like should look like this:

pathPhoneNumber: new Twilio.Types.PhoneNumber("+447738629111")

The other thing is that when the Twilio library looks up a number that is not a valid e.164 number (like in your example) then it will throw a not found error. So you should write your code to handle not found errors and report that back to yourself as an invalid number.

philnash
  • 70,667
  • 10
  • 60
  • 88
  • I am a new developer. How do you handle "not found error?" could you provide a basic example. Would be nice if this was covered in the example or sdk. Thanks – Barry MSIH Jun 09 '21 at 12:43
  • I think you need to wrap the code in try/catch and catch any [`RestException`s](https://github.com/twilio/twilio-csharp/blob/main/src/Twilio/Exceptions/RestException.cs). You can then inspect the exception's `code` property and if it is a [20404](https://www.twilio.com/docs/api/errors/20404) then it is a not found error. – philnash Jun 10 '21 at 04:23