> ## Documentation Index
> Fetch the complete documentation index at: https://docs.stackshift.cloud/llms.txt
> Use this file to discover all available pages before exploring further.

# Send messages

> Send one message or a batch of up to 100 from the SDKs, the CLI or the API, as JSON, text or bytes, with an optional delay and idempotency key.

> [!TIP]
>
> **Live.** This area is documented as current, user-reliable behavior.

## Goal

Put messages on a queue from your app.

## Prerequisites

* A queue
* An API key that can use the queue's project

## Workflow

**Step 1**

Install the SDK for your language and create a client with your API key.

**Step 2**

Call send for one message or sendBatch for up to 100.

**Step 3**

Pass a delay when the message should wait before delivery.

**Step 4**

Pass an idempotency key when a retry of your own code might send the same message twice.

## Send one message

```ts JavaScript theme={null}
import { StackShift } from '@stackshift-cloud/sdk'

const client = new StackShift({ apiKey: process.env.STACKSHIFT_API_KEY! })

await client.queues.send('orders', { orderId: 'ord_123' })
await client.queues.send('orders', 'plain text', { contentType: 'text', delaySeconds: 60 })
await client.queues.send('orders', { orderId: 'ord_123' }, { idempotencyKey: 'order:ord_123' })
```

```python Python theme={null}
from stackshift import StackShift

client = StackShift()

client.queues.send("orders", {"order_id": "ord_123"})
client.queues.send("orders", "plain text", content_type="text", delay_seconds=60)
client.queues.send("orders", {"order_id": "ord_123"}, idempotency_key="order:ord_123")
```

```go Go theme={null}
client, err := stackshift.New(stackshift.Options{APIKey: os.Getenv("STACKSHIFT_API_KEY")})
if err != nil {
	return err
}
_, err = client.Queues.Send(ctx, "orders", map[string]any{"order_id": "ord_123"}, stackshift.QueueSendOptions{})
_, err = client.Queues.Send(ctx, "orders", "plain text", stackshift.QueueSendOptions{ContentType: stackshift.QueueContentText})
_, err = client.Queues.Send(ctx, "orders", map[string]any{"order_id": "ord_123"}, stackshift.QueueSendOptions{IdempotencyKey: "order:ord_123"})
```

```php PHP theme={null}
$queues = new \StackShift\QueuesClient(getenv('STACKSHIFT_API_KEY'));

$queues->send('orders', ['order_id' => 'ord_123']);
$queues->send('orders', 'plain text', ['content_type' => 'text', 'delay_seconds' => 60]);
$queues->send('orders', ['order_id' => 'ord_123'], ['idempotency_key' => 'order:ord_123']);
```

```ruby Ruby theme={null}
require "stackshift/queues"

queues = StackShift::QueuesClient.new(api_key: ENV.fetch("STACKSHIFT_API_KEY"))

queues.send_message("orders", { order_id: "ord_123" })
queues.send_message("orders", "plain text", content_type: "text", delay_seconds: 60)
queues.send_message("orders", { order_id: "ord_123" }, idempotency_key: "order:ord_123")
```

```bash CLI theme={null}
stackshift queues send orders '{"order_id":"ord_123"}' --idempotency-key order:ord_123
echo hello | stackshift queues send orders - --type text --delay 60
stackshift queues send thumbnails --file photo.jpg --type bytes
```

```bash curl theme={null}
curl -X POST https://api.stackshift.cloud/api/v1/queues/orders/messages \
  -H "Authorization: Bearer $STACKSHIFT_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"body": {"order_id": "ord_123"}, "content_type": "json", "delay_seconds": 0, "idempotency_key": "order:ord_123"}'
```

## Send a batch

A batch takes up to 100 messages and 256 KB in total. Each message can set its own content type, delay and idempotency key. The reply lists the ids in the same order.

```ts JavaScript theme={null}
await client.queues.sendBatch('orders', [
  { body: { orderId: 'ord_1' } },
  { body: { orderId: 'ord_2' }, delaySeconds: 30 },
])
```

```python Python theme={null}
client.queues.send_batch("orders", [
    {"body": {"order_id": "ord_1"}},
    {"body": {"order_id": "ord_2"}, "delay_seconds": 30},
])
```

```go Go theme={null}
delay := 30
ids, err := client.Queues.SendBatch(ctx, "orders", []stackshift.QueueMessageInput{
	{Body: map[string]any{"order_id": "ord_1"}},
	{Body: map[string]any{"order_id": "ord_2"}, DelaySeconds: &delay},
})
```

```php PHP theme={null}
$queues->sendBatch('orders', [
    ['body' => ['order_id' => 'ord_1']],
    ['body' => ['order_id' => 'ord_2'], 'delay_seconds' => 30],
]);
```

```ruby Ruby theme={null}
queues.send_batch("orders", [
  { body: { order_id: "ord_1" } },
  { body: { order_id: "ord_2" }, delay_seconds: 30 }
])
```

```bash curl theme={null}
curl -X POST https://api.stackshift.cloud/api/v1/queues/orders/messages/batch \
  -H "Authorization: Bearer $STACKSHIFT_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"messages": [{"body": {"order_id": "ord_1"}}, {"body": {"order_id": "ord_2"}, "delay_seconds": 30}]}'
```

## Content types

* json (default): body is any JSON value and is delivered as the same value.
* text: body is a string.
* bytes: body is base64 in the API. The SDKs take raw bytes and encode them for you, and pulled messages have a bytes() helper (Bytes() in Go) that decodes them.
* The SDKs check a batch holds at most 100 messages before sending anything.
* A queue only accepts the content types chosen in its settings.

## Delays and idempotency

* delay\_seconds holds a message back for up to 86,400 seconds (24 hours). Without it, the queue's default delay applies.
* idempotency\_key makes a repeat send return the first message's id with 200 instead of 201, and no second message is stored. Keys are remembered for the queue's retention.

## Expected result

> [!TIP]
>
> Each send returns the message id (msg\_ followed by 12 characters), when it was sent and when it becomes visible to the consumer.

## Common failures

> [!WARNING]
>
> * 413 message\_too\_large: a message is over 128 KB.
> * A batch over 100 messages or 256 KB is refused; split it.
> * 429 queue\_rate\_limited: the queue is taking more than 5,000 messages a second. Back off and try again.
> * 507 queue\_full: the backlog has reached 25 GB. Fix or scale the consumer, or purge.

## Related guides

> [card] [Consumers and Function consumers](https://docs.stackshift.cloud/queues/consumers.md)
>
> Each queue has one consumer: a Function, an HTTPS endpoint, or pull. Function consumers are chosen in the dashboard or API, not in code.

> [card] [Limits and error codes](https://docs.stackshift.cloud/queues/limits-and-errors.md)
>
> Sizes, rates, retention and the error codes the Queues API returns when a limit is reached.

> [card] [Laravel queue driver](https://docs.stackshift.cloud/queues/laravel.md)
>
> Use StackShift Queues as a Laravel queue connection. dispatch() and php artisan queue:work keep working as they are.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.