简体   繁体   中英

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

Here is the link to the relevant documentation.

IncomingPhoneNumber resource

Reference the Node.js code sample: "Provision a Phone Number"

You can get a list of available numbers using another API:

AvailablePhoneNumber resource

and the associated sub-resources on that page (Local, Toll Free, Mobile).

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM