Fetching Json data from API

Hi I just want to fetch a standard text output from an API source with a URL, but I can’t see anything in the docs that’s much help to send an HTTP POST request , this isn’t ORC based and all the questions about API’s in the forum only seem to be about issues with that.

[GET] https://api.taapi.io/rsi?secret=MY_SECRET&exchange=binance&symbol=BTC/USDT&interval=1h

I simply need someway to store the following kind of result so I can do a source search on it {“value”:48202.687257633756,“valueAdvice”:“long”}

You can search the API response directly in the browser.

Here is a test macro. Since I do not have the API key, I search the JSON response for the word “Please”, which is part of the error message.

{
  "Name": "json-api",
  "CreationDate": "2021-8-30",
  "Commands": [
    {
      "Command": "open",
      "Target": "https://api.taapi.io/rsi?secret=MY_SECRET&exchange=binance&symbol=BTC/USDT&interval=1h",
      "Value": "",
      "Description": ""
    },
    {
      "Command": "sourceSearch",
      "Target": "Please",
      "Value": "a",
      "Description": ""
    },
    {
      "Command": "echo",
      "Target": "${a}",
      "Value": "green",
      "Description": ""
    }
  ]
}

my api : https://tempapi.proj.me/api/Zc2UfMdJZ
I try to search ‘Hello’ from above api but script not working.

Maybe this code snippet helps. I use executeScript to call the free OCR API from inside the RPA software. This approach works for any API.

For generating the Javascript code I used the PostMan code export feature. So if your api call works from inside Postman, than you can export the working code.

{
  "Name": "ocr api",
  "CreationDate": "2023-4-29",
  "Commands": [
    {
      "Command": "open",
      "Target": "https://forum.ui.vision",
      "Value": "",
      "Description": "Open any website, we need a place to run \"executeScript\" inside"
    },
    {
      "Command": "executeScript",
      "Target": "async function callOCR() {\nconst apiKey = \"helloworld\";\nconst imageurl = \"https://ui.vision/Content/Images/ui.vision.logo2.webp\";\nconst ocrUrl = \"https://api.ocr.space/parse/image\";\n\nvar myHeaders = new Headers();\nmyHeaders.append(\"apikey\", apiKey);\n\nvar formdata = new FormData();\nformdata.append(\"language\", \"ger\");\nformdata.append(\"ocrengine\", \"2\");\nformdata.append(\"url\", imageurl);\n\nvar requestOptions = {\n  method: 'POST',\n  headers: myHeaders,\n  body: formdata,\n  redirect: 'follow'\n};\n\nconst response = await fetch (ocrUrl, requestOptions)\n\nconst data = await response.json();\n\nparsed_text = data[\"ParsedResults\"][0][\"ParsedText\"]\n\nreturn JSON.stringify(parsed_text);\n\n}\nreturn callOCR();",
      "Value": "v",
      "Description": "Call OCR API, code was generated by Postman"
    },
    {
      "Command": "echo",
      "Target": "OCR Text result=${v}",
      "Value": "green",
      "Description": ""
    }
  ]
}

Postman code export:

1 Like

Hi @Plankton thanks so much for this! I have been searching for this for a while. I also just want to return the results of a basic API call.

Your code seems more complex than what I need, but I tried running your UI Vision JSON code exactly, but I got this error below.

I want to do exactly the same as the OP, but don’t want to open the API call in a new browser window as suggested by @ulrich I just want to return it and search for relevant text and save it in a variable in UI vision so I can use it later (e.g. as @ulrich suggested with a sourceSearch)

[status]
Playing macro testAPIcall
[info]
Executing:  | open | https://forum.ui.vision |  | 
[info]
Executing:  | executeScript | async function callOCR() {
const apiKey = "helloworld";
const imageurl = "https://ui.vision/Content/Images/ui.vision.logo2.webp";
const ocrUrl = "https://api.ocr.space/parse/image";

var myHeaders = new Headers();
myHeaders.append("apikey", apiKey);

var formdata = new FormData();
formdata.append("language", "ger");
formdata.append("ocrengine", "2");
formdata.append("url", imageurl);

var requestOptions = {
  method: 'POST',
  headers: myHeaders,
  body: formdata,
  redirect: 'follow'
};

const response = await fetch (ocrUrl, requestOptions)

const data = await response.json();

parsed_text = data["ParsedResults"][0]["ParsedText"]

return JSON.stringify(parsed_text);

}
return callOCR(); | v | 
[error]
Line 2: Error in executeScript code: Cannot read properties of undefined (reading '0')
[info]
Macro failed (Runtime 5.16s)

I saw this error as well, but after I cleaned my browser cache (Chrome → Clear Browsing Data) all worked fine again.

here is the same macro using variables, works for me:

{
  "Name": "api",
  "CreationDate": "2023-5-13",
  "Commands": [
    {
      "Command": "store",
      "Target": "fast",
      "Value": "!replayspeed",
      "Description": ""
    },
    {
      "Command": "open",
      "Target": "https://ui.vision",
      "Value": "",
      "Description": "Open any website, we need a place to run \"executeScript\" inside. Just do not use security-enhanced pages like Github.com which block external Javascript."
    },
    {
      "Command": "store",
      "Target": "helloworld",
      "Value": "apikey",
      "Description": ""
    },
    {
      "Command": "store",
      "Target": "https://bigimagesystems.com/wp-content/uploads/2023/01/avicii-museum-approved-for-some-jpg.webp",
      "Value": "myimage",
      "Description": "Link to the image to be OCR'ed "
    },
    {
      "Command": "executeScript",
      "Target": "async function callOCR() {\nconst apiKey = ${apikey};\nconst imageurl = ${myimage};\nconst ocrUrl = \"https://api.ocr.space/parse/image\";\n\nvar myHeaders = new Headers();\nmyHeaders.append(\"apikey\", apiKey);\n\nvar formdata = new FormData();\nformdata.append(\"language\", \"ger\");\nformdata.append(\"ocrengine\", \"2\");\nformdata.append(\"url\", imageurl);\n\nvar requestOptions = {\n  method: 'POST',\n  headers: myHeaders,\n  body: formdata,\n  redirect: 'follow'\n};\n\nconst response = await fetch (ocrUrl, requestOptions)\n\nconst data = await response.json();\n\nparsed_text = data[\"ParsedResults\"][0][\"ParsedText\"]\n\nreturn JSON.stringify(parsed_text);\n\n}\nreturn callOCR();",
      "Value": "v",
      "Description": "Call OCR API, code was generated by Postman"
    },
    {
      "Command": "echo",
      "Target": "OCR Text result=${v}",
      "Value": "green",
      "Description": ""
    }
  ]
}

PS: You do not need to open a new browser window. ExecuteScript will insert and run the JS in any web page that is currently loaded. Just avoid pages like Github that block external JS due to a strict Content Security Policy (CSP).

Thanks so much! This works now. Great to know you can pass the variable name.

@Plankton what am I doing wrong?

    {
      "Command": "executeScript",
      "Target": "async function test() {\n  const url = 'https://postman-echo.com/get?action=12345';\n\n  const response = await fetch(url);\n  const data = await response.text();\n\n  return data;\n}\nreturn test();",
      "Value": "codereturned",
      "Description": "test API"
    }

I get:
Error in executeScript code: Failed to fetch

I’ve tried restarting chrome/deleting cookies/deleting browser multiple times.

If you navigate to the link directly, it successfully returns echo data:
https://postman-echo.com/get?action=12345

Indeed, the postman-echo.com test api returns some error message:
Access to fetch at 'https://postman-echo.com/get?action=12345' from origin 'https://ui.vision' has been blocked by CORS policy: → That api does not like to be called from within a website it seems?

Anyway, for other “normal” APIs the fetch method works well. For example, here is my simple script to get my current IP address via GET API call:

{
  "Name": "GET API call",
  "CreationDate": "2023-8-15",
  "Commands": [
    {
      "Command": "open",
      "Target": "https://ui.vision",
      "Value": "",
      "Description": "Open any website, \"executeScript\" runs inside it"
    },
    {
      "Command": "executeScript",
      "Target": "async function callmyAPI() {\n  \nconst response = await fetch(\"https://api.ipify.org?format=json\");\n  \nconst data = await response.json();\n  \nresult = data[\"ip\"];\n  \nreturn JSON.stringify(result);\n}\nreturn callmyAPI();",
      "Value": "v",
      "Description": "Call GET API"
    },
    {
      "Command": "echo",
      "Target": "My IP address is =${v}",
      "Value": "blue",
      "Description": ""
    }
  ]
}

The executeScript JS code is:

async function callmyAPI() {
  
const response = await fetch("https://api.ipify.org?format=json");
  
const data = await response.json();
  
result = data["ip"];
  
return JSON.stringify(result);
}
return callmyAPI();

Edit: Initially it was a long post, removed it cuz was completely irrelevant, but tl:dr just had code and postman snippets, would not work no matter what. Returning Unexpected token.

I solved it by using executeScript instead of executeScript_Sandbox omg it was that simple! Thanks a lot!