Skip to main content

Packages

PackageServicePurpose
@aws-sdk/client-snsAmazon Simple Notification ServicePublish messages to topics; fan-out to multiple subscribers
@aws-sdk/client-sqsAmazon Simple Queue ServiceSend and receive messages via durable queues

Installation

npm install @aws-sdk/client-sns @aws-sdk/client-sqs

Creating the clients

import { SNSClient } from "@aws-sdk/client-sns";
import { SQSClient } from "@aws-sdk/client-sqs";

const snsClient = new SNSClient({ region: "us-east-1" });
const sqsClient = new SQSClient({ region: "us-east-1" });

SNS commands

import { SNSClient, PublishCommand } from "@aws-sdk/client-sns";

const client = new SNSClient({ region: "us-east-1" });

const response = await client.send(
  new PublishCommand({
    TopicArn: "arn:aws:sns:us-east-1:123456789012:my-topic",
    Message: "Hello from SNS!",
    Subject: "Order confirmation",
    // Optional: structured JSON for per-protocol messages
    // MessageStructure: "json",
    // Message: JSON.stringify({
    //   default: "Hello!",
    //   email: "Hello from email",
    //   sqs: JSON.stringify({ event: "order.created" }),
    // }),
  })
);

console.log(response.MessageId);
To publish to a direct endpoint (SMS or mobile push) instead of a topic, replace TopicArn with TargetArn or PhoneNumber.
import { SNSClient, CreateTopicCommand } from "@aws-sdk/client-sns";

const client = new SNSClient({ region: "us-east-1" });

const response = await client.send(
  new CreateTopicCommand({
    Name: "order-events",
    // Optional: create a FIFO topic
    // Name: "order-events.fifo",
    // Attributes: {
    //   FifoTopic: "true",
    //   ContentBasedDeduplication: "true",
    // },
  })
);

console.log(response.TopicArn);
// "arn:aws:sns:us-east-1:123456789012:order-events"
CreateTopicCommand is idempotent — calling it again with the same name returns the existing topic ARN.
import { SNSClient, SubscribeCommand } from "@aws-sdk/client-sns";

const client = new SNSClient({ region: "us-east-1" });

// Subscribe an SQS queue
const response = await client.send(
  new SubscribeCommand({
    TopicArn: "arn:aws:sns:us-east-1:123456789012:order-events",
    Protocol: "sqs",
    Endpoint: "arn:aws:sqs:us-east-1:123456789012:order-processing-queue",
    Attributes: {
      // Confirm the subscription immediately (requires appropriate IAM policy)
      RawMessageDelivery: "true",
    },
  })
);

// Subscribe an HTTPS endpoint
await client.send(
  new SubscribeCommand({
    TopicArn: "arn:aws:sns:us-east-1:123456789012:order-events",
    Protocol: "https",
    Endpoint: "https://example.com/webhooks/sns",
  })
);

// Subscribe an email address
await client.send(
  new SubscribeCommand({
    TopicArn: "arn:aws:sns:us-east-1:123456789012:order-events",
    Protocol: "email",
    Endpoint: "alerts@example.com",
  })
);

console.log(response.SubscriptionArn);
Supported protocols include sqs, https, http, email, email-json, sms, lambda, and application.
import { SNSClient, ListTopicsCommand } from "@aws-sdk/client-sns";

const client = new SNSClient({ region: "us-east-1" });

const response = await client.send(new ListTopicsCommand({}));

for (const topic of response.Topics ?? []) {
  console.log(topic.TopicArn);
}

// Paginate if NextToken is present
if (response.NextToken) {
  const nextPage = await client.send(
    new ListTopicsCommand({ NextToken: response.NextToken })
  );
  console.log(nextPage.Topics);
}

Fan-out pattern: SNS to SQS

A common pattern is to publish a single SNS message and deliver it to multiple SQS queues, decoupling producers from consumers:
import { SNSClient, PublishCommand } from "@aws-sdk/client-sns";

const snsClient = new SNSClient({ region: "us-east-1" });

// Publish once — SNS fans out to all subscribed SQS queues
await snsClient.send(
  new PublishCommand({
    TopicArn: "arn:aws:sns:us-east-1:123456789012:order-events",
    Message: JSON.stringify({
      event: "order.created",
      orderId: "order-789",
      total: 99.99,
    }),
  })
);
Each subscribed SQS queue receives a copy of the message independently.

Build docs developers (and LLMs) love