Skip to content
Last updated

This guide shows how to make a Text-to-speech phone call in a .NET application using the Voice API and .NET SDK.

Note:

Before you can get started, you need the following already set up:

Step 1. Set up your ASP.NET Core application

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:

  1. Open a command prompt or terminal to the local repository folder.
  2. Execute the following command:
    dotnet add package Sinch

Modify your application

In your project folder, open the Program.cs file and paste the provided "Program.cs" code into the file, replacing all the existing content.

Program.cs
// 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.

Initialize the client

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.

Note:

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.

Initialize client
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:

Initialize client for only Voice
using Sinch;

var sinch = new SinchClient(default, default, default);

var voice = sinch.Voice("YOUR_application_key", "YOUR_application_secret");

Set your Destination number parameter

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.

Note:

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.

Step 2. Make your phone call

  1. 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.

Next steps

Now that you know how to make a call, learn how to handle an incoming call.

Additional resources