As an outcome of this guide, you will send a WhatsApp text message using the Infobip WhatsApp API and the Infobip API C# SDK. The C# SDK targets .NET Standard 2.0, so you can use it with that version or any later version of .NET.

Prerequisites

Infobip account
• Working .NET environment

Difficulty level

This guide assumes very basic knowledge of C# and basic familiarity with APIs.

Summary of the steps

• Install the Infobip API C# SDK.
• Create an API Client instance using your Infobip credentials.
• Build your WhatsApp text payload.
• Use the the client’s WhatsApp attribute to send a text message.
• Wrap your code in an async task.

Install the Infobip API C# SDK

You can use the Package Manager Console:

Install-Package Infobip.Api.SDK

You can also use the .NET Core CLI:

dotnet add package Infobip.Api.SDK

Create a Client instance

First, create an ApiClientConfiguration instance with your Infobip credentials, which are your API Key and Base URL. You can access these from your Infobip account or from the Infobip API landing page once you are logged in.

var configuration = new ApiClientConfiguration(
        "<Base_URL>",
        "<API_Key>");

Next, use this configuration to create an InfobipApiClient instance.

var client = new InfobipApiClient(configuration);

Build and send your WhatsApp text payload

The SENDER is a string that is displayed as the sender when the message is received. When using a trial Infobip account, the DESTINATION_NUMBER must be the number that you registered during account creation. The MESSAGE_ID can be a string of your choosing; if you don’t supply one, the Infobip API will generate one for you.

var request = new WhatsAppTextMessageRequest(
        from : "SENDER",
        to : "DESTINATION_NUMBER",
        messageId : "MESSAGE_ID",
        content : new WhatsAppTextContent("Message Text!")
    );

return await client.WhatsApp.SendWhatsAppTextMessage(request);

Wrap your code in an async Task

Put all this code together into a Task which can be run in order to send your WhatsApp message.

public async Task<WhatsAppSingleMessageInfoResponse> SendWhatsAppTextMessage()
{
    var configuration = new ApiClientConfiguration(
        "<Base_URL>",
        "<API_Key>");

    var client = new InfobipApiClient(configuration);
    var request = new WhatsAppTextMessageRequest(
        from : "SENDER",
        to : "DESTINATION_NUMBER",
        messageId : "MESSAGE_ID",
        content : new WhatsAppTextContent("Message Text!")
    );

    return await client.WhatsApp.SendWhatsAppTextMessage(request);
}

You can inspect the WhatsAppSingleMessageInfoResponse return value to see information about the status of your message.