rare-sapphire
rare-sapphire2w ago

Vapi Client using C#

I'm trying to implement a Vapi client using the c# nuget package for .net. I am able to connect our application to the service, however I can't figure out how to start a call. Vapi seems to provide a .start() method to other platforms, but I can't find the equivalent for the .net environment.
1 Reply
Sahil
Sahil2w ago
Hi, To start a call using the Vapi client in a .NET environment, you can use the Vapi API through HTTP requests, as the direct .start() method used in other environments might not be available in the .NET package. Here are the steps to make an outbound call: 1. Get your Vapi API key: Ensure you have your API key ready from the Vapi dashboard. 2. Make an HTTP POST request: Use an HTTP client to call the API endpoint with the necessary headers and payload. Example using C# and HttpClient:
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

public async Task StartCall(string apiKey, string assistantId, string phoneNumberId, string customerNumber)
{
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Add("Authorization", $"Bearer {apiKey}");
var requestData = new
{
assistantId = assistantId,
customer = new { number = customerNumber, numberE164CheckEnabled = false },
phoneNumberId = phoneNumberId
};

var json = Newtonsoft.Json.JsonConvert.SerializeObject(requestData);
var content = new StringContent(json, Encoding.UTF8, "application/json");

var response = await client.PostAsync("https://api.vapi.ai/call/phone", content);
response.EnsureSuccessStatusCode();

var responseString = await response.Content.ReadAsStringAsync();
Console.WriteLine("Call started: " + responseString);
}
}

using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

public async Task StartCall(string apiKey, string assistantId, string phoneNumberId, string customerNumber)
{
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Add("Authorization", $"Bearer {apiKey}");
var requestData = new
{
assistantId = assistantId,
customer = new { number = customerNumber, numberE164CheckEnabled = false },
phoneNumberId = phoneNumberId
};

var json = Newtonsoft.Json.JsonConvert.SerializeObject(requestData);
var content = new StringContent(json, Encoding.UTF8, "application/json");

var response = await client.PostAsync("https://api.vapi.ai/call/phone", content);
response.EnsureSuccessStatusCode();

var responseString = await response.Content.ReadAsStringAsync();
Console.WriteLine("Call started: " + responseString);
}
}

Make sure to replace "YOUR_VAPI_PRIVATE_API_KEY", "YOUR_ASSISTANT_ID", "YOUR_PHONE_NUMBER_ID", and "CUSTOMER_PHONE_NUMBER" with your actual data. This will initiate a call through your Vapi setup.

Can you also provide any available CallerID or sessionID?

Did you find this page helpful?