Fetch works in App, but not in Automation

I have a script that makes a request to a GIS API and fetches the response. When the script is in an app, it works at expected. As an automation, it does not work. The fetch returns a response with 403 status. This is strange because this means access is forbidden, however it was not forbidden when this script was an app.

Here is what I have in the automation script:


    const geocode_data = { "SingleLine": addressText,
                                    "f": "json",
                                    "outSR": {"wkid":102100,"latestWkid":3857},
                                    "outFields":"*",
                                    "distance": 50000,
                                    "location" : {"x": -8396932.14535, "y": 4854564.864250004, "spatialReference":{"wkid":102100}},
                                    "maxLocations": 2};

            const geocode_req = new Request(
               "https://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer/findAddressCandidates",
                {
                    method: "POST",
                    body: geocode_data,
                },
            )
           
            // This returns 403 forbidden 
            const geocode_res = await fetch(geocode_req);

I know there are differences between fetch() in apps vs automations, but from the documentation it seems like I can use fetch() like this. Is my geocode_data incorrectly formatted? In the app version, I use a FormData object for geocode_data, but I know that is not supported in automations. I referenced this page Airtable Scripting, which is a little unclear so I’m looking for more help!

Thanks!

Welcome to the community, @Erin_Ettenger! :smiley: I’ve never used new Request to build a request before calling an API using fetch. I just call fetch directly. Not sure if that’s part of the problem or not. Also, I noticed that you’re directly passing your data object instead of converting it to a JSON string first using JSON.stringify() and specifying the JSON content in a header property, which may also be an issue. Why the Scripting app let these slide and an automation scripting action doesn’t is a mystery.

Try this alternate version with these changes applied.

const geocode_data = { "SingleLine": addressText,
    "f": "json",
    "outSR": {"wkid":102100,"latestWkid":3857},
    "outFields":"*",
    "distance": 50000,
    "location" : {"x": -8396932.14535, "y": 4854564.864250004, "spatialReference":{"wkid":102100}},
    "maxLocations": 2
};

const geocode_res = await fetch("https://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer/findAddressCandidates", {
    method: "POST",
    headers: {"Content-Type": "application/json"},
    body: JSON.stringify(geocode_data)
});

Thanks so much, Justin! I implemented your suggestions - they make sense to me. I think they got me closer to the solution, but I am still unfortunately getting this 403 error. I wonder if there are different permissions parameters for running an Automation vs an app?

The only difference that I know for certain is that scripts in the Scripting app run on your local system, whereas scripts in automations run on Airtable’s servers. It’s possible that this difference is somehow tied to the error. I recommend reaching out to Airtable support directly (in the app: Help → Contact support) to see if they can shed more light on the problem.