# Build messages in Python

Build nested messages and responses through plain dot access.

Messages in Zato are built through plain dot access - you assign attributes and nested structures appear on their own, with no declarations, no dictionaries to pre-build and no classes to define first.

The same pattern works everywhere a message is produced: standalone objects, service responses and SOAP requests alike.

## Standalone messages {#standalone-messages}

A `Message` is a dynamic object - assigning to any attribute path creates the whole path in one go:

```python
# -*- coding: utf-8 -*-

# Zato
from zato.common import Message

# ###############################################################################

msg = Message()

msg.customer.name = 'John Doe'
msg.customer.address.city = 'Amsterdam'
msg.customer.address.country = 'Netherlands'
```

Calling `to_dict` returns the tree as a plain dict, in the order the fields were assigned in:

```python
msg.to_dict()
```

```json
{"customer": {"name": "John Doe", "address": {"city": "Amsterdam", "country": "Netherlands"}}}
```

Assigning a list means repeated elements, and the list may contain scalars and messages alike:

```python
line1 = Message()
line1.product = 'ABC'
line1.quantity = 3

line2 = Message()
line2.product = 'DEF'
line2.quantity = 7

msg = Message()
msg.order.lines = [line1, line2]
```

```json
{"order": {"lines": [{"product": "ABC", "quantity": 3}, {"product": "DEF", "quantity": 7}]}}
```

## Messages as responses {#messages-as-responses}

A message built anywhere in your code can be assigned to `self.response.payload` as a whole and it serializes to the channel's data format automatically:

```python
# -*- coding: utf-8 -*-

# Zato
from zato.common import Message
from zato.server.service import Service

# ###############################################################################

class GetCustomer(Service):

    name = 'example.messages.get-customer'

    def handle(self) -> 'None':

        msg = Message()
        msg.customer.name = 'John Doe'
        msg.customer.address.city = 'Amsterdam'

        self.response.payload = msg

# ###############################################################################
```

## Zero-declaration responses {#zero-declaration-responses}

Most of the time you don't even need a standalone object - `self.response.payload` is already a message, so a service with no input or output declarations at all builds its response through the same dot access directly:

```python
# -*- coding: utf-8 -*-

# Zato
from zato.server.service import Service

# ###############################################################################

class GetCustomer(Service):

    name = 'example.messages.get-customer'

    def handle(self) -> 'None':

        self.response.payload.customer.name = 'John Doe'
        self.response.payload.customer.address.city = 'Amsterdam'

# ###############################################################################
```

```json
{"customer": {"name": "John Doe", "address": {"city": "Amsterdam"}}}
```

If you prefer a fixed structure for your responses, declare output names or [data models](https://zato.io/docs/dev/model/index.html) and the payload will follow them - the [request and response chapter](https://zato.io/docs/dev/request-response/index.html) covers all three cases.

## SOAP {#soap}

Behind SOAP channels, the very same dot access builds SOAP messages - you assign attributes to a `SOAPMessage`, the channel takes care of the envelope, and you never touch XML at all. The [SOAP tutorial](https://zato.io/tutorials/soap/receiving.html) shows the whole flow.

## See also {#see-also}

- [Request and response](https://zato.io/docs/dev/request-response/index.html) - Everything about self.request.input and self.response.payload
- [Data models](https://zato.io/docs/dev/model/index.html) - Declare your API's data structures, validated and documented
- [Config tables](https://zato.io/docs/dev/examples/config-tables.html) - Translate message codes from one party's vocabulary to another's

## 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
