简体   繁体   English

Twilio:如何通过 nodejs 购买号码?

[英]Twilio: How to buy number through nodejs?

I need to buy twilio numbers through nodejs.我需要通过 nodejs 购买 twilio 数字。 I could not find any answer regarding that.我找不到任何关于此的答案。 Is it really possible to buy twilio number through node.js ?真的可以通过 node.js 购买 twilio number 吗?

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"参考 Node.js 代码示例:“Provision a Phone Number”

You can get a list of available numbers using another API:您可以使用另一个 API 获取可用号码列表:

AvailablePhoneNumber resource 可用电话号码资源

and the associated sub-resources on that page (Local, Toll Free, Mobile).以及该页面上的相关子资源(本地、免费、移动)。

See Alan's response for Twilio's docs.请参阅 Alan 对 Twilio 文档的回复。 In short:简而言之:

  1. create a folder " twilio-node-numbers ", open a terminal and change to this folder创建一个文件夹“ twilio-node-numbers ”,打开一个终端并切换到这个文件夹
  2. run " npm init -y "运行“ npm init -y
  3. run " npm install twilio "运行“ npm install twilio
  4. create a " .env " file, add your Twilio credentials you can find on your Twilio console创建一个“ .env ”文件,添加您可以在 Twilio 控制台上找到的 Twilio 凭据
TWILIO_ACCOUNT_SID=AC...
TWILIO_AUTH_TOKEN=4f...
  1. create a " get_available_numbers.js " file创建一个“ get_available_numbers.js ”文件
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 CA是国家代码,604是区号

  1. run " node get_available_numbers.js "运行“节点 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您将根据您在 get_available_numbers.js 中提供的国家/地区代码和区号获得可用电话号码列表

  1. create a " buy_phone_number.js " file创建一个“ buy_phone_number.js ”文件
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"其中 +16047574779 是您在运行“node get_available_number.js”后获得的列表中的电话号码之一

  1. run " node buy_phone_number.js "运行“节点buy_phone_number.js

You will get a response with information about your provisioned phone number您将收到有关您提供的电话号码信息的回复

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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