C# / .NET SDK
The official AfriRoute NuGet package provides a strongly-typed, async/await client for SMS, Voice, WhatsApp, and Email — with automatic retries and Bearer-token authentication built in. Targets .NET 6+ and .NET Framework 4.6.2+.
Installation
dotnet add package AfriRoute
Or via the Package Manager Console:
Install-Package AfriRoute
Authentication
Create a client with your API key. It is sent as Authorization: Bearer $AFRIROUTE_API_KEY against https://api.afriroute.ai.
using AfriRoute;
using AfriRoute.Models;
var client = new AfrirouteClient("$AFRIROUTE_API_KEY");
Target the sandbox or tune behavior with the full constructor:
var client = new AfrirouteClient(
apiKey: "$AFRIROUTE_API_KEY",
baseUrl: "https://api.afriroute.ai",
timeoutSeconds: 60,
maxRetries: 5
);
Read the key from configuration or an environment variable rather than hard-coding it.
Send an SMS
var sms = await client.Sms.SendAsync(new SmsRequest
{
To = "+251900000000",
Message = "Hello from AfriRoute",
SenderId = "AfriRoute",
});
Console.WriteLine(sms.MessageId);
Send a WhatsApp Message
var wa = await client.WhatsApp.SendAsync(new WhatsAppRequest
{
To = "+251900000000",
Message = "Hello via WhatsApp",
});
Console.WriteLine(wa.MessageId);
Make a Voice Call
var call = await client.Voice.CallAsync(new VoiceCallRequest
{
To = "+251900000000",
From = "+1234567890",
TtsMessage = "Welcome to AfriRoute",
});
Console.WriteLine(call.CallId);
Send an Email
var email = await client.Email.SendAsync(new EmailRequest
{
To = "[email protected]",
Subject = "Welcome",
Body = "<h1>Hello</h1>",
});
Console.WriteLine(email.MessageId);
Error Handling
Failed requests throw AfriRouteException, exposing Code, Message, and StatusCode:
try
{
await client.Sms.SendAsync(new SmsRequest
{
To = "+251900000000",
Message = "Hello",
SenderId = "AfriRoute",
});
}
catch (AfriRouteException ex)
{
switch (ex.Code)
{
case "INVALID_PHONE_NUMBER":
Console.WriteLine("Use E.164 format, e.g. +251911234567");
break;
case "INSUFFICIENT_BALANCE":
Console.WriteLine("Top up your account");
break;
case "RATE_LIMIT_EXCEEDED":
Console.WriteLine("Slow down — retried automatically");
break;
default:
Console.WriteLine($"{ex.Code}: {ex.Message}");
break;
}
}
Transient failures (429, 5xx) are retried automatically up to maxRetries with exponential back-off.
Dependency Injection
Register the client as a singleton in ASP.NET Core:
builder.Services.AddSingleton(
new AfrirouteClient(builder.Configuration["AfriRoute:ApiKey"]));
Features
- Strongly-typed request and response models
async/awaitthroughout,CancellationTokensupport- Automatic retries with exponential back-off
- Built-in rate-limit (
429) handling - Compatible with .NET 6+ and .NET Framework
Related Resources
Last Updated: May 2026 · Need help? Contact Support →