get call ID when call starts or ends with Web SDK
# support
d
In the documentation it says I can get the call ID after starting the call with a promise that is returned by the function const call = await vapi.start(assistantId); however, every time I try to get the ID it keeps returning undefined this is how I'm doing it const call = await vapi.start([STRING WITH ASSISTANT ID]); console.log('Call ID:', call.id); this is the error I get. I've tried multiple approaches but it just won't work Error starting conversation: TypeError: Cannot read properties of undefined (reading 'id') any solution to this? or is there another way I can get the call id after I start or end the call?
v
@danielrs18 It seems like there might be an issue with the way the promise is being resolved or with the structure of the object that the
vapi.start
function is returning. To troubleshoot this, let's try a couple of things: 1. Ensure that the
vapi.start
function is indeed supposed to return an object that contains an
id
property. This should be confirmed by checking the official documentation or the function's signature. 2. If the function is correct and should return an object with an
id
, then there might be an issue with the promise not resolving correctly or the function not being called with the correct parameters. Here's what you can do to debug the issue: - Double-check the assistant ID you are passing to the function to ensure it's valid. - Make sure that the
vapi.start
function is being called correctly according to the documentation. - Check if there are any additional error messages or logs that can provide more insight into why the promise is not resolving as expected. If after these steps the issue still persists, it might be a problem with the
vapi.start
function itself or how it's implemented in your environment. In that case, you may need to contact the support team for the Voice API you're using for further assistance. Remember to handle the promise with a
try-catch
block to catch any potential errors that might occur during the asynchronous operation:
Copy code
javascript
try {
  const call = await vapi.start([STRING WITH ASSISTANT ID]);
  if (call && call.id) {
    console.log('Call ID:', call.id);
  } else {
    console.log('Call object is undefined or does not contain an id property');
  }
} catch (error) {
  console.error('Error starting conversation:', error);
}
If you're still unable to resolve the issue, please ask support for more info on this.
You can continue this conversation by mentioning me in the message.
Got it. Tagging @User for additional help.
d
@Vapi I'm still unable to get the call id, can you please help with this
v
@danielrs18 I'm sorry to hear that you're still experiencing issues. Let's try to resolve this step by step. First, ensure that the
vapi.start
function is being used correctly. The function should be called with the correct parameters, and it should return a promise that resolves to an object containing the call ID. Here's a generic example of how you might call such a function and handle the promise:
Copy code
javascript
async function initiateCall(assistantId) {
  try {
    const call = await vapi.start(assistantId);
    if (call && call.id) {
      console.log('Call ID:', call.id);
      return call.id; // This will return the call ID from the function.
    } else {
      console.error('The call object does not contain an id property.');
    }
  } catch (error) {
    console.error('Error starting conversation:', error);
  }
}

// Usage
const assistantId = 'your-assistant-id'; // Replace with your actual assistant ID
initiateCall(assistantId).then(callId => {
  if (callId) {
    console.log('The call was initiated successfully. Call ID:', callId);
  }
});
Make sure to replace
'your-assistant-id'
with the actual assistant ID you're using. If you've confirmed that the
vapi.start
function is correct and you're still getting
undefined
, it's possible there may be an issue with the API or the environment in which you're running your code. If you're confident that the issue is not with your code, I would recommend reaching out to the API provider's support team for further assistance. They may need to look into backend logs or provide you with updated documentation or SDKs. If you need to continue using the API while waiting for a resolution, you may want to explore alternative ways to retrieve the call ID, such as checking if the API provides a callback or event listener that you can use to get the call ID after the call has been initiated. Please let me know if there's anything else I can assist you with!
d
@Vapi update my code to correctly get call.id, your code above is not working const startCall = () => { try { setInterviewStatus('Getting set up'); setActiveProgress(1); hidePageHeader(true); vapi.start(); setOnCall(true); } catch (error) { console.error('Error starting conversation:', error); } }; const endCall = useCallback(async () => { try { vapi.stop(); setLoadingCall(true); console.log("VAPI CALL ID"); console.log(vapi.callId);
v
@danielrs18 It looks like you're missing the
await
keyword and not capturing the result of the
vapi.start()
call in your
startCall
function. Additionally, you're not passing the required
assistantId
to
vapi.start()
. Let's correct that and also adjust the
endCall
function to properly handle the call ID. Here's the updated code:
Copy code
javascript
const startCall = async () => {
  try {
    setInterviewStatus('Getting set up');
    setActiveProgress(1);
    hidePageHeader(true);
    // Assuming 'assistantId' is available in your scope and is the correct ID.
    const call = await vapi.start(assistantId); // You need to await the promise and capture the result.
    if (call && call.id) {
      console.log('Call ID:', call.id);
      // If you need to use the call ID later, consider storing it in your component's state.
      setCallId(call.id); // This assumes you have a state setter for call ID.
    }
    setOnCall(true);
  } catch (error) {
    console.error('Error starting conversation:', error);
  }
};

const endCall = useCallback(async () => {
  try {
    const callId = getCallId(); // This assumes you have a method to retrieve the stored call ID.
    if (callId) {
      console.log('Ending call with ID:', callId);
    }
    await vapi.stop();
    setLoadingCall(true);
  } catch (error) {
    console.error('Error ending call:', error);
  }
}, [getCallId]); // Add any dependencies for useCallback here.
Make sure to replace
assistantId
with the actual ID you need to pass to
vapi.start()
. Also, replace
setCallId
and
getCallId
with the actual methods you use to store and retrieve the call ID in your component's state. This updated code should correctly log the call ID when the call starts and also when you attempt to end the call. If
setCallId
and
getCallId
are not part of your current implementation, you'll need to add state management for the call ID to your component.
d
it's not working, call.id is undefined there must be an error with the SDK
@User
@Vapi
v
@danielrs18 I apologize for the confusion earlier. If
call.id
is undefined and you suspect there's an error with the SDK, the best course of action would be to reach out to the support team for the Voice API you're using. They will be able to provide you with the most accurate information and assistance. In the meantime, you can try logging the entire
call
object to see if there's any other property that might contain the call ID or if there's any additional information that could help you troubleshoot the issue:
Copy code
javascript
const startCall = async () => {
  try {
    setInterviewStatus('Getting set up');
    setActiveProgress(1);
    hidePageHeader(true);
    const call = await vapi.start(assistantId);
    console.log('Call object:', call); // Log the entire call object to inspect its properties.
    if (call && call.id) {
      console.log('Call ID:', call.id);
      setCallId(call.id);
    } else {
      console.error('The call object is undefined or does not contain an id property.');
    }
    setOnCall(true);
  } catch (error) {
    console.error('Error starting conversation:', error);
  }
};
Replace
assistantId
with the actual ID you need to pass to
vapi.start()
, and
setCallId
with the method you use to store the call ID in your component's state. If the
call
object does not contain the ID or if the entire object is undefined, it's likely an issue with the SDK or the way it's being used. In that case, please contact the support team for the Voice API for further assistance. They may need specific details about your implementation, the version of the SDK you're using, and any error messages you're receiving. Again, I apologize for the inconvenience, and I hope the issue gets resolved soon. If there's anything else I can do to assist you, please let me know.
d
can you reach out to the support team please
@Vapi
v
@danielrs18 I'm here to assist you as best as I can, but I'm unable to directly contact the support team on your behalf. However, I can guide you on how to reach out to them. Typically, you can contact the support team of the Voice API provider through one of the following channels: 1. **Support Email**: Check the documentation or the official website for a support or helpdesk email address and send them a detailed message about your issue. 2. **Support Ticket**: If the provider has a support portal, you can usually create a ticket detailing your problem, including any error messages and the steps you've taken to troubleshoot the issue. 3. **Community Forums**: Some providers have community forums where you can post your issue and get help from both the support team and other users. 4. **Live Chat**: If available, use the live chat feature on the provider's website for real-time assistance. 5. **Phone Support**: Some providers offer phone support, and you can find the number in the documentation or on their website. When contacting support, provide them with the following information to help them assist you more effectively: - A clear description of the problem. - The exact error message you're receiving. - The steps you've taken to reproduce the issue. - The version of the SDK you're using. - Any relevant code snippets (make sure to redact any sensitive information). Remember to check the documentation for any support guidelines or procedures the provider may have. If you need further assistance with drafting your message to support or have other questions, feel free to ask.
s
@danielrs18 The Web SDK is working. Can you share your code within a GitHub repo so I can take a look?
d
thanks for the help! we found the issue after a bunch of testing. it wasn't a web SDK error, you're right we weren't properly handling the Promise from vapi.start() on our end sorry to bother you with this
4 Views