Search code examples
twiliotwilio-api

How to restore previously released phone number using Twilio API?


By following this article, I released a Phone Number using Twilio API by submitting an HTTP DELETE request to the IncomingPhoneNumber REST API Resource and it succeeded, but now I cannot purchase it back using Twilio IncomingPhoneNumber API. I want to dynamically repurchase back the number using Twilio API and not using console.

Note: I am using node version: 16.20.2 and Twilio library version: 3.72.0

After releasing the number, using the same number I tried to purchase the number back using the Twilio IncomingPhoneNumber resource but got an error:

Expected: purchased successfully and should have got a new sid for that number.

Got: RestException [Error]: +12059557580 is not available.

Here is the code which I use to purchase a number:

exports.purchaseNumber = async (number, voiceSid) => {
  try {
    //we can pass voice application sid
    let status = await client.incomingPhoneNumbers.create({
      phoneNumber: number,
      voiceApplicationSid: voiceSid,
      smsApplicationSid: voiceSid,
    });
    return status;
  } catch (error) {
    if (error.code == 20404) {
      throw { message: lang.NUMBER_NOT_FOUND, statusCode: FAIL };
    }
    throw error;
  }
};

And here is the code to release the number:

exports.deletePhoneNumber = async (id) => {
  return client.incomingPhoneNumbers(id).remove();
};

Solution

  • This functionality is, as of now, only available in the Console and not via API. Sorry.

    By using Console, you can restore numbers that you released in the past 10 days. Release numbers will show up on Console's Released Numbers page. Simply click on the Repurchase Number button to provision the number back to your project. Docs