CDS, Data Integration, Flow, Microsoft Flow, Model-Driven Apps, Power Platform, PowerApps

Creating CDS records from an open API – Nasa’s Astronomy Picture of the Day – using Microsoft Flow

For about a week ago I had some fun exploring one of Nasa’s Open APIs, Astronomy Picture of the Day. I made a Canvas App which displayed the picture of today (with a Microsoft Flow in the background). As a separate thing I also created a Microsoft Flow which I scheduled to run each day, it gets me the picture of today and saves it as a record in the Common Data Service. Before I set up the flow I created an entity to hold this information. This blog post will let you in on how I set up the flow which creates CDS records and how to display the records in a Model-Driven App.

Actually, I had no plans for writing this blog post. However something got me to sit down and do just that. This morning I got a bit surprised when my Twitter feed said that there was a blog post about updating a SharePoint document library from this same API with a Microsoft Flow as well as a comment that someone else had created a video about utilizing the API in a PowerApp using Microsoft Flow. Check out That API Guy and his video. The Astronomy Picture of the Day API sure seams to be very popular.

No need for me to demonstrate the same when it comes to the Canvas App. My version did not have a date field though, it had a button and a Microsoft Flow behind that button which made a call to the API and returned the photo of today back to the Canvas App. However, I thought I might as well share my version of the Microsoft Flow which creates the CDS records.  

Nasa’s Astronomy Picture of the Day

According to Nasa one of the most popular websites at Nasa is the Astronomy Picture of the Day. The documentation says:  

This endpoint structures the APOD imagery and associated metadata so that it can be repurposed for other applications.

 You can find all the details about it here. All you need to know in order to utilize it is summarized in the picture below.

APOD documentation found at api.nasa.gov

You can get an API key for free, just by signing up at api.nasa.gov.

Creating the entity in the CDS

So we want to store these pictures in the CDS. In order to do that we need to create an entity in which we can store these pictures/videos. Then we will set up a Microsoft Flow which makes a call to the API and creates a record of the entity. The Microsoft Flow will be scheduled to run each day (simply since there is one new picture for each day).

Let us start with creating the entity. Go to the maker portal. Since we want our entity and the fields of the entity to have a certain prefix (the prefix comes from the publisher) we want to work in a solution. This is also best practise in general, to work in solution I mean. We want to have proper ALM (Application Lifecycle Management) for everything we do and we want to have a structured way of working etc. Just do it – make it a habit to work in a solution.

Go to Solutions and open your solution (or create a new solution with a publisher if you do not have any solution yet). From your solution choose to create a new entity. Please note, my solution is not called Sales Hub, but Space Hub!

Extending the Common Data Model – Creating a new entity

One thing which is new in the 2019 Release Wave 2 is that when you have created your entity you do not need to wait for it to be saved, that is all done in the background. Nice! Now we can continue with our work creating fields and views etc. This is a real time saver! Go to Fields and create the fields needed for your entity. Then go to views and add your fields to the views of your choice and then go to forms and the main form in order to add your fields to the form.

The below picture shows the fields that I used.

Fields for the new entity

Setting up the Microsoft Flow

From your solution, choose New -> Flow. The below picture shows what the Microsoft Flow looks like when it has been set up. I will then go through each step.

The Microsoft Flow which gets the Astronomy Pictures of the Day and creates a CDS record

The first step is used in order to get the flow scheduled.

A step used in order to get the flow scheduled to run each day

The next step is a HTTP request and here we enter the query and we should include our key. I have used the demo key here though.

HTTP Request which calls the API

The next thing to do is to setting up the Parse JSON step. There is a little trick you can use here. Run your flow without this step first. Then go and check the run flow. Copy the Body part. Then go back and edit your flow. Add a Parse JSON step and click “Use sample payload to generate schema”. Add what was in the Body of the run flow. A schema will be created for you.

Generating a schema

Below is what the schema should look like in this case.

{
    "type": "object",
    "properties": {
        "date": {
            "type": "string"
        },
        "explanation": {
            "type": "string"
        },
        "hdurl": {
            "type": "string"
        },
        "media_type": {
            "type": "string"
        },
        "service_version": {
            "type": "string"
        },
        "title": {
            "type": "string"
        },
        "url": {
            "type": "string"
        }
    }
}

The last step is to create the record. I have used the Common Data Service (current environment) connector.

Displaying the pictures in a Model-Driven App

So now we have a Microsoft Flow which goes and gets a new Astronomy picture or video for us each day, cool! But what to do which this data? Let us go and create a Model-Driven App and then we can go to this app when we want to check out the latest pictures or videos added to the CDS.

Again, go to your solution and choose to create a new Model-Driven App. Give the app a name and when the App Designer opens go and edit the Sitemap. Give the heading good names and add your entity.

Sitemap Designer – Rename the headings and add your entity

Play your app each day and follow how the CDS and the data for your entity grows day by day. Now, what to do with these pictures? I think a PowerApps Component Framework conponent which displayed the picture from the URL on the form would be nice. Perhaps link these pictures to other deep space information stored in the CDS? Let me think about it and I might come back to this subject and write some more.

The record created yesterday

Conclusion

Microsoft Flow can easily be used for integrating information from an open API and it can be scheduled to run at a frequency and time of your choice.

Please note that this is just an example of how you can get info from an open API into the CDS with the use of a Microsoft Flow which has been set to run on a schedule. In many cases there is no need to store data in the CDS, e.g. you might as well have the data displayed in a Canvas App or you might want to consider to use Virtual Entities.

Anyway, this is a great way to explore both Microsoft Flow with API calls as well as doing some practise with the Common Data Service, creating a new entity using the new experience for designing forms and views.  

Yesterdays Astronomy Picture of the Day. Curiosity on the Red Planet.

I hope that you enjoyed this blog post. A big thanks to Jon Levesque for highlighting the blog post about updating a SharePoint document library, that got me inspired to write about this same topic but from a CDS and Model-Driven point of view.

2 thoughts on “Creating CDS records from an open API – Nasa’s Astronomy Picture of the Day – using Microsoft Flow”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s