SMS Quickstart
This quickstart guides you through sending your first SMS message using Azure Communication Services. You'll learn how to set up your environment, obtain an Azure Communication Services resource, and write a simple application to send an SMS.
Prerequisites
- An Azure account with an active subscription. If you don't have one, create a free account.
- Node.js (version 12 or later) installed.
- An Azure Communication Services resource. You can create one in the Azure portal.
- A phone number associated with your Azure Communication Services resource that can send SMS messages. You can acquire one from the Azure portal under your Communication Services resource.
Steps
-
Set up your development environment
Create a new directory for your project and navigate into it using your terminal. Then, initialize a new Node.js project:
mkdir sms-quickstart cd sms-quickstart npm init -y
Install the Azure Communication Services SMS client library:
npm install @azure/communication-sms
-
Obtain your connection string and phone number
Navigate to your Azure Communication Services resource in the Azure portal. Under "Keys", you'll find your connection string. Note down your phone number that has SMS capabilities.
-
Create your application file
Create a file named
sendSms.js
and add the following code:const { SmsClient } = require("@azure/communication-sms"); // Replace with your actual connection string and phone number const connectionString = "YOUR_AZURE_COMMUNICATION_SERVICES_CONNECTION_STRING"; const fromPhoneNumber = "YOUR_COMMUNICATION_SERVICES_PHONE_NUMBER"; // e.g., "+11234567890" const toPhoneNumber = "+11234567890"; // Replace with the recipient's phone number const messageContent = "Hello from Azure Communication Services!"; async function main() { const smsClient = new SmsClient(connectionString); try { const sendResult = await smsClient.send( { from: fromPhoneNumber, to: [toPhoneNumber], message: messageContent, } ); console.log("SMS sent successfully. Message ID:", sendResult.messageId); } catch (error) { console.error("Error sending SMS:", error); } } main();
Important: Replace
YOUR_AZURE_COMMUNICATION_SERVICES_CONNECTION_STRING
,YOUR_COMMUNICATION_SERVICES_PHONE_NUMBER
, and thetoPhoneNumber
with your actual values. -
Run your application
Execute the script from your terminal:
node sendSms.js
Security Note
Never embed your connection string directly in client-side code. For production applications, use secure methods like environment variables or Azure Key Vault to manage secrets.
Next Steps
- Explore more advanced SMS scenarios like sending to multiple recipients or handling delivery reports.
- Integrate SMS into your existing applications.
- Learn about managing your Azure Communication Services resources.
For more detailed information and API references, please refer to the official SMS documentation.
View More Samples