# Triggering Power Automate flows

Trigger a flow by its ID or by its callback URL and read what the flow returns.

This page shows how to trigger a Power Automate flow from a Python service in two ways:

1. By the flow's ID, with the connection looking up the callback URL
2. By the callback URL itself, when the service already has it

A flow that a service triggers starts with an HTTP request trigger, and everything the flow does after that - approvals, Teams messages, SharePoint items, email - is defined in the flow and is not part of the service. The service sends a payload and receives whatever the flow replies with.

## Connection and IDs {#connection-and-ids}

The app registration, the Power Automate connection in the Zato Dashboard and the flow ID are the same as in the [tutorial](https://zato.io/docs/dev/examples/cloud/power-automate/tutorial.html), so set them up as described there first.

## Triggering a flow by its ID {#triggering-a-flow-by-its-id}

In the tutorial, the payload was part of the service. In this example, a caller of the service sends the customer and the order total, and the service passes them to the flow.

The service below will:

1. Take the customer ID and the order total from its input
2. Trigger the flow with them, through [`conn.trigger`](https://zato.io/docs/dev/examples/cloud/power-automate/api/flows.html#trigger)
3. Return the flow's response to the caller

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

# Zato
from zato.server.service import Service

class CheckDiscountForOrder(Service):

    name = 'power-automate.check-discount-for-order'
    input = 'customer_id', 'order_total'

    def handle(self):

        # The ID from the tutorial ..
        flow_id = '8f3c1a2e-5b7d-4e9a-b6c0-2d4f7a9e1c35'

        # .. the payload, taken from our input ..
        payload = {
            'customer_id': self.request.input.customer_id,
            'order_total': self.request.input.order_total,
        }

        # .. get a Power Automate connection ..
        conn = self.microsoft.power_platform['Zato Power Automate']

        # .. trigger the flow ..
        result = conn.trigger(flow_id, payload)

        # .. and return its response to our caller.
        self.response.payload = result
```

Invoked with `{"customer_id": "CUST-001", "order_total": 1250.50}`, the service returns:

```json
{"customer_id": "CUST-001", "is_eligible": true, "discount_percent": 10}
```

`conn.trigger` returns the flow's response parsed from JSON. A flow that replies with no body makes it return `None`, and a service that only starts such a flow has nothing to read.

## Triggering a flow by its callback URL {#triggering-a-flow-by-its-callback-url}

`conn.trigger` looks up the callback URL of the flow's trigger before each call. A service that already has the URL - read from its configuration, or received from another system - sends the payload to that URL with [`conn.trigger_url`](https://zato.io/docs/dev/examples/cloud/power-automate/api/flows.html#trigger_url). The URL has its own signature built in, so the connection's token is not used.

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

# Zato
from zato.server.service import Service

class CheckDiscountByUrl(Service):

    name = 'power-automate.check-discount-by-url'
    input = 'customer_id', 'order_total'

    def handle(self):

        # The callback URL, as read from configuration ..
        url = self.config.power_automate.discount_flow_url

        # .. the payload, taken from our input ..
        payload = {
            'customer_id': self.request.input.customer_id,
            'order_total': self.request.input.order_total,
        }

        # .. get a Power Automate connection ..
        conn = self.microsoft.power_platform['Zato Power Automate']

        # .. send the payload to the URL ..
        result = conn.trigger_url(url, payload)

        # .. and return the flow's response to our caller.
        self.response.payload = result
```

The URL itself comes from [`conn.get_trigger_url`](https://zato.io/docs/dev/examples/cloud/power-automate/api/flows.html#get_trigger_url), which is the lookup `conn.trigger` makes.

## See also {#see-also}

- [Power Automate API - Flows](https://zato.io/docs/dev/examples/cloud/power-automate/api/flows.html) - trigger, trigger\_url and get\_trigger\_url, with every parameter
- [Monitoring Power Automate runs](https://zato.io/docs/dev/examples/cloud/power-automate/monitoring-runs.html) - The run each trigger creates - reading, cancelling and resubmitting runs
- [Power Automate API - Connection](https://zato.io/docs/dev/examples/cloud/power-automate/api/connection.html) - The connection form and how tokens are obtained and refreshed

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