Amazon Pinpoint
Developer Guide

The AWS Documentation website is getting a new look!
Try it now and let us know what you think. Switch to the new look >>

You can return to the original look by selecting English in the language selector above.

Send SMS Messages

You can use the Amazon Pinpoint API to send SMS messages (text messages) to specific phone numbers or endpoint IDs. This section contains complete code examples that you can use to send SMS messages through the Amazon Pinpoint API by using an AWS SDK.

C#JavaJavaScript (Node.js)Python
C#

Use this example to send an SMS message by using the AWS SDK for .NET. This example assumes that you've already installed and configured the AWS SDK for .NET. For more information, see Getting Started in the AWS SDK for .NET Developer Guide.

This example assumes that you're using a shared credentials file to specify the Access Key and Secret Access Key for an existing IAM user. For more information, see Configuring AWS Credentials in the AWS SDK for .NET Developer Guide.

using System; using System.Collections.Generic; using Amazon; using Amazon.Pinpoint; using Amazon.Pinpoint.Model; namespace SendMessage { class MainClass { // The AWS Region that you want to use to send the message. For a list of // AWS Regions where the Amazon Pinpoint API is available, see // https://docs.aws.amazon.com/pinpoint/latest/apireference/ private static readonly string region = "us-east-1"; // The phone number or short code to send the message from. The phone number // or short code that you specify has to be associated with your Amazon Pinpoint // account. For best results, specify long codes in E.164 format. private static readonly string originationNumber = "+12065550199"; // The recipient's phone number. For best results, you should specify the // phone number in E.164 format. private static readonly string destinationNumber = "+14255550142"; // The content of the SMS message. private static readonly string message = "This message was sent through Amazon Pinpoint" + "using the AWS SDK for .NET. Reply STOP to opt out."; // The Pinpoint project/application ID to use when you send this message. // Make sure that the SMS channel is enabled for the project or application // that you choose. private static readonly string appId = "ce796be37f32f178af652b26eexample"; // The type of SMS message that you want to send. If you plan to send // time-sensitive content, specify TRANSACTIONAL. If you plan to send // marketing-related content, specify PROMOTIONAL. private static readonly string messageType = "TRANSACTIONAL"; // The registered keyword associated with the originating short code. private static readonly string registeredKeyword = "myKeyword"; // The sender ID to use when sending the message. Support for sender ID // varies by country or region. For more information, see // https://docs.aws.amazon.com/pinpoint/latest/userguide/channels-sms-countries.html private static readonly string senderId = "mySenderId"; public static void Main(string[] args) { using (AmazonPinpointClient client = new AmazonPinpointClient(RegionEndpoint.GetBySystemName(region))) { SendMessagesRequest sendRequest = new SendMessagesRequest { ApplicationId = appId, MessageRequest = new MessageRequest { Addresses = new Dictionary<string, AddressConfiguration> { { destinationNumber, new AddressConfiguration { ChannelType = "SMS" } } }, MessageConfiguration = new DirectMessageConfiguration { SMSMessage = new SMSMessage { Body = message, MessageType = messageType, OriginationNumber = originationNumber, SenderId = senderId, Keyword = registeredKeyword } } } }; try { Console.WriteLine("Sending message..."); SendMessagesResponse response = client.SendMessages(sendRequest); Console.WriteLine("Message sent!"); } catch (Exception ex) { Console.WriteLine("The message wasn't sent. Error message: " + ex.Message); } } } } }
Java

Use this example to send an SMS message by using the AWS SDK for Java. This example assumes that you've already installed and configured the SDK for Java. For more information, see Getting Started in the AWS SDK for Java Developer Guide.

This example assumes that you're using a shared credentials file to specify the Access Key and Secret Access Key for an existing IAM user. For more information, see Set up AWS Credentials and Region for Development in the AWS SDK for Java Developer Guide.

package com.amazonaws.samples; import java.io.IOException; import java.util.HashMap; import java.util.Map; import com.amazonaws.services.pinpoint.AmazonPinpoint; import com.amazonaws.services.pinpoint.AmazonPinpointClientBuilder; import com.amazonaws.services.pinpoint.model.AddressConfiguration; import com.amazonaws.services.pinpoint.model.ChannelType; import com.amazonaws.services.pinpoint.model.DirectMessageConfiguration; import com.amazonaws.services.pinpoint.model.MessageRequest; import com.amazonaws.services.pinpoint.model.SMSMessage; import com.amazonaws.services.pinpoint.model.SendMessagesRequest; public class SendMessage { // The AWS Region that you want to use to send the message. For a list of // AWS Regions where the Amazon Pinpoint API is available, see // https://docs.aws.amazon.com/pinpoint/latest/apireference/ public static String region = "us-east-1"; // The phone number or short code to send the message from. The phone number // or short code that you specify has to be associated with your Amazon Pinpoint // account. For best results, specify long codes in E.164 format. public static String originationNumber = "+12065550199"; // The recipient's phone number. For best results, you should specify the // phone number in E.164 format. public static String destinationNumber = "+14255550142"; // The content of the SMS message. public static String message = "This message was sent through Amazon Pinpoint " + "using the AWS SDK for Java. Reply STOP to " + "opt out."; // The Pinpoint project/application ID to use when you send this message. // Make sure that the SMS channel is enabled for the project or application // that you choose. public static String appId = "ce796be37f32f178af652b26eexample"; // The type of SMS message that you want to send. If you plan to send // time-sensitive content, specify TRANSACTIONAL. If you plan to send // marketing-related content, specify PROMOTIONAL. public static String messageType = "TRANSACTIONAL"; // The registered keyword associated with the originating short code. public static String registeredKeyword = "myKeyword"; // The sender ID to use when sending the message. Support for sender ID // varies by country or region. For more information, see // https://docs.aws.amazon.com/pinpoint/latest/userguide/channels-sms-countries.html public static String senderId = "MySenderID"; public static void main(String[] args) throws IOException { try { Map<String,AddressConfiguration> addressMap = new HashMap<String,AddressConfiguration>(); addressMap.put(destinationNumber, new AddressConfiguration() .withChannelType(ChannelType.SMS)); AmazonPinpoint client = AmazonPinpointClientBuilder.standard() .withRegion(region).build(); SendMessagesRequest request = new SendMessagesRequest() .withApplicationId(appId) .withMessageRequest(new MessageRequest() .withAddresses(addressMap) .withMessageConfiguration(new DirectMessageConfiguration() .withSMSMessage(new SMSMessage() .withBody(message) .withMessageType(messageType) .withOriginationNumber(originationNumber) .withSenderId(senderId) .withKeyword(registeredKeyword) ) ) ); System.out.println("Sending message..."); client.sendMessages(request); System.out.println("Message sent!"); } catch (Exception ex) { System.out.println("The message wasn't sent. Error message: " + ex.getMessage()); } } }
JavaScript (Node.js)

Use this example to send an SMS message by using the AWS SDK for JavaScript in Node.js. This example assumes that you've already installed and configured the SDK for JavaScript in Node.js. For more information, see Getting Started in the AWS SDK for JavaScript in Node.js Developer Guide.

This example assumes that you're using a shared credentials file to specify the Access Key and Secret Access Key for an existing IAM user. For more information, see Setting Credentials in the AWS SDK for JavaScript in Node.js Developer Guide.

'use strict'; var AWS = require('aws-sdk'); // The AWS Region that you want to use to send the message. For a list of // AWS Regions where the Amazon Pinpoint API is available, see // https://docs.aws.amazon.com/pinpoint/latest/apireference/. var aws_region = "us-east-1"; // The phone number or short code to send the message from. The phone number // or short code that you specify has to be associated with your Amazon Pinpoint // account. For best results, specify long codes in E.164 format. var originationNumber = "+12065550199"; // The recipient's phone number. For best results, you should specify the // phone number in E.164 format. var destinationNumber = "+14255550142"; // The content of the SMS message. var message = "This message was sent through Amazon Pinpoint " + "using the AWS SDK for JavaScript in Node.js. Reply STOP to " + "opt out."; // The Amazon Pinpoint project/application ID to use when you send this message. // Make sure that the SMS channel is enabled for the project or application // that you choose. var applicationId = "ce796be37f32f178af652b26eexample"; // The type of SMS message that you want to send. If you plan to send // time-sensitive content, specify TRANSACTIONAL. If you plan to send // marketing-related content, specify PROMOTIONAL. var messageType = "TRANSACTIONAL"; // The registered keyword associated with the originating short code. var registeredKeyword = "myKeyword"; // The sender ID to use when sending the message. Support for sender ID // varies by country or region. For more information, see // https://docs.aws.amazon.com/pinpoint/latest/userguide/channels-sms-countries.html var senderId = "MySenderID"; // Specify that you're using a shared credentials file, and optionally specify // the profile that you want to use. var credentials = new AWS.SharedIniFileCredentials({profile: 'default'}); AWS.config.credentials = credentials; // Specify the region. AWS.config.update({region:aws_region}); //Create a new Pinpoint object. var pinpoint = new AWS.Pinpoint(); // Specify the parameters to pass to the API. var params = { ApplicationId: applicationId, MessageRequest: { Addresses: { [destinationNumber]: { ChannelType: 'SMS' } }, MessageConfiguration: { SMSMessage: { Body: message, Keyword: registeredKeyword, MessageType: messageType, OriginationNumber: originationNumber, SenderId: senderId, } } } }; //Try to send the message. pinpoint.sendMessages(params, function(err, data) { // If something goes wrong, print an error message. if(err) { console.log(err.message); // Otherwise, show the unique ID for the message. } else { console.log("Message sent! " + data['MessageResponse']['Result'][destinationNumber]['StatusMessage']); } });
Python

Use this example to send an SMS message by using the AWS SDK for Python (Boto 3). This example assumes that you've already installed and configured the SDK for Python. For more information, see Quickstart in AWS SDK for Python (Boto 3) Getting Started.

This example assumes that you're using a shared credentials file to specify the Access Key and Secret Access Key for an existing IAM user. For more information, see Credentials in the AWS SDK for Python (Boto 3) API Reference.

import boto3 from botocore.exceptions import ClientError # The AWS Region that you want to use to send the message. For a list of # AWS Regions where the Amazon Pinpoint API is available, see # https://docs.aws.amazon.com/pinpoint/latest/apireference/ region = "us-east-1" # The phone number or short code to send the message from. The phone number # or short code that you specify has to be associated with your Amazon Pinpoint # account. For best results, specify long codes in E.164 format. originationNumber = "+12065550199" # The recipient's phone number. For best results, you should specify the # phone number in E.164 format. destinationNumber = "+14255550142" # The content of the SMS message. message = ("This is a sample message sent from Amazon Pinpoint by using the " "AWS SDK for Python (Boto 3).") # The Amazon Pinpoint project/application ID to use when you send this message. # Make sure that the SMS channel is enabled for the project or application # that you choose. applicationId = "ce796be37f32f178af652b26eexample" # The type of SMS message that you want to send. If you plan to send # time-sensitive content, specify TRANSACTIONAL. If you plan to send # marketing-related content, specify PROMOTIONAL. messageType = "TRANSACTIONAL" # The registered keyword associated with the originating short code. registeredKeyword = "myKeyword" # The sender ID to use when sending the message. Support for sender ID # varies by country or region. For more information, see # https://docs.aws.amazon.com/pinpoint/latest/userguide/channels-sms-countries.html senderId = "MySenderID" # Create a new client and specify a region. client = boto3.client('pinpoint',region_name=region) try: response = client.send_messages( ApplicationId=applicationId, MessageRequest={ 'Addresses': { destinationNumber: { 'ChannelType': 'SMS' } }, 'MessageConfiguration': { 'SMSMessage': { 'Body': message, 'Keyword': registeredKeyword, 'MessageType': messageType, 'OriginationNumber': originationNumber, 'SenderId': senderId } } } ) except ClientError as e: print(e.response['Error']['Message']) else: print("Message sent! Message ID: " + response['MessageResponse']['Result'][destinationNumber]['MessageId'])