Amazon SNS examples using SDK for JavaScript V3 - Amazon SDK for JavaScript
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China.

The Amazon SDK for JavaScript V3 API Reference Guide describes in detail all the API operations for the Amazon SDK for JavaScript version 3 (V3).

Starting October 1, 2022, Amazon SDK for JavaScript (v3) will end support for Internet Explorer 11 (IE 11). For details, see Announcing the end of support for Internet Explorer 11 in the Amazon SDK for JavaScript (v3).

Amazon SNS examples using SDK for JavaScript V3

The following code examples show you how to perform actions and implement common scenarios by using the Amazon SDK for JavaScript V3 with Amazon SNS.

Actions are code excerpts that show you how to call individual Amazon SNS functions.

Scenarios are code examples that show you how to accomplish a specific task by calling multiple Amazon SNS functions.

Each example includes a link to GitHub, where you can find instructions on how to set up and run the code in context.

Topics

Actions

The following code example shows how to check whether a phone number is opted out of receiving Amazon SNS messages.

SDK for JavaScript V3
Tip

To learn how to set up and run this example, see GitHub.

Create the client in a separate module and export it.

import { SNSClient } from "@aws-sdk/client-sns"; // Set the AWS Region. const REGION = "REGION"; //e.g. "us-east-1" // Create SNS service object. const snsClient = new SNSClient({ region: REGION }); export { snsClient };

Import the SDK and client modules and call the API.

// Import required AWS SDK clients and commands for Node.js import {CheckIfPhoneNumberIsOptedOutCommand } from "@aws-sdk/client-sns"; import {snsClient } from "./libs/snsClient.js"; // Set the parameters const params = { phoneNumber: "353861230764" }; //PHONE_NUMBER, in the E.164 phone number structure const run = async () => { try { const data = await snsClient.send( new CheckIfPhoneNumberIsOptedOutCommand(params) ); console.log("Success.", data); return data; // For unit tests. } catch (err) { console.log("Error", err.stack); } }; run();

The following code example shows how to confirm the owner of an endpoint wants to receive Amazon SNS messages by validating the token sent to the endpoint by an earlier Subscribe action.

SDK for JavaScript V3
Tip

To learn how to set up and run this example, see GitHub.

Create the client in a separate module and export it.

import { SNSClient } from "@aws-sdk/client-sns"; // Set the AWS Region. const REGION = "REGION"; //e.g. "us-east-1" // Create SNS service object. const snsClient = new SNSClient({ region: REGION }); export { snsClient };

Import the SDK and client modules and call the API.

// Import required AWS SDK clients and commands for Node.js import {ConfirmSubscriptionCommand } from "@aws-sdk/client-sns"; import {snsClient } from "./libs/snsClient.js"; // Set the parameters const params = { Token: "TOKEN", // Required. Token sent to the endpoint by an earlier Subscribe action. */ TopicArn: "TOPIC_ARN", // Required AuthenticateOnUnsubscribe: "true", // 'true' or 'false' }; const run = async () => { try { const data = await snsClient.send(new ConfirmSubscriptionCommand(params)); console.log("Success.", data); return data; // For unit tests. } catch (err) { console.log("Error", err.stack); } }; run();

The following code example shows how to create an Amazon SNS topic.

SDK for JavaScript V3
Tip

To learn how to set up and run this example, see GitHub.

Create the client in a separate module and export it.

import { SNSClient } from "@aws-sdk/client-sns"; // Set the AWS Region. const REGION = "REGION"; //e.g. "us-east-1" // Create SNS service object. const snsClient = new SNSClient({ region: REGION }); export { snsClient };

Import the SDK and client modules and call the API.

// Import required AWS SDK clients and commands for Node.js import {CreateTopicCommand } from "@aws-sdk/client-sns"; import {snsClient } from "./libs/snsClient.js"; // Set the parameters const params = { Name: "TOPIC_NAME" }; //TOPIC_NAME const run = async () => { try { const data = await snsClient.send(new CreateTopicCommand(params)); console.log("Success.", data); return data; // For unit tests. } catch (err) { console.log("Error", err.stack); } }; run();

The following code example shows how to delete an Amazon SNS subscription.

SDK for JavaScript V3
Tip

To learn how to set up and run this example, see GitHub.

Create the client in a separate module and export it.

import { SNSClient } from "@aws-sdk/client-sns"; // Set the AWS Region. const REGION = "REGION"; //e.g. "us-east-1" // Create SNS service object. const snsClient = new SNSClient({ region: REGION }); export { snsClient };

Import the SDK and client modules and call the API.

// Import required AWS SDK clients and commands for Node.js import {UnsubscribeCommand } from "@aws-sdk/client-sns"; import {snsClient } from "./libs/snsClient.js"; // Set the parameters const params = { SubscriptionArn: "TOPIC_SUBSCRIPTION_ARN" }; //TOPIC_SUBSCRIPTION_ARN const run = async () => { try { const data = await snsClient.send(new UnsubscribeCommand(params)); console.log("Success.", data); return data; // For unit tests. } catch (err) { console.log("Error", err.stack); } }; run();

The following code example shows how to delete an Amazon SNS topic and all subscriptions to that topic.

SDK for JavaScript V3
Tip

To learn how to set up and run this example, see GitHub.

Create the client in a separate module and export it.

import { SNSClient } from "@aws-sdk/client-sns"; // Set the AWS Region. const REGION = "REGION"; //e.g. "us-east-1" // Create SNS service object. const snsClient = new SNSClient({ region: REGION }); export { snsClient };

Import the SDK and client modules and call the API.

// Load the AWS SDK for Node.js // Import required AWS SDK clients and commands for Node.js import {DeleteTopicCommand } from "@aws-sdk/client-sns"; import {snsClient } from "./libs/snsClient.js"; // Set the parameters const params = { TopicArn: "TOPIC_ARN" }; //TOPIC_ARN const run = async () => { try { const data = await snsClient.send(new DeleteTopicCommand(params)); console.log("Success.", data); return data; // For unit tests. } catch (err) { console.log("Error", err.stack); } }; run();

The following code example shows how to get the properties of an Amazon SNS topic.

SDK for JavaScript V3
Tip

To learn how to set up and run this example, see GitHub.

Create the client in a separate module and export it.

import { SNSClient } from "@aws-sdk/client-sns"; // Set the AWS Region. const REGION = "REGION"; //e.g. "us-east-1" // Create SNS service object. const snsClient = new SNSClient({ region: REGION }); export { snsClient };

Import the SDK and client modules and call the API.

// Import required AWS SDK clients and commands for Node.js import {GetTopicAttributesCommand } from "@aws-sdk/client-sns"; import {snsClient } from "./libs/snsClient.js"; // Set the parameters const params = { TopicArn: "TOPIC_ARN" }; // TOPIC_ARN const run = async () => { try { const data = await snsClient.send(new GetTopicAttributesCommand(params)); console.log("Success.", data); return data; // For unit tests. } catch (err) { console.log("Error", err.stack); } }; run();
SDK for JavaScript V2
Tip

To learn how to set up and run this example, see GitHub.

Import the SDK and client modules and call the API.

// Load the AWS SDK for Node.js var AWS = require('aws-sdk'); // Set region AWS.config.update({region: 'REGION'}); // Create promise and SNS service object var getTopicAttribsPromise = new AWS.SNS({apiVersion: '2010-03-31'}).getTopicAttributes({TopicArn: 'TOPIC_ARN'}).promise(); // Handle promise's fulfilled/rejected states getTopicAttribsPromise.then( function(data) { console.log(data); }).catch( function(err) { console.error(err, err.stack); });

The following code example shows how to get the settings for sending Amazon SNS SMS messages.

SDK for JavaScript V3
Tip

To learn how to set up and run this example, see GitHub.

Create the client in a separate module and export it.

import { SNSClient } from "@aws-sdk/client-sns"; // Set the AWS Region. const REGION = "REGION"; //e.g. "us-east-1" // Create SNS service object. const snsClient = new SNSClient({ region: REGION }); export { snsClient };

Import the SDK and client modules and call the API.

// Import required AWS SDK clients and commands for Node.js import {GetSMSAttributesCommand } from "@aws-sdk/client-sns"; import {snsClient } from "./libs/snsClient.js"; // Set the parameters var params = { attributes: [ "DefaultSMSType", "ATTRIBUTE_NAME", /* more items */ ], }; const run = async () => { try { const data = await snsClient.send(new GetSMSAttributesCommand(params)); console.log("Success.", data); return data; // For unit tests. } catch (err) { console.log("Error", err.stack); } }; run();

The following code example shows how to retrieve the list of subscribers of an Amazon SNS topic.

SDK for JavaScript V3
Tip

To learn how to set up and run this example, see GitHub.

Create the client in a separate module and export it.

import { SNSClient } from "@aws-sdk/client-sns"; // Set the AWS Region. const REGION = "REGION"; //e.g. "us-east-1" // Create SNS service object. const snsClient = new SNSClient({ region: REGION }); export { snsClient };

Import the SDK and client modules and call the API.

// Import required AWS SDK clients and commands for Node.js import {ListSubscriptionsByTopicCommand } from "@aws-sdk/client-sns"; import {snsClient } from "./libs/snsClient.js"; // Set the parameters const params = { TopicArn: "TOPIC_ARN" }; //TOPIC_ARN const run = async () => { try { const data = await snsClient.send(new ListSubscriptionsByTopicCommand(params)); console.log("Success.", data); return data; // For unit tests. } catch (err) { console.log("Error", err.stack); } }; run();

The following code example shows how to list Amazon SNS topics.

SDK for JavaScript V3
Tip

To learn how to set up and run this example, see GitHub.

Create the client in a separate module and export it.

import { SNSClient } from "@aws-sdk/client-sns"; // Set the AWS Region. const REGION = "REGION"; //e.g. "us-east-1" // Create SNS service object. const snsClient = new SNSClient({ region: REGION }); export { snsClient };

Import the SDK and client modules and call the API.

// Import required AWS SDK clients and commands for Node.js import {ListTopicsCommand } from "@aws-sdk/client-sns"; import {snsClient } from "./libs/snsClient.js"; const run = async () => { try { const data = await snsClient.send(new ListTopicsCommand({})); console.log("Success.", data); return data; // For unit tests. } catch (err) { console.log("Error", err.stack); } }; run();

The following code example shows how to publish messages to an Amazon SNS topic.

SDK for JavaScript V3
Tip

To learn how to set up and run this example, see GitHub.

Create the client in a separate module and export it.

import { SNSClient } from "@aws-sdk/client-sns"; // Set the AWS Region. const REGION = "REGION"; //e.g. "us-east-1" // Create SNS service object. const snsClient = new SNSClient({ region: REGION }); export { snsClient };

Import the SDK and client modules and call the API.

// Import required AWS SDK clients and commands for Node.js import {PublishCommand } from "@aws-sdk/client-sns"; import {snsClient } from "./libs/snsClient.js"; // Set the parameters var params = { Message: "MESSAGE_TEXT", // MESSAGE_TEXT TopicArn: "TOPIC_ARN", //TOPIC_ARN }; const run = async () => { try { const data = await snsClient.send(new PublishCommand(params)); console.log("Success.", data); return data; // For unit tests. } catch (err) { console.log("Error", err.stack); } }; run();

The following code example shows how to set the default settings for sending SMS messages using Amazon SNS.

SDK for JavaScript V3
Tip

To learn how to set up and run this example, see GitHub.

Create the client in a separate module and export it.

import { SNSClient } from "@aws-sdk/client-sns"; // Set the AWS Region. const REGION = "REGION"; //e.g. "us-east-1" // Create SNS service object. const snsClient = new SNSClient({ region: REGION }); export { snsClient };

Import the SDK and client modules and call the API.

// Import required AWS SDK clients and commands for Node.js import {SetSMSAttributesCommand } from "@aws-sdk/client-sns"; import {snsClient } from "./libs/snsClient.js"; // Set the parameters const params = { attributes: { /* required */ DefaultSMSType: "Transactional" /* highest reliability */, //'DefaultSMSType': 'Promotional' /* lowest cost */ }, }; const run = async () => { try { const data = await snsClient.send(new SetSMSAttributesCommand(params)); console.log("Success.", data); return data; // For unit tests. } catch (err) { console.log("Error", err.stack); } }; run();

The following code example shows how to set Amazon SNS topic attributes.

SDK for JavaScript V3
Tip

To learn how to set up and run this example, see GitHub.

Create the client in a separate module and export it.

import { SNSClient } from "@aws-sdk/client-sns"; // Set the AWS Region. const REGION = "REGION"; //e.g. "us-east-1" // Create SNS service object. const snsClient = new SNSClient({ region: REGION }); export { snsClient };

Import the SDK and client modules and call the API.

// Import required AWS SDK clients and commands for Node.js import {SetTopicAttributesCommand } from "@aws-sdk/client-sns"; import {snsClient } from "./libs/snsClient.js"; // Set the parameters const params = { AttributeName: "ATTRIBUTE_NAME", // ATTRIBUTE_NAME TopicArn: "TOPIC_ARN", // TOPIC_ARN AttributeValue: "NEW_ATTRIBUTE_VALUE", //NEW_ATTRIBUTE_VALUE }; const run = async () => { try { const data = await snsClient.send(new SetTopicAttributesCommand(params)); console.log("Success.", data); return data; // For unit tests. } catch (err) { console.log("Error", err.stack); } }; run();

The following code example shows how to subscribe a Lambda function so it receives notifications from an Amazon SNS topic.

SDK for JavaScript V3
Tip

To learn how to set up and run this example, see GitHub.

Create the client in a separate module and export it.

import { SNSClient } from "@aws-sdk/client-sns"; // Set the AWS Region. const REGION = "REGION"; //e.g. "us-east-1" // Create SNS service object. const snsClient = new SNSClient({ region: REGION }); export { snsClient };

Import the SDK and client modules and call the API.

// Import required AWS SDK clients and commands for Node.js import {SubscribeCommand } from "@aws-sdk/client-sns"; import {snsClient } from "./libs/snsClient.js"; // Set the parameters const params = { Protocol: "lambda" /* required */, TopicArn: "TOPIC_ARN", //TOPIC_ARN Endpoint: "LAMBDA_FUNCTION_ARN", //LAMBDA_FUNCTION_ARN }; const run = async () => { try { const data = await snsClient.send(new SubscribeCommand(params)); console.log("Success.", data); return data; // For unit tests. } catch (err) { console.log("Error", err.stack); } }; run();

The following code example shows how to subscribe a mobile application endpoint so it receives notifications from an Amazon SNS topic.

SDK for JavaScript V3
Tip

To learn how to set up and run this example, see GitHub.

Create the client in a separate module and export it.

import { SNSClient } from "@aws-sdk/client-sns"; // Set the AWS Region. const REGION = "REGION"; //e.g. "us-east-1" // Create SNS service object. const snsClient = new SNSClient({ region: REGION }); export { snsClient };

Import the SDK and client modules and call the API.

// Import required AWS SDK clients and commands for Node.js import {SubscribeCommand } from "@aws-sdk/client-sns"; import {snsClient } from "./libs/snsClient.js"; // Set the parameters const params = { Protocol: "application" /* required */, TopicArn: "TOPIC_ARN", //TOPIC_ARN Endpoint: "MOBILE_ENDPOINT_ARN", // MOBILE_ENDPOINT_ARN }; const run = async () => { try { const data = await snsClient.send(new SubscribeCommand(params)); console.log("Success.", data); return data; // For unit tests. } catch (err) { console.log("Error", err.stack); } }; run();

The following code example shows how to subscribe an email address to an Amazon SNS topic.

SDK for JavaScript V3
Tip

To learn how to set up and run this example, see GitHub.

Create the client in a separate module and export it.

import { SNSClient } from "@aws-sdk/client-sns"; // Set the AWS Region. const REGION = "REGION"; //e.g. "us-east-1" // Create SNS service object. const snsClient = new SNSClient({ region: REGION }); export { snsClient };

Import the SDK and client modules and call the API.

// Import required AWS SDK clients and commands for Node.js import {SubscribeCommand } from "@aws-sdk/client-sns"; import {snsClient } from "./libs/snsClient.js"; // Set the parameters const params = { Protocol: "email" /* required */, TopicArn: "TOPIC_ARN", //TOPIC_ARN Endpoint: "EMAIL_ADDRESS", //EMAIL_ADDRESS }; const run = async () => { try { const data = await snsClient.send(new SubscribeCommand(params)); console.log("Success.", data); return data; // For unit tests. } catch (err) { console.log("Error", err.stack); } }; run();