How to pass function call response to assistant
# support
a
Hi I am based on the git example https://github.com/VapiAI/client-side-example-javascript-react and I'm trying to handle the response to my custom function call. I hit an endpoint on my server, I get a response and I have to deal with it and pass it on to the assistant. I can display the response to the console log, but nothing else. It was suggested to me to add the json return below. But it didn't work. Any tips are welcome useEffect(() => { const onMessageUpdate = (message: Message) => { if (message.type !== MessageTypeEnum.FUNCTION_CALL) return; if (message.functionCall.name === "availability") { const params = message.functionCall.parameters; getAvailability(params); } }; const getAvailability = async (data: AvailabilityDto) => { let api: BookingApi = new BookingApi(); let availability: string = await api.getAvailability(data); console.log("availability", availability); return { "result": availability }; } vapi.on("message", onMessageUpdate); return () => { vapi.off("message", onMessageUpdate); }; }, []);
v
Hi @ArqadiushS, I'm an AI assistant for Vapi. I'll analyze available sources and get back to you shortly.
----------------------------- **While I generate an answer, here are some sources that might be helpful**: * [Server Events]() * [Outbound Sales Example]() * [Function Calling]() * [Create Assistant]() -----------------------------
### Answer To pass the response from your custom function call back to the assistant in Vapi, you need to ensure that the server handling the function call responds correctly to the assistant's request. When your assistant triggers a function call, it expects a response in a specific format so that it can continue the conversation or take necessary actions based on the response. Based on the official Vapi documentation, when a function is called, your server should respond with a JSON object that contains the result of the function call. The assistant will then decide what to do with the response based on its system prompt [(1)]() . Here's a general structure of how the response should look:
Copy code
json
{
  "result": "Your custom message or data here"
}
In your case, after obtaining the
availability
from your
BookingApi
, you should ensure that this information is sent back to Vapi in the expected format. However, your current implementation only logs the availability to the console and returns the response object locally within the
getAvailability
function. This local return does not communicate back to the assistant. To effectively respond to the assistant with the function call result, you need to implement server-side logic that captures the function call from the assistant, processes it (just as you've done with
getAvailability
), and then sends a HTTP response back with the desired
result
payload. Here's a simplified server-side example in Node.js, assuming you're using Express.js for handling HTTP requests:
Copy code
javascript
app.post('/your-function-endpoint', async (req, res) => {
    const data = req.body; // Assuming your parameters are sent in the body
    let api = new BookingApi();
    let availability = await api.getAvailability(data);
    res.json({"result": availability}); // Respond back to Vapi with the availability
});
This example assumes that you have a server endpoint (
/your-function-endpoint
) that Vapi calls when the
availability
function is triggered. The server then processes the request, retrieves the availability, and responds with the availability data in the format expected by Vapi. Remember, the interaction between your assistant and your server relies on HTTP requests and responses. Ensure your assistant's function configuration points to the correct server URL where your logic for handling function calls is implemented. For more detailed guidance on setting up your server to handle function calls and respond appropriately, please refer to Vapi's documentation on [Function Calling]().
s
@ArqadiushS can you please ask this question on Vapi Office Hour?
a
Sure, but what are Vapi Office Hour? And where should I ask... On vapi official website?