Friday, February 24, 2023

Sample flow Using API with Power Automate

  Trigger

Every flow starts with a trigger. This time I’ll go with a manual trigger with an email input field (so I can also send bad jokes to my poor co-workers).

HTTP action

And now we need a joke. The action for this in Power Automate is HTTP. Official Joke API does not require authentication, so we can just set the method and URI.

The base URL for the Joke API is https://official-joke-api.appspot.com/. There is no base path in this particular case.

There are several endpoints available for the Joke API, but in this exercise I’ve decided to use /random_joke.

The API documentation doesn’t say which method to use, but in this case we can safely assume that the method is GET since we only want to grab a new joke.

Method: GET
URI: https://official-joke-api.appspot.com/random_joke

Now save the flow and make a test run. If everything works, you should have a little joke in the body of your HTTP action in JSON format. Here’s an example:

{
  "id": 162,
  "type": "general",
  "setup": "What did Michael Jackson name his denim store?",
  "punchline": "Billy Jeans!"
}

Make sure to copy the text content from the body field. We need it in the next step!

Parse JSON

We get the joke in neat JSON format, but to separate the setup from the punchline, we need the Parse JSON action. Parse JSON… parses the JSON so we can use the values later as dynamic content.

So, as the content we set the body from the previous HTTP action. And then we need a schema to define the structure and the content of a JSON object.

But from where is this schema coming?

If you have followed my instructions carefully, you should have the JSON content copied. If not, go ahead and do it now. (Or be lazy and copy the JSON example above.)

Choose the generate the schema from a sample (because we have a sample; the JSON we copied from the HTTP request body field).

Paste the JSON from the HTTP request body to the sample editor and click “Done”. It will generate the schema for you automatically.

JSON Schema is a grammar language for defining the structure, content etc.

For example here we can see that ID is a whole number (integer) and the joke is text (string).

{
     "type": "array",
     "items": {
         "type": "object",
         "properties": {
             "id": {
                 "type": "integer"
             },
             "type": {
                 "type": "string"
             },
             "setup": {
                 "type": "string"
             },
             "punchline": {
                 "type": "string"
             }
         },
         "required": [
             "id",
             "type",
             "setup",
             "punchline"
         ]
     }
 }

Now we have the joke, and we have parsed it with Parse JSON action. We can now use the setup and the punchline as separate dynamic values.

Post message

I want to use the Flow bot to post the messages so I choose the action “Post a message as the Flow bot to a user”.

I add the email from the trigger to the recipient field. But to the message I can choose values from the Parse JSON action. Pretty cool.

For the first message I want the setup value and for the second I want the punchline value. And a little 10 second delay between since isn’t that how you tell these jokes in real life? ?

Now we have all the blocks ready in our flow and we can go ahead and test it. I hope you get a better joke than me though.

Congratulations, you have now successfully built a cloud flow using an API. And this is almost all there is to it. Well, not really. But understanding the basic terminology and how to implement things with Power Automate certainly helps you to get started, and experimenting does the rest.

No comments:

Post a Comment

How to Deploy your API as a web app or API app

  Before you can call your custom API from a logic app workflow, deploy your API as a web app or API app to Azure App Service. To make your ...