AWS SDK for JavaScript SQS Client provides comprehensive access to Amazon Simple Queue Service (SQS) for Node.js, Browser and React Native applications. It offers a modern, promise-based API for all SQS operations including message sending/receiving, queue management, batch operations, and advanced features like dead letter queues and message move tasks.
npm install @aws-sdk/client-sqsimport { SQSClient, SendMessageCommand, ReceiveMessageCommand } from "@aws-sdk/client-sqs";For CommonJS:
const { SQSClient, SendMessageCommand, ReceiveMessageCommand } = require("@aws-sdk/client-sqs");V2 Compatible (larger bundle):
import * as AWS from "@aws-sdk/client-sqs";
const client = new AWS.SQS({ region: "us-east-1" });import { SQSClient, SendMessageCommand, ReceiveMessageCommand } from "@aws-sdk/client-sqs";
// Initialize client
const client = new SQSClient({ region: "us-east-1" });
// Send a message
const sendResult = await client.send(new SendMessageCommand({
QueueUrl: "https://sqs.us-east-1.amazonaws.com/123456789012/MyQueue",
MessageBody: "Hello, SQS!",
MessageAttributes: {
Author: {
StringValue: "myapp",
DataType: "String"
}
}
}));
// Receive messages
const receiveResult = await client.send(new ReceiveMessageCommand({
QueueUrl: "https://sqs.us-east-1.amazonaws.com/123456789012/MyQueue",
MaxNumberOfMessages: 10,
WaitTimeSeconds: 20
}));
// Process received messages
for (const message of receiveResult.Messages || []) {
console.log("Message:", message.Body);
// Delete message after processing
await client.send(new DeleteMessageCommand({
QueueUrl: "https://sqs.us-east-1.amazonaws.com/123456789012/MyQueue",
ReceiptHandle: message.ReceiptHandle
}));
}The AWS SQS Client is built around several key components:
SendMessageCommand, ReceiveMessageCommand, etc.)SQSClient for modern v3 API, SQS for v2-compatible interfaceCore client setup and configuration options for authentication, regions, and custom settings.
class SQSClient {
constructor(configuration: SQSClientConfig);
send<InputType, OutputType>(
command: Command<InputType, OutputType, SQSClientResolvedConfig>
): Promise<OutputType>;
destroy(): void;
}
interface SQSClientConfig {
region?: string | Provider<string>;
credentials?: AwsCredentialIdentityProvider;
endpoint?: string | Endpoint | Provider<Endpoint>;
logger?: Logger;
retryMode?: string | Provider<RetryMode>;
maxAttempts?: number | Provider<number>;
}Complete queue lifecycle operations including creation, deletion, attribute management, and URL resolution.
class CreateQueueCommand {
constructor(input: CreateQueueCommandInput);
}
interface CreateQueueCommandInput {
QueueName: string;
Attributes?: Record<QueueAttributeName, string>;
tags?: Record<string, string>;
}
class DeleteQueueCommand {
constructor(input: DeleteQueueCommandInput);
}
interface DeleteQueueCommandInput {
QueueUrl: string;
}Core messaging functionality for sending, receiving, and deleting messages with full support for message attributes and batch operations.
class SendMessageCommand {
constructor(input: SendMessageCommandInput);
}
interface SendMessageCommandInput {
QueueUrl: string;
MessageBody: string;
DelaySeconds?: number;
MessageAttributes?: Record<string, MessageAttributeValue>;
MessageDeduplicationId?: string;
MessageGroupId?: string;
}
class ReceiveMessageCommand {
constructor(input: ReceiveMessageCommandInput);
}
interface ReceiveMessageCommandInput {
QueueUrl: string;
AttributeNames?: QueueAttributeName[];
MessageAttributeNames?: string[];
MaxNumberOfMessages?: number;
VisibilityTimeoutSeconds?: number;
WaitTimeSeconds?: number;
}High-performance batch operations for sending, deleting, and changing visibility of multiple messages in single API calls.
class SendMessageBatchCommand {
constructor(input: SendMessageBatchCommandInput);
}
interface SendMessageBatchCommandInput {
QueueUrl: string;
Entries: SendMessageBatchRequestEntry[];
}
interface SendMessageBatchRequestEntry {
Id: string;
MessageBody: string;
DelaySeconds?: number;
MessageAttributes?: Record<string, MessageAttributeValue>;
}Access control management for SQS queues including adding and removing permissions for cross-account access.
class AddPermissionCommand {
constructor(input: AddPermissionCommandInput);
}
interface AddPermissionCommandInput {
QueueUrl: string;
Label: string;
AWSAccountIds: string[];
Actions: string[];
}Queue configuration management through attributes and metadata management through tags.
class SetQueueAttributesCommand {
constructor(input: SetQueueAttributesCommandInput);
}
interface SetQueueAttributesCommandInput {
QueueUrl: string;
Attributes: Record<QueueAttributeName, string>;
}
class TagQueueCommand {
constructor(input: TagQueueCommandInput);
}
interface TagQueueCommandInput {
QueueUrl: string;
Tags: Record<string, string>;
}Advanced queue management for handling failed messages with dead letter queue configuration and source queue discovery.
class ListDeadLetterSourceQueuesCommand {
constructor(input: ListDeadLetterSourceQueuesCommandInput);
}
interface ListDeadLetterSourceQueuesCommandInput {
QueueUrl: string;
NextToken?: string;
MaxResults?: number;
}Advanced message management for moving messages between queues with progress tracking and cancellation support.
class StartMessageMoveTaskCommand {
constructor(input: StartMessageMoveTaskCommandInput);
}
interface StartMessageMoveTaskCommandInput {
SourceArn: string;
DestinationArn?: string;
MaxNumberOfMessagesPerSecond?: number;
}interface Message {
MessageId?: string;
ReceiptHandle?: string;
MD5OfBody?: string;
Body?: string;
Attributes?: Record<MessageSystemAttributeName, string>;
MD5OfMessageAttributes?: string;
MessageAttributes?: Record<string, MessageAttributeValue>;
}
interface MessageAttributeValue {
StringValue?: string;
BinaryValue?: Uint8Array;
StringListValues?: string[];
BinaryListValues?: Uint8Array[];
DataType: string;
}
enum QueueAttributeName {
All = "All",
Policy = "Policy",
VisibilityTimeout = "VisibilityTimeout",
MaxReceiveCount = "MaxReceiveCount",
MessageRetentionPeriod = "MessageRetentionPeriod",
ApproximateNumberOfMessages = "ApproximateNumberOfMessages",
ApproximateNumberOfMessagesNotVisible = "ApproximateNumberOfMessagesNotVisible",
CreatedTimestamp = "CreatedTimestamp",
LastModifiedTimestamp = "LastModifiedTimestamp",
QueueArn = "QueueArn",
ApproximateNumberOfMessagesDelayed = "ApproximateNumberOfMessagesDelayed",
DelaySeconds = "DelaySeconds",
ReceiveMessageWaitTimeSeconds = "ReceiveMessageWaitTimeSeconds",
RedrivePolicy = "RedrivePolicy",
FifoQueue = "FifoQueue",
ContentBasedDeduplication = "ContentBasedDeduplication",
KmsMasterKeyId = "KmsMasterKeyId",
KmsDataKeyReusePeriodSeconds = "KmsDataKeyReusePeriodSeconds",
DeduplicationScope = "DeduplicationScope",
FifoThroughputLimit = "FifoThroughputLimit",
RedriveAllowPolicy = "RedriveAllowPolicy",
SqsManagedSseEnabled = "SqsManagedSseEnabled"
}All SQS operations can throw service exceptions that extend SQSServiceException. Common exceptions include:
QueueDoesNotExist - Queue URL incorrect or queue deletedInvalidAddress - Invalid ID specifiedOverLimit - Action violates service limitsRequestThrottled - Request rate exceeded limitsInvalidSecurity - Request not over HTTPS or missing SigV4MessageNotInflight - Message not available for visibility changeReceiptHandleIsInvalid - Receipt handle invalid or expiredtry {
await client.send(command);
} catch (error) {
if (error.name === 'QueueDoesNotExist') {
console.error('Queue not found:', error.message);
} else if (error.name === 'RequestThrottled') {
console.error('Rate limit exceeded:', error.message);
}
// Access metadata
console.log('Request ID:', error.$metadata?.requestId);
}