C# / .NET SDK
Install and use the official CastBrick .NET SDK
C# / .NET SDK
CastBrick.SDK is the official .NET SDK. It targets net8.0 and netstandard2.0 —
works in ASP.NET Core, console apps, Azure Functions, and any .NET Standard project.
Installation
dotnet add package CastBrick.SDKInitialization
using CastBrick.SDK;
var cb = new CastBrickClient(new CastBrickOptions
{
ApiKey = Environment.GetEnvironmentVariable("CASTBRICK_API_KEY")!
});With IHttpClientFactory (ASP.NET Core)
// Program.cs
builder.Services.AddHttpClient<CastBrickClient>();
builder.Services.AddSingleton(new CastBrickOptions
{
ApiKey = builder.Configuration["CastBrick:ApiKey"]!
});SMS
Send
using CastBrick.SDK.Models.Sms;
var result = await cb.Sms.SendAsync(new SendSmsRequest
{
Recipients = ["+244923000000", "+244912000000"],
Content = "Your verification code is 1234",
SenderId = "MyApp", // optional
});
Console.WriteLine($"{result.MessageId} — {result.Status}");Schedule for later
await cb.Sms.SendAsync(new SendSmsRequest
{
Recipients = ["+244923000000"],
Content = "Scheduled message",
ScheduledAt = DateTime.UtcNow.AddHours(2),
});List / Get / Cancel
var page = await cb.Sms.ListAsync(page: 1, pageSize: 20);
var msg = await cb.Sms.GetAsync(messageId);
await cb.Sms.CancelScheduledAsync(messageId);Contacts
using CastBrick.SDK.Models.Contacts;
// List
var page = await cb.Contacts.ListAsync(search: "João");
// Get
var contact = await cb.Contacts.GetAsync(contactId);
// Create (comma-separated phone numbers)
await cb.Contacts.CreateAsync(new CreateContactRequest
{
PhoneNumbers = "+244923000000,+244912000000",
});
// Delete
await cb.Contacts.DeleteAsync(contactId);
// Contact lists
var list = await cb.Contacts.CreateListAsync("VIP Customers");
await cb.Contacts.AddToListAsync(list.Id, contact.Id);
await cb.Contacts.RemoveFromListAsync(list.Id, contact.Id);Broadcasts
using CastBrick.SDK.Models.Broadcasts;
// Create
var id = await cb.Broadcasts.CreateAsync(new CreateBroadcastRequest
{
Name = "Black Friday",
Message = "50% off everything today!",
ContactListId = listId, // optional
SenderId = "MyApp", // optional
});
// Send immediately
await cb.Broadcasts.SendAsync(id);
// Update with schedule
await cb.Broadcasts.UpdateAsync(id, new UpdateBroadcastRequest
{
Name = "Black Friday",
Message = "50% off everything today!",
ScheduleAt = new DateTime(2026, 11, 28, 9, 0, 0, DateTimeKind.Utc),
});
// Other operations
await cb.Broadcasts.CancelAsync(id);
var newId = await cb.Broadcasts.DuplicateAsync(id);
await cb.Broadcasts.DeleteAsync(id);Error Handling
try
{
await cb.Sms.SendAsync(...);
}
catch (CastBrickApiException ex)
{
Console.WriteLine($"{ex.StatusCode}: {ex.ResponseBody}");
// 401 → invalid or revoked API key
// 402 → insufficient credits
// 422 → validation error
}