Amazon SQS 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 SQS 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 SQS.

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

Scenarios are code examples that show you how to accomplish a specific task by calling multiple Amazon SQS 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 change how long an Amazon SQS queue waits for a message to arrive.

SDK for JavaScript V3
Tip

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

Create the client.

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

Change how long an Amazon SQS queue waits for a message to arrive.

// Import required AWS SDK clients and commands for Node.js import { SetQueueAttributesCommand } from "@aws-sdk/client-sqs"; import { sqsClient } from "./libs/sqsClient.js"; // Set the parameters const params = { Attributes: { ReceiveMessageWaitTimeSeconds: "20", }, QueueUrl: "SQS_QUEUE_URL", //SQS_QUEUE_URL; e.g., 'https://sqs.REGION.amazonaws.com/ACCOUNT-ID/QUEUE-NAME' }; const run = async () => { try { const data = await sqsClient.send(new SetQueueAttributesCommand(params)); console.log("Success", data); return data; // For unit tests. } catch (err) { console.error(err, err.stack); } }; run();
SDK for JavaScript V2
Tip

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

Change how long an Amazon SQS queue waits for a message to arrive.

// Load the AWS SDK for Node.js var AWS = require('aws-sdk'); // Set the region AWS.config.update({region: 'REGION'}); // Create the SQS service object var sqs = new AWS.SQS({apiVersion: '2012-11-05'}); var params = { Attributes: { "ReceiveMessageWaitTimeSeconds": "20", }, QueueUrl: "SQS_QUEUE_URL" }; sqs.setQueueAttributes(params, function(err, data) { if (err) { console.log("Error", err); } else { console.log("Success", data); } });

The following code example shows how to change an Amazon SQS message timeout visibility.

SDK for JavaScript V3
Tip

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

Create the client.

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

Receive an Amazon SQS message and change its timeout visibility.

// Import required AWS SDK clients and commands for Node.js import { ReceiveMessageCommand, ChangeMessageVisibilityCommand, } from "@aws-sdk/client-sqs"; import { sqsClient } from "./libs/sqsClient.js"; // Set the parameters const queueURL = "https://sqs.REGION.amazonaws.com/ACCOUNT-ID/QUEUE-NAME"; // REGION, ACCOUNT_ID, QUEUE_NAME const params = { AttributeNames: ["SentTimestamp"], MaxNumberOfMessages: 1, MessageAttributeNames: ["All"], QueueUrl: queueURL, }; const run = async () => { try { const data = await sqsClient.send(new ReceiveMessageCommand(params)); if (data.Messages != null) { try { const visibilityParams = { QueueUrl: queueURL, ReceiptHandle: data.Messages[0].ReceiptHandle, VisibilityTimeout: 20, // 20 second timeout }; const results = await sqsClient.send( new ChangeMessageVisibilityCommand(visibilityParams) ); console.log("Timeout Changed", results); } catch (err) { console.log("Delete Error", err); } } else { console.log("No messages to change"); } return data; // For unit tests. } catch (err) { console.log("Receive Error", err); } }; run();
SDK for JavaScript V2
Tip

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

Receive an Amazon SQS message and change its timeout visibility.

// Load the AWS SDK for Node.js var AWS = require('aws-sdk') // Set the region to us-west-2 AWS.config.update({ region: 'us-west-2' }) // Create the SQS service object var sqs = new AWS.SQS({ apiVersion: '2012-11-05' }) var queueURL = 'https://sqs.REGION.amazonaws.com/ACCOUNT-ID/QUEUE-NAME' var params = { AttributeNames: ['SentTimestamp'], MaxNumberOfMessages: 1, MessageAttributeNames: ['All'], QueueUrl: queueURL } sqs.receiveMessage(params, function (err, data) { if (err) { console.log('Receive Error', err) } else { // Make sure we have a message if (data.Messages != null) { var visibilityParams = { QueueUrl: queueURL, ReceiptHandle: data.Messages[0].ReceiptHandle, VisibilityTimeout: 20 // 20 second timeout } sqs.changeMessageVisibility(visibilityParams, function (err, data) { if (err) { console.log('Delete Error', err) } else { console.log('Timeout Changed', data) } }) } else { console.log('No messages to change') } } })

The following code example shows how to create an Amazon SQS queue.

SDK for JavaScript V3
Tip

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

Create the client.

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

Create an Amazon SQS standard queue.

// Import required AWS SDK clients and commands for Node.js import { CreateQueueCommand } from "@aws-sdk/client-sqs"; import { sqsClient } from "./libs/sqsClient.js"; // Set the parameters const params = { QueueName: "SQS_QUEUE_NAME", //SQS_QUEUE_URL Attributes: { DelaySeconds: "60", // Number of seconds delay. MessageRetentionPeriod: "86400", // Number of seconds delay. }, }; const run = async () => { try { const data = await sqsClient.send(new CreateQueueCommand(params)); console.log("Success", data); return data; // For unit tests. } catch (err) { console.log("Error", err); } }; run();

Create an Amazon SQS queue that waits for a message to arrive.

// Import required AWS SDK clients and commands for Node.js import { CreateQueueCommand } from "@aws-sdk/client-sqs"; import { sqsClient } from "./libs/sqsClient.js"; // Set the parameters const params = { QueueName: "SQS_QUEUE_NAME", //SQS_QUEUE_URL; e.g., 'https://sqs.REGION.amazonaws.com/ACCOUNT-ID/QUEUE-NAME' Attributes: { ReceiveMessageWaitTimeSeconds: "20", }, }; const run = async () => { try { const data = await sqsClient.send(new CreateQueueCommand(params)); console.log("Success", data); return data; // For unit tests. } catch (err) { console.error(err, err.stack); } }; run();
SDK for JavaScript V2
Tip

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

Create an Amazon SQS standard queue.

// Load the AWS SDK for Node.js var AWS = require('aws-sdk'); // Set the region AWS.config.update({region: 'REGION'}); // Create an SQS service object var sqs = new AWS.SQS({apiVersion: '2012-11-05'}); var params = { QueueName: 'SQS_QUEUE_NAME', Attributes: { 'DelaySeconds': '60', 'MessageRetentionPeriod': '86400' } }; sqs.createQueue(params, function(err, data) { if (err) { console.log("Error", err); } else { console.log("Success", data.QueueUrl); } });

Create an Amazon SQS queue that waits for a message to arrive.

// Load the AWS SDK for Node.js var AWS = require('aws-sdk'); // Set the region AWS.config.update({region: 'REGION'}); // Create the SQS service object var sqs = new AWS.SQS({apiVersion: '2012-11-05'}); var params = { QueueName: 'SQS_QUEUE_NAME', Attributes: { 'ReceiveMessageWaitTimeSeconds': '20', } }; sqs.createQueue(params, function(err, data) { if (err) { console.log("Error", err); } else { console.log("Success", data.QueueUrl); } });

The following code example shows how to delete a message from an Amazon SQS queue.

SDK for JavaScript V3
Tip

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

Create the client.

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

Receive and delete Amazon SQS messages.

// Import required AWS SDK clients and commands for Node.js import { ReceiveMessageCommand, DeleteMessageCommand, } from "@aws-sdk/client-sqs"; import { sqsClient } from "./libs/sqsClient.js"; // Set the parameters const queueURL = "SQS_QUEUE_URL"; //SQS_QUEUE_URL; e.g., 'https://sqs.REGION.amazonaws.com/ACCOUNT-ID/QUEUE-NAME' const params = { AttributeNames: ["SentTimestamp"], MaxNumberOfMessages: 10, MessageAttributeNames: ["All"], QueueUrl: queueURL, VisibilityTimeout: 20, WaitTimeSeconds: 0, }; const run = async () => { try { const data = await sqsClient.send(new ReceiveMessageCommand(params)); if (data.Messages) { var deleteParams = { QueueUrl: queueURL, ReceiptHandle: data.Messages[0].ReceiptHandle, }; try { const data = await sqsClient.send(new DeleteMessageCommand(deleteParams)); console.log("Message deleted", data); } catch (err) { console.log("Error", err); } } else { console.log("No messages to delete"); } return data; // For unit tests. } catch (err) { console.log("Receive Error", err); } }; run();
SDK for JavaScript V2
Tip

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

Receive and delete Amazon SQS messages.

// Load the AWS SDK for Node.js var AWS = require('aws-sdk'); // Set the region AWS.config.update({region: 'REGION'}); // Create an SQS service object var sqs = new AWS.SQS({apiVersion: '2012-11-05'}); var queueURL = "SQS_QUEUE_URL"; var params = { AttributeNames: [ "SentTimestamp" ], MaxNumberOfMessages: 10, MessageAttributeNames: [ "All" ], QueueUrl: queueURL, VisibilityTimeout: 20, WaitTimeSeconds: 0 }; sqs.receiveMessage(params, function(err, data) { if (err) { console.log("Receive Error", err); } else if (data.Messages) { var deleteParams = { QueueUrl: queueURL, ReceiptHandle: data.Messages[0].ReceiptHandle }; sqs.deleteMessage(deleteParams, function(err, data) { if (err) { console.log("Delete Error", err); } else { console.log("Message Deleted", data); } }); } });

The following code example shows how to delete an Amazon SQS queue.

SDK for JavaScript V3
Tip

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

Create the client.

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

Delete an Amazon SQS queue.

// Import required AWS SDK clients and commands for Node.js import { DeleteQueueCommand } from "@aws-sdk/client-sqs"; import { sqsClient } from "./libs/sqsClient.js"; // Set the parameters const params = { QueueUrl: "SQS_QUEUE_URL" }; //SQS_QUEUE_URL e.g., 'https://sqs.REGION.amazonaws.com/ACCOUNT-ID/QUEUE-NAME' const run = async () => { try { const data = await sqsClient.send(new DeleteQueueCommand(params)); console.log("Success", data); return data; // For unit tests. } catch (err) { console.error(err, err.stack); } }; run();
SDK for JavaScript V2
Tip

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

Delete an Amazon SQS queue.

// Load the AWS SDK for Node.js var AWS = require('aws-sdk'); // Set the region AWS.config.update({region: 'REGION'}); // Create an SQS service object var sqs = new AWS.SQS({apiVersion: '2012-11-05'}); var params = { QueueUrl: 'SQS_QUEUE_URL' }; sqs.deleteQueue(params, function(err, data) { if (err) { console.log("Error", err); } else { console.log("Success", data); } });

The following code example shows how to get the URL of an Amazon SQS queue.

SDK for JavaScript V3
Tip

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

Create the client.

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

Get the URL for an Amazon SQS queue.

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

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

Get the URL for an Amazon SQS queue.

// Load the AWS SDK for Node.js var AWS = require('aws-sdk'); // Set the region AWS.config.update({region: 'REGION'}); // Create an SQS service object var sqs = new AWS.SQS({apiVersion: '2012-11-05'}); var params = { QueueName: 'SQS_QUEUE_NAME' }; sqs.getQueueUrl(params, function(err, data) { if (err) { console.log("Error", err); } else { console.log("Success", data.QueueUrl); } });

The following code example shows how to list Amazon SQS queues.

SDK for JavaScript V3
Tip

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

Create the client.

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

List your Amazon SQS queues.

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

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

List your Amazon SQS queues.

// Load the AWS SDK for Node.js var AWS = require('aws-sdk'); // Set the region AWS.config.update({region: 'REGION'}); // Create an SQS service object var sqs = new AWS.SQS({apiVersion: '2012-11-05'}); var params = {}; sqs.listQueues(params, function(err, data) { if (err) { console.log("Error", err); } else { console.log("Success", data.QueueUrls); } });

The following code example shows how to receive messages from an Amazon SQS queue.

SDK for JavaScript V3
Tip

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

Create the client.

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

Receive a message from an Amazon SQS queue using long-poll support.

// Import required AWS SDK clients and commands for Node.js import { ReceiveMessageCommand } from "@aws-sdk/client-sqs"; import { sqsClient } from "./libs/sqsClient.js"; // Set the parameters const queueURL = "SQS_QUEUE_URL"; // SQS_QUEUE_URL const params = { AttributeNames: ["SentTimestamp"], MaxNumberOfMessages: 1, MessageAttributeNames: ["All"], QueueUrl: queueURL, WaitTimeSeconds: 20, }; const run = async () => { try { const data = await sqsClient.send(new ReceiveMessageCommand(params)); console.log("Success, ", data); return data; // For unit tests. } catch (err) { console.log("Error", err); } }; run();
SDK for JavaScript V2
Tip

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

Receive a message from an Amazon SQS queue using long-poll support.

// Load the AWS SDK for Node.js var AWS = require('aws-sdk'); // Set the region AWS.config.update({region: 'REGION'}); // Create the SQS service object var sqs = new AWS.SQS({apiVersion: '2012-11-05'}); var queueURL = "SQS_QUEUE_URL"; var params = { AttributeNames: [ "SentTimestamp" ], MaxNumberOfMessages: 1, MessageAttributeNames: [ "All" ], QueueUrl: queueURL, WaitTimeSeconds: 20 }; sqs.receiveMessage(params, function(err, data) { if (err) { console.log("Error", err); } else { console.log("Success", data); } });

The following code example shows how to send a message to an Amazon SQS queue.

SDK for JavaScript V3
Tip

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

Create the client.

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

Send a message to an Amazon SQS queue.

// Import required AWS SDK clients and commands for Node.js import { SendMessageCommand } from "@aws-sdk/client-sqs"; import { sqsClient } from "./libs/sqsClient.js"; // Set the parameters const params = { DelaySeconds: 10, MessageAttributes: { Title: { DataType: "String", StringValue: "The Whistler", }, Author: { DataType: "String", StringValue: "John Grisham", }, WeeksOn: { DataType: "Number", StringValue: "6", }, }, MessageBody: "Information about current NY Times fiction bestseller for week of 12/11/2016.", // MessageDeduplicationId: "TheWhistler", // Required for FIFO queues // MessageGroupId: "Group1", // Required for FIFO queues QueueUrl: "SQS_QUEUE_URL" //SQS_QUEUE_URL; e.g., 'https://sqs.REGION.amazonaws.com/ACCOUNT-ID/QUEUE-NAME' }; const run = async () => { try { const data = await sqsClient.send(new SendMessageCommand(params)); console.log("Success, message sent. MessageID:", data.MessageId); return data; // For unit tests. } catch (err) { console.log("Error", err); } }; run();
SDK for JavaScript V2
Tip

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

Send a message to an Amazon SQS queue.

// Load the AWS SDK for Node.js var AWS = require('aws-sdk'); // Set the region AWS.config.update({region: 'REGION'}); // Create an SQS service object var sqs = new AWS.SQS({apiVersion: '2012-11-05'}); var params = { // Remove DelaySeconds parameter and value for FIFO queues DelaySeconds: 10, MessageAttributes: { "Title": { DataType: "String", StringValue: "The Whistler" }, "Author": { DataType: "String", StringValue: "John Grisham" }, "WeeksOn": { DataType: "Number", StringValue: "6" } }, MessageBody: "Information about current NY Times fiction bestseller for week of 12/11/2016.", // MessageDeduplicationId: "TheWhistler", // Required for FIFO queues // MessageGroupId: "Group1", // Required for FIFO queues QueueUrl: "SQS_QUEUE_URL" }; sqs.sendMessage(params, function(err, data) { if (err) { console.log("Error", err); } else { console.log("Success", data.MessageId); } });