From generated text to a running flow.
How to get a Power Automate flow from generated text to a running flow without clicking through the portal: sign in once, find the user's real resources, check the flow, deploy it, run it and read the result back. Cloud flows and desktop flows. Every claim in the KB is marked with how sure it is.
Sign in, discover, validate, deploy, run, read, fix.
The KB in the order you'd do it, laid out like a flow's run details. Pick a step to see the request, what comes back and what goes wrong. Each step carries the mark the KB gives it.
Sign in once
Re-confirmedOne device-code sign-in can serve Graph, the Flow API and Dataverse. Start it with offline_access in the scope, so a refresh token comes back.
Inputs: one scope per resource
https://graph.microsoft.com/.default offline_accesshttps://service.flow.microsoft.com/.default offline_accesshttps://<org>.crm<N>.dynamics.com/.default offline_accessOutputs
A refresh token. Store it encrypted on the server, exchange it for an access token per resource with a different scope each time, and save the rotated refresh token each time.
Watch out for
- A token the Flow API won't take gives 401. The app registration needs Flow API permissions (
Flows.Manage.All,Flows.Read.All) consented. .defaultonly grants what's already consented. Discovery found 0 SharePoint sites, with no error, untilSites.Read.Allwas explicitly requested and consented.- A browser extension can't redeem a device code itself (
AADSTS9002326). Run device-code start, poll and refresh on your server.
Discover
Re-confirmedGive the model the user's real resources instead of letting it invent them. Models invent operation ids, site URLs and list names, and anything they invent fails at deploy time.
Requests (api-version=2016-11-01)
GET https://api.powerapps.com/providers/Microsoft.PowerApps/environmentsGET https://api.powerapps.com/providers/Microsoft.PowerApps/connections?$filter=environment eq '<envId>'GET https://api.powerapps.com/providers/Microsoft.PowerApps/apis/shared_<connector>?$filter=environment eq '<envId>'Outputs
Pick the environment that has properties.linkedEnvironmentMetadata.instanceUrl; that field is also its Dataverse org URL. Keep only connections whose statuses[0].status is Connected, one per connector.
Watch out for
- The
apiscall fails withMissingEnvironmentFilterwithout the environment filter. - The first environment in the list isn't always the right one.
- Inject all of a connector's operations, not a shortlist.
Validate
Re-confirmedNo write path checks the Robin for you. Something a paste would reject can still be written through the API, and it then fails to open and fails to run. So the Designer is the compiler.
The checks, cheapest first
- Static check. The Action KB's
check-flows.jscatches every silent-reject pattern known (107 of 107 in the probe corpus). Run it on the Action KB page. - Designer paste. Paste into an empty flow and read the status bar, the error list and the action count.
- For flows started from the cloud, put
ON ERRORonly after actions that can raise errors.
Outputs
17 of 17. Everything that pasted clean also opened and ran. Everything a paste rejected or flagged failed. Paste-validation predicts API delivery exactly.
Watch out for
- A silent reject looks clean: the canvas is empty, but the status still says Ready. Require an action count above zero.
- A Dataverse write accepts anything; it isn't a validator.
Deploy the cloud flow
Re-confirmedThe deploy path in production use. The flow lands Stopped, which is deliberate: the user reviews it first.
Request
POST https://api.flow.microsoft.com/providers/Microsoft.ProcessSimple/environments/<envId>/flows?api-version=2016-11-01
Authorization: Bearer <Flow-scoped token>
Content-Type: application/json; charset=utf-8
{
"properties": {
"displayName": "Invoice notification",
"definition": { ...Logic Apps workflow definition... },
"connectionReferences": {
"shared_office365": {
"connectionId": "/providers/Microsoft.PowerApps/apis/shared_office365/connections/<connectionName>",
"connectionName": "<connectionName>",
"id": "/providers/Microsoft.PowerApps/apis/shared_office365"
}
},
"state": "Stopped"
}
}
POST https://api.flow.microsoft.com/providers/Microsoft.ProcessSimple/environments/<envId>/flows/<flowId>/start?api-version=2016-11-01POST .../flows/<flowId>/stop?api-version=2016-11-01Outputs
The response's name is the new flow's id. Starting a flow doesn't run it. It only arms the trigger.
Measured Ten long, branching flows
Written by hand with the KB's definition rules, against real resources: 9 of 10 deployed first time, and all 10 runs succeeded. The tenth read from a deliberately missing SharePoint list.
Watch out for
- Connections need a person once. A flow created through the API gets no connections of its own; the user creates each OAuth connection once, signed in, in a browser.
charset=utf-8matters. One "400 Bad Request" was only the missing charset on the content type.- The definition rules the API enforces, each with the exact message you get, are in the KB. Decode a message below.
Deliver the desktop flow
Re-confirmedHand the user the validated script: open an empty flow, press Ctrl+V and Publish. It always works because it's exactly what the Designer accepts. The cost is a manual step.
Watch out for
- Remove PAD's file header lines before handing the script over:
@@ConnectionString,IMPORT ... AS appmask,@SENSITIVE,@INPUT,@OUTPUT. A paste that contains them is dropped.
Unsupported Writing it straight into Dataverse
A desktop flow is a workflow row with category = 6 plus four desktopflowbinaries rows, and no person is needed. Unsupported by Microsoft: a PAD update could change any of it. Proven on one environment, with attended runs only. Try it in a test environment first.
Run it
Re-confirmedA desktop flow runs from a cloud flow, attended, over a direct-to-machine connection. A cloud flow starts from code through an HTTP request trigger.
Inputs
shared_uiflow / RunUIFlow_V2 with uiFlowId = the desktop flow's workflowidPOST .../flows/<id>/triggers/manual/listCallbackUrl?api-version=2016-11-01Outputs
A two-stage pipeline, generating a desktop flow and then a cloud flow that calls it, ran in 18.9 s.
Watch out for
- Cloud-triggered runs are stricter than the Console.
ON ERRORon an action that can't raise errors givesRobinPackagerValidationError; the Console still runs such a flow. - A manual button trigger can't be started through code: it fails with
InvokerConnectionOverrideFailed.
Read the result
Read a desktop flow's run from the Dataverse flowsessions table, and a cloud flow's from its run history.
Requests
GET <org>/api/data/v9.2/flowsessions?$filter=_regardingobjectid_value eq <flowId>&$orderby=createdon desc&$top=1GET .../flows/<id>/runs and .../runs/<run>/actionsOutputs
Poll until the newest row after your start time leaves Running, InProgress, NotStarted, Queued or Waiting. Then read statuscode, errorcode, errormessage and errordetails.
Watch out for
rundetailsholds only machine information.- PAD's logs are in
C:\ProgramData\Microsoft\Power Automate\Logs, one JSON event per line, readable only from an elevated shell.
Fix and redeploy
If the deploy is rejected, send the model the error message, the connection list, the failing definition (cut to about 12,000 characters) and the known-fixes list, run its answer through the same fixups, and deploy again.
Outputs
Three attempts in total is enough; more rarely helps. A deployed flow still isn't a tested flow, so run it once.
Next
Paste the error into the decoder to find its real cause in the KB's troubleshooting table.
Steps without a mark are described in the KB but carry no mark of their own there.
Four ways to deliver a flow. One of them is unsupported.
Section 1 of the KB, "The delivery paths at a glance", built from the KB file itself.
Loading the paths from the KB...
Two findings hold across every path
Validate before you deliver. No write path checks the Robin for you. Connections need a person once. A flow created through the API gets no connections of its own, and the user has to create each OAuth connection once, signed in, in a browser.
Paste the error. Get the real cause.
All 32 error messages from the KB's troubleshooting table, each with what actually causes it and the fix. The matching runs in your browser; nothing you paste is sent anywhere.
Matches
Matches the codes and wording in the table. A message the KB has never seen won't have a row; an issue on GitHub with the full error text is how new ones get added.