Tip: How to call a REST API from EasyMorph

With the addition of the “PowerShell” transformation it’s become relatively easy to call various REST APIs from EasyMorph.

Below is a sample PowerShell script that obtains a list of categories from this Community forum, together with the number of posts in each category:

$headers = @{}
$response = Invoke-RestMethod -Method GET -Headers $headers -Uri https://community.easymorph.com/categories.json -ContentType "application/json; charset=utf-8"

foreach($category in $response.category_list.categories)
{ 
    Write-Output ($category.name + ", " + $category.post_count + " posts")
}

Note that Invoke-RestMethod automatically parses the obtained JSON and creates necessary properties in $response. Here is the structure of categories.json:

{
  "category_list": {
    "can_create_category": true,
    "can_create_topic": true,
    "draft": null,
    "draft_key": "new_topic",
    "draft_sequence": 32,
    "categories": [
      {
        "id": 1,
        "name": "General Q&A",
        "color": "B3B5B4",
        "text_color": "FFFFFF",
        "slug": "uncategorized",
        "topic_count": 138,
        "post_count": 547,
        "position": 0,
...

Here is a result:

As an exercise, you can try obtaining the number of topics (not posts) for each category.

Read more:
Invoke-RestMethod (docs.microsoft.com)

UPDATE
Added a sample project:
rest-api-request.morph (1.3 KB)

UPDATE 1/23/2019
We’ve added a few actions to deal with REST APIs without scripting in PowerShell. See the tutorial here: Tutorial: Web requests

2 Likes

Hi,

Trying to call an API do extract the data that will be transformed in Easymorph. Unfortunately, the data volume can be rather large until I get delta extracts working. So my question is:

Is there a limitation as to how much data can reasonably be returned from an API call?

Thanks,

Chris Martin

Hi Chris,

I believe it should handle up to low millions of records without problems but I didn’t test it on such data volume.

Hi Dmitry,

The situation I am working with is that I need to call an API which will return a csv file which I will then transform and write to a SQL database.

The call looks something like this:

https://mydomain//api/analytics/extract/f0b1ad0a-e589-413e-bf9d-3fdaf7590df8.csv?start=1514764800&type=completion

This call returns my file which I then need to process.

This will be the file name: f0b1ad0a-e589-413e-bf9d-3fdaf7590df8.csv

I see the action for the API call, but I want to know if this is possible to do at this time?

Thank you,

Chris Martin

Hi Chris,

just to make sure I understand your question – are you asking if it’s possible to download a file via an API call from PowerShell in EasyMorph?

Hi Dmitry,

Yes. the API call will give me the closest to real time data while reducing points of failure. The scenario would be:

Morph action calls API via powershell
Data comes in as a csv file
Then processing flows as normal

Thanks,

Chris Martin

In this case you can use Invoke-WebRequest with the -OutFile option.

Example

Invoke-WebRequest -Uri "https://easymorph.com/feed.rss" -OutFile "C:\Users\Dmitry\Desktop\feed.xml"

In your case it would be:

$fname="f0b1ad0a-e589-413e-bf9d-3fdaf7590df8.csv"
$uri="https://mydomain//api/analytics/extract/$($fname)?start=1514764800&type=completion"
Invoke-WebRequest -Uri $uri -OutFile $fname
1 Like

Hi Dmitry,

This looks great! Thank you so much!

Chris

Hi all,
Not sure if to post here or open new thread, but would be great to have an example built with

https://jsonplaceholder.typicode.com/.

I tried to run a rest api call following this thread, but I always encounter errors.

Thank you.
Jorge

Hi Jmarques, can you try this:

$headers = @{}
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$response = Invoke-RestMethod -Method GET -Headers $headers -Uri https://my-json-server.typicode.com/typicode/demo/comments -ContentType "application/json; charset=utf-8"

foreach($enum in $response.GetEnumerator())
{ 
    Write-Output ("id=" + $enum.id.ToString() + ", body=" + $enum.body +", postId="+$enum.postId.ToString())
}

Hi Dmitry,

I want to call an API to return adress information. I have to pass streetnames, housenumbers, etc. in the request. The request has the form below. When I write a small script in powerShell it is returning data. However when I copy over in EasyMorph powershell action, I don’t get results. Also, the ampersands ‘&’ in the url seem to be a problem because it is a reserved keyword.

Could you provide the correct way to call the example below ? It is a real example.

https://basisregisters.vlaanderen.be/api/v1/adresmatch?adresMatchRequest.gemeentenaam=Gent&adresMatchRequest.straatnaam=Hoogstraat&adresMatchRequest.huisnummer=1

Following code runs fine in powershell editor but not in EasyMorph

$url = 'https://basisregisters.vlaanderen.be/api/v1/adresmatch?adresMatchRequest.gemeentenaam=Gent&adresMatchRequest.straatnaam=Hoogstraat&adresMatchRequest.huisnummer=1'
$adres = Invoke-RestMethod $url -Method Get

$gebouweenheid_id =
$adres.adresMatches.adresseerbareObjecten |
select objectType, objectID |
Where-Object objectType -eq 'Gebouweenheid'

$gebouweenheid_id

Hi Nikolaas,

Try to use Write-Output cmdlet in EasyMorph. It seems that you have to explicitly output data which you want to capture in PowerShell action.

Thanks I shall try it out !

Hi Andrew,

It is not working. Output is empty whereas in the powershell IDE, it is returning data.

Nikolaas, you can capture objectId value by changing the last line of you script to the following:

$gebouweenheid_id.objectId

Ok thanks. I shall try that. I am quite new to powershell…

It works ! Thanks