Using API step's data in function step
Hi all!
I'm currently building a workflow where I get data using API step. I pass the data in to Function step and then I would like to handle data there. The end goal would be filter data by using user input and show the result in chat.
Example:
Bot: Which city you live in?
User: Stockholm
Bot: Here are the recycling points in Stockholm: adress 1, address 2 and address 3.
I currently manage to fetch data with API step and I can pass it in function step. But then the problems starts.
Is it possible to access apiData in function step and filter it? It's hard to degub this because I haven't found console in Voiceflow and some error are hard to catch.
apiData and recyclingPoints are predefined in API step.
// This gets full JSON
const apiData = args.inputVars.apiData;
//This gets first recycle point's street name
const streetName = args.inputVars.recyclePointStreet;
// User's input for question "Your city?"
const userCity = args.inputVars.userCity;
// Here I send data in to chat as a test
payload: {
message:
You said: "${userCity}" and "${streetName}",
},6 Replies
Sure. You use fetch() to get the api data in your function. A Function allows you to do anything you can do in JS (for the most part). For instance, you can't import outside packages.
Thanks for the answer! 🙂
I tried to fetch directly inside a function step but it brings error.
"message": "[ERROR]: Encountered an error in a Function step. Error :: content size at https://.... over limit: 1000000"
}
If I pass data from API step in to function step I do not know how to correctly handle data. With following way I get error about undefined:
const apiData = args.inputVars.apiData;
const firstPoint = apiData.recycling_points[0];
"message": "[ERROR]: Encountered an error in a Function step. TypeError :: Cannot read properties of undefined (reading '0')"
Looks like there is too much data
Try filtering it or limit the response
Thanks again and so fast answer!
I could try that filtering data before or while fetching it.
But it seems that when I use API step it can fetch all the data still? Is it possible to use that data in function step somehow?
I'm guessing this call takes a bit to load. You really should only return the number of rows you want to show to make it fast.
EXAMPLE: If amazon returned every shirt they had every request it would be millions of items. They only return like 50 at a time.
Thanks for clarifying! I think the problem was with our test JSON which was only static file. I think we need to setup real API and then fetch only needed data in function step using one of the methods.