# to_amqp

Sends messages to an AMQP exchange.

```tql
to_amqp url:secret, [message=str|blob, channel=int, exchange=str,
                     routing_key=str, options=record, mandatory=bool,
                     immediate=bool]
```

## Description

The `to_amqp` operator is an [AMQP](https://www.amqp.org/) 0-9-1 client that sends messages to an exchange. It sends one AMQP message per input event.

By default, `to_amqp` serializes each input event as NDJSON. Use the `message` option to send a specific string or blob expression instead. If the expression evaluates to `null` or another type, the operator skips that event and emits a warning.

### `url: secret`

The URL of the AMQP server. The URL must have the following format:

```txt
amqp://[USERNAME[:PASSWORD]@]HOSTNAME[:PORT]/[VHOST]
```

The URL is required. Values in the URL override corresponding entries from `options`.

### `message = str | blob (optional)`

The expression that produces the AMQP message body.

Defaults to `this.print_ndjson()`.

### `channel = int (optional)`

The channel number to use.

The value must fit into an unsigned 16-bit integer.

Defaults to `1`.

### `exchange = str (optional)`

The exchange to publish to.

Defaults to `"amq.direct"`.

### `routing_key = str (optional)`

The routing key to publish messages with.

Defaults to the empty string.

### `options = record (optional)`

An option record for the AMQP connection. Values must be numbers, booleans, strings, or secrets.

Available options are:

```yaml
hostname: 127.0.0.1
port: 5672
ssl: false
vhost: /
max_channels: 2047
frame_size: 131072
heartbeat: 0
sasl_method: plain
username: guest
password: guest
```

### `mandatory = bool (optional)`

If `true`, the broker returns an unroutable message instead of silently dropping it.

Defaults to `false`.

### `immediate = bool (optional)`

If `true`, the broker returns an undeliverable message when no consumer can receive it immediately.

Defaults to `false`.

## Examples

### Send events to an AMQP exchange

```tql
plugins
to_amqp "amqp://admin:pass@0.0.0.1:5672/vhost"
```

### Send a specific field as the message body

```tql
from {payload: "hello"}
to_amqp "amqp://admin:pass@0.0.0.1:5672/vhost", message=payload
```

### Configure connection options

```tql
from {payload: "hello"}
to_amqp "amqp://broker/vhost", message=payload, options={
  username: "tenzir",
  password: secret("amqp-password"),
  heartbeat: 30,
}
```

## See Also

* [`from_amqp`](https://preview.docs.tenzir.com/375/375/reference/operators/from_amqp.md)
* [Send to destinations](https://preview.docs.tenzir.com/375/375/guides/routing/send-to-destinations.md)
* [AMQP](https://preview.docs.tenzir.com/375/375/integrations/amqp.md)