# AMQP usage examples

AMQP queues and exchanges - receiving messages through channels and sending them with self.outgoing.amqp.

## Receiving messages from queues {#receiving-messages-from-queues}

No programming is needed to receive messages from AMQP queues. Create an AMQP definition and channel in Dashboard, point it to a queue, and a service of your choice will be invoked for each message taken off the queue.

[Video: New AMQP channel](https://zatosource-production.b-cdn.net/docs/gfx/channels/amqp-create.webm?v=1790440788)

Dashboard menu: Connections > Message queues > AMQP > Channels

The message payload will be available in self.request.payload.

```python
from zato.server.service import Service

class MyService(Service):

    def handle(self):

        # The AMQP message payload
        data = self.request.payload

        # Log what was received
        self.logger.info('AMQP message: %s', data)
```

## Sending messages to exchanges {#sending-messages-to-exchanges}

Create an AMQP definition and outgoing connection in Dashboard and use self.outgoing.amqp.send to publish messages.

```python
from zato.server.service import Service

class MyService(Service):

    def handle(self):
        self.outgoing.amqp.send('Hello from Zato', 'my-outconn', '/exchange', 'routing-key')
```

## Sending with headers {#sending-with-headers}

AMQP headers can be attached to each message by passing a headers dict.

```python
from zato.server.service import Service

class MyService(Service):

    def handle(self):
        self.outgoing.amqp.send(
            'My message',
            'my-outconn',
            '/exchange',
            'routing-key',
            headers={'content-type': 'application/json', 'x-source': 'zato'},
        )
```

## Processing and forwarding {#processing-and-forwarding}

A common pattern is to consume a message from one queue, process it and publish the result to another.

```python
from zato.server.service import Service

class MyService(Service):

    def handle(self):

        # Incoming message from an AMQP channel
        data = self.request.payload
        self.logger.info('Processing: %s', data)

        # Forward to another exchange
        self.outgoing.amqp.send(data, 'processed-outconn', '/processed', 'done')
```

## Learn more {#learn-more}

- [Development documentation](https://zato.io/docs/dev/) - The complete reference for writing services
- [Requests and responses](https://zato.io/docs/dev/request-response/) - What a service receives, what it returns and how both are structured
- [Integration examples](https://zato.io/docs/dev/examples/) - Ready-made code for the systems you are likely to connect to
- [IDE and debugging](https://zato.io/docs/dev/ide/) - Write services in the Dashboard or in your own editor
- [Data models](https://zato.io/docs/dev/model/) - Declare inputs and outputs and have them validated for you
- [In-depth API tutorial](https://zato.io/tutorials/main/01.html) - The full platform tutorial, from installation to production patterns
