API request to start a call from WordPress to the ...
# support
j
Using wordpress, I have a form that asks for information like clients name, location, phone number... When they click Call Now, the below API request should be made so that VAPI will call the given phone number. The VAPI assistant should also have information stored to use later in the call like the user's location. I keep getting errors just trying to start a call > function initiate_vapi_call($phone_number) { > $api_url = 'https://api.vapi.ai/call'; // Replace with the correct VAPI.AI API URL > $api_key = 'KEY'; // Replace with your VAPI.AI API key > > // Prepare the request data > $data = [ > 'to' => [ > [ > 'type' => 'phone', > 'number' => $phone_number > ] > ], > 'from' => [ > 'type' => 'phone', > 'number' => 'PHONENUMBER' // Replace with your registered VAPI.AI caller ID > ], > 'ncco' => [ > [ > 'action' => 'talk', > 'text' => 'Hello....' > ] > ] > ]; > > // Send the POST request > $response = wp_remote_post($api_url, [ > 'method' => 'POST', > 'body' => json_encode($data), > 'headers' => [ > 'Content-Type' => 'application/json', > 'Authorization' => 'Bearer ' . $api_key, > ] > ]); > > // Check for errors > if (is_wp_error($response)) { > error_log('VAPI.AI Error: ' . $response->get_error_message()); > return false; > } > > $response_body = wp_remote_retrieve_body($response); > $response_data = json_decode($response_body, true); > > // Log the response for debugging (optional) > error_log('VAPI Response: ' . print_r($response_data, true)); > > // Check if the call was successful > return isset($response_data['status']) && $response_data['status'] === 'success'; > }
v
@jshmn It looks like you're on the right track with your WordPress function to initiate a VAPI call. However, there are a few things you might want to check to ensure that the API request is correctly formatted and sent: 1. Make sure that the
$api_url
is set to the correct endpoint for initiating calls with VAPI. The URL you've commented in the code should be replaced with the actual endpoint provided by VAPI. 2. Replace
'KEY'
with your actual VAPI API key. Ensure that this key is kept secure and not exposed in your codebase. 3. Replace
'PHONENUMBER'
with the phone number you've registered with VAPI as your caller ID. 4. The structure of the request body should match the expected schema by VAPI. Make sure that the
to
,
from
, and
ncco
fields are structured as per the VAPI documentation. 5. Ensure that your server is able to make outbound HTTP requests. Some hosting environments restrict outbound HTTP requests, which could cause the
wp_remote_post
function to fail. 6. Check the response body for any error messages or codes that could give you more information about why the call is not being initiated successfully. 7. Lastly, ensure that the phone number format is correct and includes the country code if necessary. If you've checked all of these and are still encountering errors, it would be helpful to know the specific error messages you're receiving. This information can guide us to a more precise solution. Remember to handle user data securely and comply with any relevant data protection regulations when storing or transmitting personal information.
You can continue this conversation by mentioning me in the message.
j
@User How should the request body be structured and where is the documentation to follow?
s
@jshmn please check this docu on making phone call using API https://docs.vapi.ai/api-reference/calls/create
2 Views