parkeragee
parkeragee

Reputation: 313

What is the syntax for deleting/releasing Twilio phone numbers in Node.js?

I'm getting a 404 error when trying to delete a Twilio phone number via the API.

Here's my code:

  var twilioSID = user.numberSID; // PN946a0603c974be563c5916f865be4d0b
  var accountSid = '{removed}';
  var authToken = '{removed}';
  var client = require('twilio')(accountSid, authToken);

  client.incomingPhoneNumbers(twilioSID).delete(function(err, deleted) {
    if (err){
      console.log(err);
    } else {
      console.log('Deleted from Twilio');
    }
  });

Here is the error I'm getting in the console:

  { 
     status: 404,
     message: 'The requested resource /2010-04-01/Accounts/{removed}/IncomingPhoneNumbers/PN946a0603c974be563c5916f865be4d0b.json was not found',
     code: 20404,
     moreInfo: 'https://www.twilio.com/docs/errors/20404' 
  }

The Twilio API doesn't have hardly any documentation for deleting numbers either. Any ideas on why this is not working?

Upvotes: 3

Views: 1371

Answers (5)

M3talM0nk3y
M3talM0nk3y

Reputation: 1432

As of this writing, the function to remove Twilio phone numbers via the API is called remove. If you try to use delete, you should receive the following error:

client.incomingPhoneNumbers(...).delete is not a function

I wasn't able to find any reference in the Twilio API docs; found this by reading the source, Luke.

Here's an example invoking remove:

client.incomingPhoneNumbers(sid).remove()
.then(function(deleted) {
    // Success
})
.catch(function(error) {
    // Handle error
});

Upvotes: 2

user3705074
user3705074

Reputation: 21

They changed it from delete to remove in their node js module.

Upvotes: 2

Vladyslav Babenko
Vladyslav Babenko

Reputation: 1369

@parkeragee Your solution is working

client.incomingPhoneNumbers(poneNumberSID).delete(function(err, deleted) {
    if (err){
      console.log(err);
    } else {
      console.log('Deleted from Twilio');
    }
});

Upvotes: 2

parkeragee
parkeragee

Reputation: 313

My issue was that I was overwriting my production phone nuber numberSID variable with my test phone number. So the user.numberSID; that I was assigning to a variable was for the Twilio test phone numbers. So, when it was searching for that numberSID, it returned a 404.

Upvotes: 1

mscdex
mscdex

Reputation: 106736

According to their REST API documentation, you can send an HTTP DELETE request to a url like /2010-04-01/Accounts/{AccountSid}/IncomingPhoneNumbers/{IncomingPhoneNumberSid}. So the URL in the error message looks almost right, except for the .json on the end. Either way it looks like a bug in their code if the phone number is in fact still attached to your account.

Upvotes: 1

Related Questions