SMS 用 API
Nexmo の SMS 用 API を使用すると、簡単な REST 用 API を使ったテキストメッセージのやり取りが世界中で可能になります。
- プログラムを使って、大量の SMS を世界中で送受信。
- 低レイテンシと確実性の高い方法で SMS を送信。
- ローカル番号を使って SMS を受信。
- 使い慣れた Web テクノロジーでアプリケーションを拡張。
- 使用した分だけ支払い、追加の支払いはなし。
内容
このトピックでは次の内容について説明します。
- 最初のステップ - 最初のステップをすぐ開始するための情報
- トラブルシューティング - メッセージオブジェクトのステータスフィールドとエラーコード情報
- 基本概念 - 初歩的概念
- ガイド - SMS 用 API の使用方法に関する説明
- コードスニペット - 特定のタスクに役立つコードスニペット
- ユースケース - コードサンプルのユースケース
- 関連情報 - REST 用 API のドキュメント
最初のステップ
SMS を送信
以下の例では、選択した番号への SMS 送信方法を説明します。
Nexmo に登録していない場合は、まず Nexmo アカウントにサインアップします。Dashboard の [最初のステップ] ページの API キーとシークレットを書き留めます。
次のサンプルコードに含まれるプレースホルダーの値を置き換えます。
| キー | 説明 |
|---|---|
NEXMO_API_KEY | Nexmo の API キー。 |
NEXMO_API_SECRET | Nexmo の API シークレット。 |
Write the code
Add the following to send-sms.sh:
curl -X "POST" "https://rest.nexmo.com/sms/json" \
-d "from=$VONAGE_BRAND_NAME" \
-d "text=A text message sent using the Vonage SMS API" \
-d "to=$TO_NUMBER" \
-d "api_key=$VONAGE_API_KEY" \
-d "api_secret=$VONAGE_API_SECRET"Run your code
Save this file to your machine and run it:
Prerequisites
npm install @vonage/server-sdkCreate a file named send.js and add the following code:
const { Vonage } = require('@vonage/server-sdk');
const vonage = new Vonage({
apiKey: VONAGE_API_KEY,
apiSecret: VONAGE_API_SECRET,
});Write the code
Add the following to send.js:
const from = VONAGE_BRAND_NAME;
const to = TO_NUMBER;
const text = 'A text message sent using the Vonage SMS API';
vonage.sms.send({ to, from, text })
.then((resp) => {
console.log('Message sent successfully');
console.log(resp);
})
.catch((err) => {
console.log('There was an error sending the messages.');
console.error(err);
});Run your code
Save this file to your machine and run it:
Prerequisites
Add the following to build.gradle:
implementation 'com.vonage:server-sdk-kotlin:1.1.2'Create a class named SendMessage and add the following code to the main method:
val client = Vonage {
apiKey(VONAGE_API_KEY)
apiSecret(VONAGE_API_SECRET)
}Write the code
Add the following to the main method of the SendMessage class:
val response = client.sms.sendText(
from = VONAGE_BRAND_NAME,
to = TO_NUMBER,
message = "Hello from Vonage SMS API"
)
println(
if (response.wasSuccessfullySent())
"Message sent successfully."
else
"Message failed with error: ${response[0].errorText}"
)Run your code
We can use the application plugin for Gradle to simplify the running of our application. Update your build.gradle with the following:
apply plugin: 'application'
mainClassName = project.hasProperty('main') ? project.getProperty('main') : ''Run the following gradle command to execute your application, replacing com.vonage.quickstart.kt.sms with the package containing SendMessage:
Prerequisites
Add the following to build.gradle:
implementation 'com.vonage:server-sdk:8.15.1'Create a class named SendMessage and add the following code to the main method:
VonageClient client = VonageClient.builder().apiKey(VONAGE_API_KEY).apiSecret(VONAGE_API_SECRET).build();Write the code
Add the following to the main method of the SendMessage class:
TextMessage message = new TextMessage(VONAGE_BRAND_NAME,
TO_NUMBER,
"A text message sent using the Vonage SMS API"
);
SmsSubmissionResponse response = client.getSmsClient().submitMessage(message);
if (response.getMessages().get(0).getStatus() == MessageStatus.OK) {
System.out.println("Message sent successfully.");
} else {
System.out.println("Message failed with error: " + response.getMessages().get(0).getErrorText());
}Run your code
We can use the application plugin for Gradle to simplify the running of our application. Update your build.gradle with the following:
apply plugin: 'application'
mainClassName = project.hasProperty('main') ? project.getProperty('main') : ''Run the following gradle command to execute your application, replacing com.vonage.quickstart.sms with the package containing SendMessage:
Prerequisites
Install-Package VonageCreate a file named SendSms.cs and add the following code:
using Vonage;
using Vonage.Request;Add the following to SendSms.cs:
var credentials = Credentials.FromApiKeyAndSecret(
vonageApiKey,
vonageApiSecret
);
var vonageClient = new VonageClient(credentials);Write the code
Add the following to SendSms.cs:
var response = await vonageClient.SmsClient.SendAnSmsAsync(new Vonage.Messaging.SendSmsRequest()
{
To = toNumber,
From = vonageBrandName,
Text = "A text message sent using the Vonage SMS API"
});
Console.WriteLine(response.Messages[0].To);Prerequisites
composer require vonage/clientCreate a file named send-sms.php and add the following code:
$basic = new \Vonage\Client\Credentials\Basic(VONAGE_API_KEY, VONAGE_API_SECRET);
$client = new \Vonage\Client($basic);Write the code
Add the following to send-sms.php:
$response = $client->sms()->send(
new \Vonage\SMS\Message\SMS(TO_NUMBER, BRAND_NAME, 'A text message sent using the Vonage SMS API')
);
$message = $response->current();
if ($message->getStatus() == 0) {
echo "The message was sent successfully\n";
} else {
echo "The message failed with status: " . $message->getStatus() . "\n";
}Run your code
Save this file to your machine and run it:
Prerequisites
pip install vonageWrite the code
Add the following to send-an-sms.py:
from vonage import Auth, Vonage
from vonage_sms import SmsMessage, SmsResponse
client = Vonage(Auth(api_key=VONAGE_API_KEY, api_secret=VONAGE_API_SECRET))
message = SmsMessage(
to=TO_NUMBER,
from_=VONAGE_BRAND_NAME,
text="A text message sent using the Vonage SMS API.",
)
response: SmsResponse = client.sms.send(message)
print(response)Run your code
Save this file to your machine and run it:
Prerequisites
gem install vonageCreate a file named send.rb and add the following code:
client = Vonage::Client.new(
api_key: VONAGE_API_KEY,
api_secret: VONAGE_API_SECRET
)Write the code
Add the following to send.rb:
client.sms.send(
from: VONAGE_BRAND_NAME,
to: TO_NUMBER,
text: 'A text message sent using the Vonage SMS API'
)Run your code
Save this file to your machine and run it:
トラブルシューティング
API コールで問題が生じた場合、ステータスフィールドを再確認して、エラーコードを必ず特定してください。
基本概念
Nexmo の SMS 用 API を使い始める前に、次の項目を把握しておきます。
番号形式 - SMS 用 API には E.164 format の電話番号が必要です。
認証 - アカウント用 API キーとシークレットを使用する SMS 用 API 認証。
Web フック - SMS 用 API は着信 SMS や受信確認など、判断基準となる HTTP リクエストをアプリケーションの Web サーバーに出します。
ガイド
- Overview: Learn more about working with the Vonage SMS API.
- 連結とエンコード: メッセージはバイト長に応じて複数の SMS として送信されるかどうかが決まります。
- 国特有の機能: 国別の SMS 送信ルールによってキャンペーンにどのような影響が及ぶでしょうか。
- 送信者 ID: SMS の送信元表示を変更する方法。
- 受信確認: 通信会社に受信確認 (DLR) をリクエストする方法。
- 着信 SMS: Nexmo の仮想番号で SMS を受信する方法。
- SMPP アクセス: REST でなく SMPP を使って SMS 用 API にアクセスします。
- トラブルシューティング: SMS 配信に失敗した場合の対処方法
コードスニペット
ユースケース
- Mobile app invites
- Private SMS communication
- Receiving Concatenated SMS
- SMS Customer Support
- Two-way SMS for customer engagement