Tutorials & Examples Web-help Blog

Tip: How to call a REST API from EasyMorph


#1

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)


Importing JSON files
Formula for currency converter
Get data from RabbitMQ
#2

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


#3

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.


#4

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


#5

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?


#6

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


#7

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

#8

Hi Dmitry,

This looks great! Thank you so much!

Chris