Backend issue
# support
s
Hi, Just wanted to ask that Vapi can retireve an information through make.com as shown in the video on the website when it retirves the current time from make.com and tells time to the caller. How can I do the same thing from my backend. Thank you
v
@Shrey Hello! Yes, Vapi can indeed retrieve information from external services like make.com (formerly Integromat) and provide that information to the caller. To achieve this, you would typically set up a webhook in your backend that Vapi can call during a voice interaction. When Vapi hits this webhook, your backend can then make a request to make.com to retrieve the current time or any other information you need, and then return that data to Vapi in a format it can use to relay the information to the caller. Here's a high-level overview of the steps you would follow: 1. **Set up a webhook on your backend**: This endpoint will handle requests from Vapi. 2. **Configure make.com**: Set up a scenario in make.com that retrieves the current time or the information you want. 3. **Connect your backend to make.com**: When your webhook is called by Vapi, make an HTTP request to the make.com scenario to get the information. 4. **Respond to Vapi**: Format the information retrieved from make.com and send it back to Vapi in the response to the webhook call. 5. **Configure Vapi**: Set up your Vapi call flow to include an action that calls your backend webhook at the appropriate time during the call. If you need specific guidance on how to set up the webhook or how to configure Vapi to use the webhook, please refer to the Vapi documentation or ask support for more detailed instructions.
You can continue this conversation by mentioning me in the message.
s
Ok. So I can't do it independentlly from my backend? Is it necessary to link my backend to make.com? Thank you
@Vapi
v
@Shrey Apologies for any confusion. You can certainly retrieve information independently from your backend without involving make.com or any other third-party service. If you have the capability to fetch the current time or any other data directly within your backend, you can simply set up a webhook that Vapi can call, and your backend can respond with the required information. Here's how you can do it: 1. **Create a webhook endpoint**: Develop an endpoint in your backend that can handle HTTP requests from Vapi. 2. **Fetch the data**: Within this endpoint, write the logic to retrieve the current time or the data you need directly within your backend. 3. **Respond to Vapi**: Return the data in a format that Vapi expects, so it can be spoken or used in the call flow. By doing this, you eliminate the need for any third-party service like make.com, and you keep the entire process within your control. If you have the necessary data and logic already in your backend, this can be a more straightforward and efficient approach. If you need further assistance with setting up your backend to work with Vapi, please let me know!
s
Can you explain me briefly how can I do it? A code snippet may help
@Vapi
v
@Shrey Certainly! Below is a simplified example of how you might set up a webhook endpoint in a Node.js Express backend to retrieve the current time and respond to a Vapi request. This example assumes you're using the Express framework for Node.js.
Copy code
javascript
const express = require('express');
const app = express();

app.use(express.json());

app.post('/get-current-time', (req, res) => {
  // Retrieve the current time
  const currentTime = new Date().toLocaleTimeString();

  // Format the response in a way that Vapi can use
  // This will depend on Vapi's expected response format
  const response = {
    time: currentTime
  };

  // Send the response back to Vapi
  res.json(response);
});

const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
  console.log(`Server is running on port ${PORT}`);
});
In this example, when Vapi sends a POST request to your
/get-current-time
endpoint, your server will calculate the current time, format it into a JSON response, and send it back. You would need to replace the response format with whatever JSON structure Vapi expects. Please note that this is a very basic example. In a production environment, you would need to consider security (e.g., authentication to ensure only Vapi can call your endpoint), error handling, and possibly more complex logic depending on what data you're retrieving and how you're processing it. Once your backend is set up, you would configure your Vapi call flow to make a webhook call to this endpoint at the appropriate time, and then use the response in your voice application. If you need more specific details or have any other questions, feel free to ask!
s
I am using python and fast api please help me with that @Vapi
v
@Shrey Certainly! Below is an example of how you might set up a webhook endpoint using FastAPI in Python to retrieve the current time and respond to a Vapi request:
Copy code
python
from fastapi import FastAPI
from datetime import datetime
import json

app = FastAPI()

@app.post("/get-current-time")
async def get_current_time():
    # Retrieve the current time
    current_time = datetime.now().strftime("%H:%M:%S")

    # Format the response in a way that Vapi can use
    # This will depend on Vapi's expected response format
    response = {
        "time": current_time
    }

    # Send the response back to Vapi
    return json.dumps(response)

# Run the server on port 8000
if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=8000)
In this example, when Vapi sends a POST request to your
/get-current-time
endpoint, your FastAPI application will calculate the current time, format it into a JSON response, and send it back. The
json.dumps(response)
is used to ensure the response is in JSON format, which is what Vapi would expect. To run this FastAPI application, save the code to a file (e.g.,
main.py
) and run it using Uvicorn, which is an ASGI server for Python. You can install Uvicorn using pip if you haven't already:
Copy code
bash
pip install fastapi uvicorn
Then, you can start your FastAPI application with:
Copy code
bash
uvicorn main:app --reload
The
--reload
flag enables auto-reloading of the server when you make changes to the code. Remember to configure your Vapi call flow to make a webhook call to this endpoint at the appropriate time, and then use the response in your voice application. If you have any further questions or need additional assistance, please let me know!
s
What if I want to send some other information say patient data like name scheduled time and date
@Vapi
@Vapi Please reply. Thank you
@Vapi Hi, I tried this method, but Vapi wont fetch time
v
@Shrey I apologize for the inconvenience. To troubleshoot the issue, we need to ensure that: 1. Your FastAPI endpoint is correctly set up and accessible over the internet. 2. Vapi is configured to call your endpoint correctly. 3. The response format from your FastAPI endpoint matches what Vapi expects. Let's start by verifying that your FastAPI endpoint is working as expected. You can do this by making a test request to your endpoint using a tool like
curl
or Postman. Here's an example of how to test it with `curl`:
Copy code
bash
curl -X POST http://yourserver.com/get-current-time -H "Content-Type: application/json"
Replace
http://yourserver.com/get-current-time
with the actual URL where your FastAPI app is hosted. You should see a JSON response with the current time. If this works, the next step is to ensure that Vapi is configured to call your endpoint. You'll need to provide the correct URL in your Vapi call flow configuration and ensure that any required headers or parameters are included. Regarding the response format, Vapi might expect a specific JSON structure. Ensure that your FastAPI endpoint sends back the data in the correct format. If you're unsure about the format Vapi expects, please refer to the Vapi documentation or contact their support for the exact specification. If you've confirmed that your endpoint works and the issue persists, please provide more details about how you've configured Vapi to call your endpoint, and I'll do my best to assist you further. If you're unable to resolve the issue, you may need to reach out to Vapi support for more specific guidance.
s
@Vapi It fetches time in this format:"{\"time\": \"08:53:37\"}"
v
@Shrey The format you've provided indicates that the time is being returned as a JSON string with escaped quotes. This is a correct JSON response, but depending on how Vapi processes the response, you might need to ensure that the JSON is not double-encoded or incorrectly formatted. In FastAPI, you typically don't need to manually convert your Python dictionary to a JSON string because FastAPI will automatically convert it when you return a dictionary from an endpoint. If you're using
json.dumps(response)
in your FastAPI code, you might be double-encoding the JSON, which could cause issues. Here's how you should return the response from your FastAPI endpoint:
Copy code
python
from fastapi import FastAPI
from datetime import datetime

app = FastAPI()

@app.post("/get-current-time")
async def get_current_time():
    # Retrieve the current time
    current_time = datetime.now().strftime("%H:%M:%S")

    # Format the response in a way that Vapi can use
    response = {
        "time": current_time
    }

    # FastAPI automatically converts the dictionary to a JSON response
    return response
With this code, when you return a Python dictionary from the endpoint, FastAPI will handle converting it to a JSON response with the correct headers (
Content-Type: application/json
). If you're still having issues, please ensure that: 1. The endpoint URL in your Vapi configuration is correct. 2. Vapi is set up to handle the JSON response properly. 3. There are no network issues preventing Vapi from reaching your FastAPI server. If the problem persists, you may need to consult Vapi's documentation or support to understand the exact requirements for the response format and how to configure the call flow to handle the webhook response.
s
@Vapi So, the assistant says, I am having issue fetching the time. WHat I observed in make.com procedure was that it also gets the toolid and enters it along with the response, can it be something that is affecting the proces with my backend?
v
@Shrey If the process with make.com includes a tool ID as part of the response, and Vapi expects this tool ID to be present in the response from your webhook, then yes, this could be affecting the process with your backend. In Vapi or similar systems, a tool ID or some other identifier might be used to correlate the response with a specific part of the call flow. If Vapi is expecting a specific format that includes a tool ID, you will need to include this in your response. Here's an example of how you might modify your FastAPI endpoint to include a tool ID in the response:
Copy code
python
from fastapi import FastAPI
from datetime import datetime

app = FastAPI()

@app.post("/get-current-time")
async def get_current_time():
    # Retrieve the current time
    current_time = datetime.now().strftime("%H:%M:%S")

    # Assume 'tool_id' is a parameter passed by Vapi in the request body
    # You would extract it and include it in the response
    request_data = await request.json()
    tool_id = request_data.get('tool_id')

    # Format the response in a way that Vapi can use, including the tool ID
    response = {
        "time": current_time,
        "tool_id": tool_id  # Include the tool ID in the response
    }

    return response
In this example, the
tool_id
is extracted from the incoming request and included in the response. You would need to adjust the code to match the actual parameter names and data structure that Vapi is using. Please ensure that you're following the expected schema for the response that Vapi requires. If you're unsure about the schema, you should refer to the Vapi documentation or contact their support for the exact specification. If you continue to face issues, it would be helpful to have more details about the expected request and response structure from Vapi, including any required identifiers like a tool ID.
s
Go through following docs in order. [1] https://docs.vapi.ai/server-url/events [2] https://api.vapi.ai/api-json# > components > schemas> ServerMessage
Basically your supposed to return the response to the server event request in required format.