# Your first Power Automate integration

Create the connection and trigger a flow from a Python service, reading back what the flow returns.

This tutorial shows how to trigger a Power Automate flow from a Python service and how to read what the flow returns. The flow already exists in Power Automate and starts with an HTTP request trigger, the service sends it a payload and receives its response.

## Step 1 - Create an app registration in Entra ID {#step-1-create-an-app-registration-in-entra-id}

The connection signs in to Power Automate as an app registration from the Azure portal. Its overview page has the two IDs the connection needs, and a secret is created under `Certificates and secrets`.

You need to copy:

1. Directory (tenant) ID
2. Application (client) ID
3. The secret's value, which the portal shows once, right after it is created

![Azure portal, App registrations - the Essentials section with the display name, the application (client) ID, the object ID, the directory (tenant) ID and the supported account types](https://zatosource-production.b-cdn.net/docs/gfx/api/dev/examples/cloud/power-automate/tutorial/app-registration.webp)

You also need the ID of the Power Platform environment the flow is in. The [Power Platform admin center](https://admin.powerplatform.microsoft.com) lists it under `Environments`, and it looks like `Default-98765432-5432-5432-5432-dcba98765432`.

## Step 2 - Create a Power Automate connection in Zato Dashboard {#step-2-create-a-power-automate-connection-in-zato-dashboard}

Open the Zato Dashboard and go to `Cloud → Microsoft → Power Automate`. Click "Create a new connection" and fill in the form:

| Field | Value |
| --- | --- |
| Name | `Zato Power Automate` |
| Address | `https://api.flow.microsoft.com` |
| Tenant ID | The directory (tenant) ID |
| Client ID | The application (client) ID |
| Client secret | The secret of the app registration |
| Environment ID | The environment ID from step 1 |

Click "Ping". It obtains a token and lists the flows in the environment.

## Step 3 - Find the flow's ID {#step-3-find-the-flows-id}

Open the flow in Power Automate. Its ID is the GUID at the end of the address bar, after `/flows/`, e.g. `8f3c1a2e-5b7d-4e9a-b6c0-2d4f7a9e1c35`. Copy it for the service.

## Step 4 - Write the service {#step-4-write-the-service}

The service triggers a flow that checks whether a customer's order qualifies for a discount. It sends the customer and the order total, and the flow replies with the discount it worked out.

Take the code below and deploy it, then invoke it from the Dashboard's IDE under `Services → IDE`.

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

# Zato
from zato.server.service import Service

class CheckDiscount(Service):

    name = 'power-automate.check-discount'

    def handle(self):

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

        # .. the payload the flow receives ..
        payload = {
            'customer_id': 'CUST-001',
            'order_total': 1250.50,
        }

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

## Step 5 - Run it {#step-5-run-it}

After invoking the service you see the flow's response:

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

The same run is listed in the flow's run history in Power Automate, with the payload the service sent as the trigger's input.

## See also {#see-also}

- [Triggering Power Automate flows](https://zato.io/docs/dev/examples/cloud/power-automate/triggering-flows.html) - Triggering a flow by its ID or by its callback URL and what comes back
- [Power Automate API - Flows](https://zato.io/docs/dev/examples/cloud/power-automate/api/flows.html) - trigger, trigger\_url and the flow methods, with every parameter
- [Power Automate API - Connection](https://zato.io/docs/dev/examples/cloud/power-automate/api/connection.html) - Every field of 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
