Skip to main content

SDK examples

NNumbers Messaging uses the official AWS clients. In all of them the rule is the same: the service address as the endpoint override, the br-se-1 region and the access key read from the environment (AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY). Never write the key in code.

Addresses​

ServicePlanned addressClient
Queues (SQS API)https://nqs.msg.nnumbers.com.brSqsClient, boto3.client("sqs"), @aws-sdk/client-sqs, aws sqs
Notifications (SNS API)https://nns.msg.nnumbers.com.brSnsClient, boto3.client("sns"), @aws-sdk/client-sns, aws sns
Email (SES v2 API)https://nes.msg.nnumbers.com.brSesV2Client, boto3.client("sesv2"), @aws-sdk/client-sesv2, aws sesv2
Email (SMTP)smtp.msg.nnumbers.com.br, ports 587 (STARTTLS) and 465 (TLS)Any SMTP client
Planned addresses

These are the addresses decided for the service. The Documentation and examples page of the Messaging module shows the current configuration for your organization and whether each address is already published.

A queue URL is https://nqs.msg.nnumbers.com.br/<account>/<queue> and a topic ARN is arn:nnumbers:sns:br-se-1:<account>:<topic>. Both appear on the queue and topic pages in the Console. In the examples, 123456789012 stands for your organization's virtual account.

Java (AWS SDK for Java v2)​

Dependencies: software.amazon.awssdk:sqs, sns and sesv2.

import java.net.URI;
import software.amazon.awssdk.auth.credentials.EnvironmentVariableCredentialsProvider;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.sesv2.SesV2Client;
import software.amazon.awssdk.services.sns.SnsClient;
import software.amazon.awssdk.services.sqs.SqsClient;
import software.amazon.awssdk.services.sqs.model.*;

Region region = Region.of("br-se-1");
EnvironmentVariableCredentialsProvider creds = EnvironmentVariableCredentialsProvider.create();
SqsClient sqs = SqsClient.builder().endpointOverride(URI.create("https://nqs.msg.nnumbers.com.br"))
.region(region).credentialsProvider(creds).build();
String queueUrl = sqs.getQueueUrl(b -> b.queueName("orders")).queueUrl();
sqs.sendMessage(b -> b.queueUrl(queueUrl).messageBody("order 42 created"));
ReceiveMessageResponse resp = sqs.receiveMessage(b -> b.queueUrl(queueUrl)
.maxNumberOfMessages(10).waitTimeSeconds(20));
for (Message m : resp.messages()) {
System.out.println(m.body());
sqs.deleteMessage(b -> b.queueUrl(queueUrl).receiptHandle(m.receiptHandle()));
}
SnsClient sns = SnsClient.builder().endpointOverride(URI.create("https://nns.msg.nnumbers.com.br"))
.region(region).credentialsProvider(creds).build();
sns.publish(b -> b.topicArn("arn:nnumbers:sns:br-se-1:123456789012:orders-created")
.message("order 42 created"));
SesV2Client ses = SesV2Client.builder().endpointOverride(URI.create("https://nes.msg.nnumbers.com.br"))
.region(region).credentialsProvider(creds).build();
ses.sendEmail(b -> b.fromEmailAddress("no-reply@example.com")
.destination(d -> d.toAddresses("customer@example.com"))
.content(c -> c.simple(msg -> msg
.subject(s -> s.data("Order confirmed"))
.body(body -> body.text(t -> t.data("Your order 42 has been confirmed."))))));

Python (boto3)​

boto3 reads AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY from the environment.

import json
import boto3

sqs = boto3.client("sqs", region_name="br-se-1", endpoint_url="https://nqs.msg.nnumbers.com.br")
queue_url = sqs.get_queue_url(QueueName="orders")["QueueUrl"]
sqs.send_message(QueueUrl=queue_url, MessageBody="order 42 created")
resp = sqs.receive_message(QueueUrl=queue_url, MaxNumberOfMessages=10, WaitTimeSeconds=20)
for msg in resp.get("Messages", []):
print(msg["Body"])
sqs.delete_message(QueueUrl=queue_url, ReceiptHandle=msg["ReceiptHandle"])
sns = boto3.client("sns", region_name="br-se-1", endpoint_url="https://nns.msg.nnumbers.com.br")
sns.publish(
TopicArn="arn:nnumbers:sns:br-se-1:123456789012:orders-created",
Message="order 42 created",
MessageAttributes={"event_type": {"DataType": "String", "StringValue": "order_created"}},
)
ses = boto3.client("sesv2", region_name="br-se-1", endpoint_url="https://nes.msg.nnumbers.com.br")
ses.send_email(
FromEmailAddress="no-reply@example.com",
Destination={"ToAddresses": ["customer@example.com"]},
Content={"Template": {"TemplateName": "order-confirmed",
"TemplateData": json.dumps({"name": "Ana", "order": "42"})}},
)

Node.js (AWS SDK for JavaScript v3)​

Packages: @aws-sdk/client-sqs, @aws-sdk/client-sns and @aws-sdk/client-sesv2. Credentials come from the environment.

import { SQSClient, GetQueueUrlCommand, SendMessageCommand, ReceiveMessageCommand, DeleteMessageCommand } from "@aws-sdk/client-sqs";
import { SNSClient, PublishCommand } from "@aws-sdk/client-sns";
import { SESv2Client, SendEmailCommand } from "@aws-sdk/client-sesv2";

const region = "br-se-1";
const sqs = new SQSClient({ region, endpoint: "https://nqs.msg.nnumbers.com.br" });
const { QueueUrl } = await sqs.send(new GetQueueUrlCommand({ QueueName: "orders" }));
await sqs.send(new SendMessageCommand({ QueueUrl, MessageBody: "order 42 created" }));
const { Messages = [] } = await sqs.send(new ReceiveMessageCommand({ QueueUrl, MaxNumberOfMessages: 10, WaitTimeSeconds: 20 }));
for (const m of Messages) {
console.log(m.Body);
await sqs.send(new DeleteMessageCommand({ QueueUrl, ReceiptHandle: m.ReceiptHandle }));
}
const sns = new SNSClient({ region, endpoint: "https://nns.msg.nnumbers.com.br" });
await sns.send(new PublishCommand({ TopicArn: "arn:nnumbers:sns:br-se-1:123456789012:orders-created", Message: "order 42 created" }));
const ses = new SESv2Client({ region, endpoint: "https://nes.msg.nnumbers.com.br" });
await ses.send(new SendEmailCommand({
FromEmailAddress: "no-reply@example.com",
Destination: { ToAddresses: ["customer@example.com"] },
Content: { Simple: { Subject: { Data: "Order confirmed" }, Body: { Text: { Data: "Your order 42 has been confirmed." } } } },
}));

AWS CLI​

With AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY in the environment, pass --endpoint-url and --region br-se-1 on every command:

aws sqs create-queue --endpoint-url https://nqs.msg.nnumbers.com.br --region br-se-1 \
--queue-name orders
aws sqs send-message --endpoint-url https://nqs.msg.nnumbers.com.br --region br-se-1 \
--queue-url https://nqs.msg.nnumbers.com.br/123456789012/orders --message-body "order 42 created"
aws sns publish --endpoint-url https://nns.msg.nnumbers.com.br --region br-se-1 \
--topic-arn arn:nnumbers:sns:br-se-1:123456789012:orders-created --message "order 42 created"
aws sesv2 send-email --endpoint-url https://nes.msg.nnumbers.com.br --region br-se-1 \
--from-email-address no-reply@example.com \
--destination ToAddresses=customer@example.com \
--content '{"Simple":{"Subject":{"Data":"Order confirmed"},"Body":{"Text":{"Data":"Your order 42 has been confirmed."}}}}'

SMTP​

To send over SMTP, use an SMTP credential (not the access key), created in Email, SMTP credentials:

SettingPlanned value
Serversmtp.msg.nnumbers.com.br
Port587 with STARTTLS (or 465 with TLS from the start of the connection)
Username and passwordThose of the SMTP credential, read from the environment
SenderAn address of a verified identity

In the sandbox, the recipient must also be a verified identity. See Email.