palantir-foundry

Palantir Foundry - Making http request and capturing JSON response


I have been trying and failing to write a backend function in typescript that performs an http request and captures a JSON response.

I get an error messsage that starts like this:

"{
 "errorCode": "INVALID_ARGUMENT",
 "errorInstanceId": "bf2e359b-5202-4c1f-88f9-d4d5105e3cd8",
 "errorName": "Functions:CompileFailed",
 "parameters": {
  "stdout": "./node_modules/node-fetch-native/dist/index.mjs 1:650\nModule parse failed: Unexpected token (1:650)"

This is the code I am attempting:

import fetch from "node-fetch-native";
import { Function, Integer, Double, Float,OntologyEditFunction , Edits, DoublePropertyBaseType, GeoPoint  } from "@foundry/functions-api";

export class MyFunctions {
    @OntologyEditFunction()
    async fetchTestData(url: string): Promise<void> {
        try {
        const response = await fetch(url);
        if (!response.ok) {
            throw new Error(`HTTP error! Status: ${response.status}`);
        }
        const data: MyExpectedData = await response.json();
        console.log(data);
        } catch (error) {
            console.error('Error fetching data:', error);
        }
    }
}

Really appreciate any help with this.


Solution

  • You can't call APIs from Foundry Functions directly with the above code.

    This is not yet generally available, but you will soon be able to call Webhooks from Foundry Functions. The behavior should be similar as for External Transforms where you enable "External Systems" and then call reuse/call webhooks setup in Data Connection.