This guide shows how to make a Text-to-speech phone call in a .NET application using the Voice API and .NET SDK.
Before you can get started, you need the following already set up:
- Set all Voice API configuration settings.
- ASP.NET Core 7.0 or later SDK and ASP.NET Core Runtime and a familiarity with how to create an app.
- A mobile handset that can receive phone calls.
Create a new project folder and open a command prompt. Execute the following command to create a new ASP.NET Core console application:
dotnet new console
This creates a new console application and project.
The easiest way to install the SDK is using the dotnet
CLI:
- Open a command prompt or terminal to the local repository folder.
- Execute the following command:
dotnet add package Sinch
In your project folder, open the Program.cs
file and paste the provided "Program.cs" code into the file, replacing all the existing content.
// Use this code to make a Text-to-speech phone call using the Voice API and .NET SDK.
using System.Text.Json;
using Sinch;
using Sinch.Voice.Callouts.Callout;
var sinch = new SinchClient("YOUR_access_key",
"YOUR_access_secret",
"YOUR_project_id");
var voice = sinch.Voice("YOUR_application_key", "YOUR_application_secret");
CalloutResponse response = await voice.Callouts.Tts(new TextToSpeechCalloutRequest{
Destination = new Destination{
Type = DestinationType.Number,
Endpoint = "YOUR_phone_number"
},
Text = "This is a Text to speech callout to test the Sinch dotnet Voice SDK."
});
Console.WriteLine(JsonSerializer.Serialize(response, new JsonSerializerOptions()
{
WriteIndented = true
}));
The code provided includes placeholder parameters. You'll need to update the parameters detailed in the following subsections with your values.
To start using the Voice API with the SDK, you need to initialize the main client class with your credentials from your Sinch dashboard and additionally create a Voice client object that uses your Voice app credentials.
For testing purposes on your local environment it's fine to use hardcoded values, but before deploying to production we strongly recommend using environment variables to store the credentials.
using Sinch;
var sinch = new SinchClient("YOUR_access_key",
"YOUR_access_secret",
"YOUR_project_id");
var voice = sinch.Voice("YOUR_application_key", "YOUR_application_secret");
Or, if you only need to use Voice API:
using Sinch;
var sinch = new SinchClient(default, default, default);
var voice = sinch.Voice("YOUR_application_key", "YOUR_application_secret");
In this example you want to call a phone number. Change the value of the YOUR_phone_number
parameter to the phone number you verified in your dashboard in E.164 format.
When your account is in trial mode, you can only call your verified numbers. If you want to call any number, you need to upgrade your account!
Save the file.
- Now you can execute the code and make your phone call. Run the following command:
dotnet run
You should receive a phone call to your mobile handset with a voice speaking the words in the request. You should also see the JSON response in the console.
Now that you know how to make a call, learn how to handle an incoming call.