Error: [HTTP 400] Unable to create record: +xxxxxxx is not available happens for all numbers IncomingPhoneNumber API

10k Views Asked by At

The following code was tested with test credentials and worked as expected, but when attempting to purchase a number with the production credentials I always get [HTTP 400] Unable to create record: +xxxxxxx is not available

Below is the code that retrieves the available numbers.

public function getAvailablePhoneNumbers(): array
    {
        if ($this->useTestClient) {
            return [self::VALID_TEST_MAGIC_PHONE];
        }

        $numbers = $this->getClient()
            ->availablePhoneNumbers('US')
            ->local
            ->read([
                'smsEnabled' => true,
            ]);

        /* @var LocalInstance $number */
        return array_map(function ($number) {
            return $number->phoneNumber;
        }, $numbers);
    }

After retrieving the list of phoneNumber I try to acquire a number in the list or pass to the next in the list if it fails, all the numbers on the list fail with the same error, the code that tries to acquire it is.

public function purchaseNumber(string $number): \Twilio\Rest\Api\V2010\Account\IncomingPhoneNumber\LocalInstance
    {
        return $this->getClient()
            ->incomingPhoneNumbers
            ->local
            ->create($number, [
                'SmsApplicationSid' => config('twilio.sms_app_sid'),
            ]);
    }

Edit: I tried to acquire one of the unavailable numbers form the log in the Twilio console and it succeeded.

2

There are 2 best solutions below

3
jassent On

I am not super familiar with PHP, but according to the docs, the syntax is:

$incoming_phone_number = $twilio->incomingPhoneNumbers
                                ->create(["phoneNumber" => "+15017122661"]);

Try using the example code with a known available phone number - including the proper "phoneNumber" reference. If it works, then the problem is within your getAvailablePhoneNumbers() or how you are referencing your $number array.

0
Angel On

The issues was that the service wrapper that we created around Twilio SDK was swapping the testing credentials at incorrect times, therefore in the test API those numbers were in fact unavailable.

What helped to debug this was using the environment variable DEBUG_HTTP_TRAFFIC to force the Twilio SDK to output debug information for the requests, where we noticed the wrong credentials being used.

The solution was to fix the wrapper code to use the correct credentials outside of test mode.