What is in the custom tool request body call? Erro...
# support
b
@Vapi Support Bot I'm trying to figure out what does the json format look like in the body of the request from Vapi to my server endpoint. I might be missunderstanding something.... I'm getting this error before I can even log the body received on my server. so the request json model I thought I would recieve seems to be different: INFO: 100.64.0.3:33266 - "POST /api/get-questions HTTP/1.1" 422 Unprocessable Entity In the Webhook logs on Vapi, I can see a request body sent to my server, it looks like its basically the entire assistant config, is this correct? In any event, my server is expecting that, and if I send that same body to my endpoint via postman, it works, I can see the content logged in my server, and a successfull response In the call logs on Vapi I see : "Your server rejected
tool-calls
webhook. Error: Request failed with status code 422" I see the message logs shows gives : "result": "No result returned.", In the documentation https://docs.vapi.ai/tools/custom-tools It refers to the "**toolCallId (X): This is a unique identifier included in the initial request from Vapi.**" so this I assume should be sent in the body to my server? but it isnt there, if I look at the webhook logs only id is there. Maybe I'm not reading the docs correct but I dont see what the json body should look like that contains toolCallId so that my endpoint can read it and send it back. (I hope I understood that part...) Also I dont know what this means in the docs: * "Make sure to add “Tools Calls” in both the Server and Client messages and remove the function calling from it."* I've also tried enforcing strict parameter validation and async/sync options. still nothing. I've even tried using the json format of the message.... Any help would be appreciated.
s
The 422 Unprocessable Entity error you're getting indicates that the JSON body format your server is expecting doesn't match what Vapi is sending. Let me explain what's happening and how to fix it: ### What Vapi Sends in the Custom Tool Request When Vapi makes a request to your server endpoint for a custom tool, it sends a JSON body with this structure:
Copy code
json
{
  "message": {
    "type": "tool-calls",
    "toolWithToolCallList": [...],
    "timestamp": "...",
    "artifact": {...},
    "assistant": {...},
    "customer": {...},
    "call": {...},
    "toolCallList": [
      {
        "type": "function",
        "function": {
          "name": "your_function_name",
          "arguments": {...}
        },
        "id": "call_xyz123..." 
      }
    ]
  }
}
The important parts here are: 1. The message is nested under a "message" property 2. The
toolCallId
you need to respond with is actually found as
id
in the
toolCallList
array 3. The entire assistant configuration is included as context ### What Your Server Should Respond With Your server needs to respond with a specific JSON format:
Copy code
json
{
  "results": [
    {
      "toolCallId": "call_xyz123...",  // The id from the toolCallList
      "result": "Your result here"     // The actual result of your function
    }
  ]
}
### How to Fix It 1. Update your server endpoint code to properly parse the incoming request from Vapi: - Extract the
id
from
message.toolCallList[0].id
to use as the
toolCallId
in your response - Process the function arguments from
message.toolCallList[0].function.arguments
2. Ensure your response follows the exact format shown above with the
results
array containing an object with
toolCallId
and
result
properties. 3. Make sure your content types are correct - your server should expect
application/json
and respond with
application/json
. 4. Check for proper field validation - Make sure your server doesn't throw validation errors on the incoming JSON structure. ### The Note in the Documentation The documentation mentions "Make sure to add 'Tools Calls' in both the Server and Client messages and remove the function calling from it." This means: 1. You need to configure your assistant to receive "tool-calls" message type in the server messages settings 2. You should configure your assistant to handle tool calls properly in the UI ## Example Server Code (Pseudocode) Here's a basic example of how your server endpoint should handle the request:
Copy code
javascript
app.post('/api/get-questions', (req, res) => {
  try {
    // Extract the tool call ID from the request
    const toolCallId = req.body.message?.toolCallList?.[0]?.id;
    
    if (!toolCallId) {
      console.error('No toolCallId found in request');
      return res.status(400).json({ error: 'Missing toolCallId' });
    }
    
    // Extract the function arguments
    const functionArgs = req.body.message?.toolCallList?.[0]?.function?.arguments;
    
    // Process the function call with your business logic
    // ...your logic here...
    
    // Return the result in the expected format
    return res.status(200).json({
      results: [
        {
          toolCallId: toolCallId,
          result: "Your processed result here"
        }
      ]
    });
  } catch (error) {
    console.error('Error processing tool call:', error);
    return res.status(500).json({ error: 'Internal server error' });
  }
});
This should help you resolve the 422 Unprocessable Entity error and get your custom tool working correctly with Vapi.
b
Hi @Shubham Bajaj! this is an awesome breakdown. thank you!! I will let you know how it goes!
Hey @Shubham Bajaj, that worked like a charm, thanks for the help! sorry for the delay updating you.
a
Marking this ticket as Solved ✅
2 Views