node.jstwiliotwilio-apitwilio-node

Twilio: How to buy number through nodejs?


I need to buy twilio numbers through nodejs. I could not find any answer regarding that. Is it really possible to buy twilio number through node.js ?

I really appreciate any guidance.

Thanks


Solution

  • See Alan's response for Twilio's docs. In short:

    1. create a folder "twilio-node-numbers", open a terminal and change to this folder
    2. run "npm init -y"
    3. run "npm install twilio"
    4. create a ".env" file, add your Twilio credentials you can find on your Twilio console
    TWILIO_ACCOUNT_SID=AC...
    TWILIO_AUTH_TOKEN=4f...
    
    1. create a "get_available_numbers.js" file
    const accountSid = process.env.TWILIO_ACCOUNT_SID;
    const authToken = process.env.TWILIO_AUTH_TOKEN;
    const client = require('twilio')(accountSid, authToken);
    
    client.availablePhoneNumbers('CA')
        .local
        .list({ areaCode: 604, limit: 20 })
        .then(local => local.forEach(l => console.log(l.friendlyName)));
    

    CA is the country code and 604 is the area code

    1. run "node get_available_numbers.js"

    You will get a list of available phone numbers based on the country code and area code you provided in the get_available_numbers.js

    1. create a "buy_phone_number.js" file
    const accountSid = process.env.TWILIO_ACCOUNT_SID;
    const authToken = process.env.TWILIO_AUTH_TOKEN;
    const client = require('twilio')(accountSid, authToken);
    
    client.incomingPhoneNumbers
        .create({ phoneNumber: '+16047574779' })
        .then(incoming_phone_number => console.log(incoming_phone_number.sid));
    

    Where +16047574779 is one of the phone numbers from the list you got after running "node get_available_number.js"

    1. run "node buy_phone_number.js"

    You will get a response with information about your provisioned phone number