# Use an SQS client

Point the AWS SDKs, boto3 or the AWS CLI at the SQS endpoint and use your queues with code written for Amazon SQS.

The SQS endpoint speaks Amazon SQS's JSON protocol (`application/x-amz-json-1.0`), which AWS SDK
and AWS CLI releases since November 2023 use. Libraries built on those SDKs work the same way. What
the endpoint supports is listed in [SQS compatibility](sqs-compatibility.md).

## Prerequisites

- An access key: create one under **Identity**, **Access keys** (see
  [Create an access key](/docs/iam/guide/create-an-access-key)). Keys whose kind there reads "API,
  CLI and S3" sign SQS requests; so do legacy Object Storage keys (`EWSS3…`) and temporary
  credentials with their session token. The key acts as its IAM user: that user's policies, and
  the key's scope when it has one, decide which queues it may use (see [Security](security.md)).
- Permission for the IAM action of each operation you call, listed in
  [SQS compatibility](sqs-compatibility.md). `GetQueueUrl`, which the examples below use, needs
  `queue:GetQueueAttributes` on the queue.
- The queue's URL: it is shown as **Queue URL** at the top of the queue's page in the Deutsche Cloud Console,
  and `GetQueueUrl` returns it.

## Settings

| Setting           | Value                                                                                           |
| ----------------- | ----------------------------------------------------------------------------------------------- |
| Endpoint          | The origin of the queue URL: `https://sqs.<your platform domain>`                               |
| Region            | `de1`                                                                                           |
| Access key ID     | The key's ID                                                                                    |
| Secret access key | The key's secret                                                                                |
| Queue URL         | `https://sqs.<your platform domain>/<organization id>/<queue name>`                             |
| Queue ARN         | `arn:aws:sqs:de1:<organization id>:<queue name>`; `GetQueueAttributes` returns it as `QueueArn` |

## AWS CLI

```sh
export AWS_ACCESS_KEY_ID=EWS1EXAMPLEEXAMPLE00 AWS_SECRET_ACCESS_KEY=secret
SQS=https://sqs.example.com

aws sqs create-queue --endpoint-url "$SQS" --region de1 --queue-name my-queue
QUEUE=$(aws sqs get-queue-url --endpoint-url "$SQS" --region de1 --queue-name my-queue \
  --query QueueUrl --output text)
aws sqs send-message --endpoint-url "$SQS" --region de1 --queue-url "$QUEUE" \
  --message-body '{"orderId":42}'
aws sqs receive-message --endpoint-url "$SQS" --region de1 --queue-url "$QUEUE" \
  --wait-time-seconds 20 --max-number-of-messages 10
```

The queue's **Overview** in the console shows a ready-made **Send with the AWS CLI** command for
that queue under **Connect**.

## Python (boto3)

```python
import boto3, json, os

sqs = boto3.client(
    "sqs",
    endpoint_url=os.environ["EWS_SQS_ENDPOINT"],  # https://sqs.<your platform domain>
    region_name="de1",
    aws_access_key_id=os.environ["EWS_ACCESS_KEY_ID"],
    aws_secret_access_key=os.environ["EWS_SECRET_ACCESS_KEY"],
)
queue = sqs.get_queue_url(QueueName="my-queue")["QueueUrl"]

sqs.send_message(QueueUrl=queue, MessageBody=json.dumps({"orderId": 42}))

while True:
    resp = sqs.receive_message(QueueUrl=queue, MaxNumberOfMessages=10, WaitTimeSeconds=20)
    done = []
    for m in resp.get("Messages", []):
        handle(json.loads(m["Body"]))
        done.append({"Id": m["MessageId"], "ReceiptHandle": m["ReceiptHandle"]})
    if done:
        sqs.delete_message_batch(QueueUrl=queue, Entries=done)
```

## Node.js (`@aws-sdk/client-sqs`)

```js
import { SQSClient, GetQueueUrlCommand, SendMessageCommand } from '@aws-sdk/client-sqs';

const sqs = new SQSClient({
  endpoint: process.env.EWS_SQS_ENDPOINT, // https://sqs.<your platform domain>
  region: 'de1',
  credentials: {
    accessKeyId: process.env.EWS_ACCESS_KEY_ID,
    secretAccessKey: process.env.EWS_SECRET_ACCESS_KEY,
  },
});
const { QueueUrl } = await sqs.send(new GetQueueUrlCommand({ QueueName: 'my-queue' }));
await sqs.send(new SendMessageCommand({ QueueUrl, MessageBody: JSON.stringify({ orderId: 42 }) }));
```

## A FIFO queue

```python
queue = sqs.create_queue(
    QueueName="orders.fifo",
    Attributes={"FifoQueue": "true", "ContentBasedDeduplication": "true"},
)["QueueUrl"]
sqs.send_message(QueueUrl=queue, MessageBody='{"order": 42}', MessageGroupId="customer-7")
```

Two consumers can receive from it side by side; neither gets a message of a group the other holds.
See [FIFO queues](fifo-queues.md).

## Result

Your client works with the queues of the key's organization. A queue URL naming another
organization answers `QueueDoesNotExist`.

## Related

- [SQS compatibility](sqs-compatibility.md)
- [Troubleshooting](troubleshooting.md)
