Stopping a call via API
# support
h
I am creating a call that lets two agents talk to each other for testing purposes. I want to also be able to end this call. Is there an interface for me to take the call_id and end the call?
v
@H dog Yes, there is typically an interface or an API endpoint that allows you to end a call using the call_id. You would need to send a request to the appropriate endpoint with the call_id as a parameter to terminate the call. However, to provide you with the exact steps or the function to call, I would need to know the specifics of the API or system you are using. If you are using VAPI, you would usually have a function or method that you can call to end the call. If you can provide me with the call_id and confirm that you are ready to end the call, I can assist you further with this process.
You can continue this conversation by mentioning me in the message.
h
I want to see some documentation or an example
c
no - not possible.
h
hm
so no way to end ongoing calls manually ?
c
you can't end calls at all if I am correct. Besides hanging up obviously.
h
ya
the thing is, I am using one AI agent to call another AI agent
i need to build a failsafe incase they start looping
c
I don't know why they would start looping, but what you are asking - I don't think is possible whatsoever.
s
you can use twilio api or sdk to end the call.
h
is there reference documentation ?
s
[1] checkout twilio docs. [2] also you can take reference from this code snippet.
Copy code
ts
export default async function DeleteHandler(req: Request, res: NextApiResponse) {
  if (req.method === "POST") {
    try {
      const body = await req.json();
      const { callSid, accountSid, authToken } = body
      if (!callSid) {
        throw new Error('No callSid provided')
      }
      const basicAuthToken = Buffer.from(`${accountSid}:${authToken}`).toString('base64')
      const response = await fetch(`https://api.twilio.com/2010-04-01/Accounts/${accountSid}/Calls/${callSid}.xml`, {
        method: 'POST',
        headers: {
          'Content-Type': 'application/x-www-form-urlencoded',
          'Authorization': `Basic ${basicAuthToken}`,
        },
        // body: 'Twiml=<?xml version="1.0" encoding="UTF-8"?><Response><Hangup/></Response>',
        body: new URLSearchParams({ Status: 'completed' }),

      })

      if (!response.ok) {
        throw Error(
          `Couldn't Hang Up Twilio Call. Status: ${response.status}`
        );
      }

      return NextResponse.json(
        { message: "Succesfully hangup outbound call" },
        { status: 200 }
      );
    } catch (error: any) {
      return NextResponse.json(
        {
          message: "Failed to hangup outbound call",
          error: error?.message,
        },
        { status: 500 }
      );
    }

  } else {
    res.setHeader('Allow', ['POST']);
    res.status(405).end(`Method ${req.method} Not Allowed`);
  }
}
7 Views