Twilio Node ヘルパーライブラリー
twilio-node ヘルパーライブラリは Twilio APIに対するHTTPリクエストを、Node.js コードを使って書けるようにします。
本ライブラリーはオープンソースのため、機能の不足やバグを見つけた場合はtwilio-nodeプロジェクトにissueをファイルするか、修正にご協力いただければ幸いです。
Server Side only, not Front End
This library is only for back end applications running on Node.js, not for front end, client side applications that you might build with React, Angular, Vue, or plain HTML/CSS/Javascript. For general HTTP requests to the Twilio API, consider writing a Twilio Function and making an HTTP request to the Function from within your app. Twilio also has several Javascript libraries that run in the browser, which you can find at the SDKs page under the Javascript SDKs header.
このNode.jsライブラリーを音声通話向けのTwilio Client JavaScriptライブラリーと組み合わせて使用する方法についてお知りになりたい場合は、Node.jsとVue.jsを使用したブラウザーダイヤラーを参照してください。 エンドユーザーから開発者のTwilioアカウントのクレデンシャルを保護する方法についてご覧いただけます。 この例ではNode.jsのエンドポイントを使用してケイパビリティートークンを作成します。 Vueは使用していない、ですって? このガイドにはReact、Angular、そしてEmberといったフレームワーク用のコード例も用意されています。
インストール
最も簡単に twilio-node をインストールするには、NPM からインストールします。プロジェクトディレクトリーから以下のコマンドを実行することで、ライブラリーをインストールできます。
npm install twilio
そして、あなたのスクリプトの中で、以下を付与します。
var twilio = require('twilio');
インストールをテストする
このようにSMSメッセージ送信を試します
var accountSid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'; // Your Account SID from www.twilio.com/console var authToken = 'your_auth_token'; // Your Auth Token from www.twilio.com/console var twilio = require('twilio'); var client = new twilio(accountSid, authToken); client.messages.create({ body: 'Hello from Node', to: '+12345678901', // Text this number from: '+12345678901' // From a valid Twilio number }) .then((message) => console.log(message.sid));
Using This Library
Authenticate Client
const accountSid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'; const authToken = 'your_auth_token'; const client = require('twilio')(accountSid, authToken);
Create A New Record
const accountSid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'; const authToken = 'your_auth_token'; const client = require('twilio')(accountSid, authToken); client.calls .create({ url: 'http://demo.twilio.com/docs/voice.xml', to: '+14155551212', from: '+15017250604', }) .then(call => process.stdout.write(call.sid));
Get Existing Record
const accountSid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'; const authToken = 'your_auth_token'; const client = require('twilio')(accountSid, authToken); client .calls('CA42ed11f93dc08b952027ffbc406d0868') .fetch() .then(call => console.log(call.to));
Iterate Through Records
const accountSid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'; const authToken = 'your_auth_token'; const client = require('twilio')(accountSid, authToken); client.calls.each(call => console.log(call.direction));
The library automatically handles paging for you. Collections, such as calls
and messages
, have list
and each
methods that page under the hood. With both list
and each
, you can specify the number of records you want to receive (limit
) and the maximum size you want each page fetch to be (pageSize
). The library will then handle the task for you.
list
eagerly fetches all records and returns them as a list, whereas each
streams records and lazily retrieves pages of records as you iterate over the collection. You can also page manually using the page
method.
For more information about these methods, view the auto-generated library docs.
例外ハンドリング
Twilio API が 4XX または 5XX の HTTPレスポンスを受け取ると、twilio-node
ライブラリはその情報をコールバックの error
パラメータの中に含めます。4XX エラーは、通常のAPIオペレーションとして取り扱われ(無効な宛先、該当番号にSMSを送信できない 等)これらは適切にハンドリングされます。
追加ドキュメント
Nodeヘルパーライブラリーのインストールが問題なく完了したら、最新版のライブラリーを使用しているREST APIドキュメント、および各Twilio製品のドキュメントがお役に立つでしょう。 またこちらで、自動生成された最新版のSDK用のドキュメントもご活用ください。
バージョン2.xのSDKにアクセスする
Node SDKのもっとも最近のバージョンは、以前のTwilioアプリケーションで使用されている可能性のあるNode SDKの前バージョンである2.xと互換性がありません。 旧バージョンは引き続き機能し、引き続きドキュメント全体にわたってサンプルコードをご利用いただけます。 NPMからこのバージョンのインストールが必要な場合、下記コマンドで行います。
npm install twilio@2.1.1
代替手段として、こちらからGitHub上の旧バージョンのSDKを直接ダウンロードすることができます。
ヘルプが必要ですか?
誰しもが一度は考える「コーディングって難しい」。そんな時は、お問い合わせフォームから質問してください。 または、Stack Overflow でTwilioタグのついた情報から欲しいものを探してみましょう。