devcode_sms
A Flutter plugin for the DevCode SMS API.
Easily send SMS messages and check your balance from any Flutter application.
Features
- โ๏ธ Send a single SMS to any phone number
- ๐ฆ Bulk SMS โ send the same message to multiple recipients
- ๐ณ Check balance โ query remaining SMS credits
- ๐งช Fully testable โ injectable HTTP client for unit tests
- ๐ก๏ธ Typed exceptions โ
DevcodeSmsExceptionwith status codes
Getting started
1. Add the dependency
dependencies:
devcode_sms: ^0.0.1
2. Get your API key
- Create an account at devcodesms.com
- Subscribe to an SMS plan
- Generate your API key in the "API Key" section
Usage
Initialize the client
import 'package:devcode_sms/devcode_sms.dart';
final sms = DevcodeSms(apiKey: 'YOUR_API_KEY');
Send an SMS
try {
final result = await sms.sendSms(
sender: 'MyApp', // Sender ID (max ~11 chars)
phone: '+237659373726', // International format
message: 'Hello from Flutter!',
);
if (result.isSuccess) {
print('Sent! ${result.message}');
} else {
print('Failed: ${result.message}');
}
} on DevcodeSmsException catch (e) {
print('Error ${e.statusCode}: ${e.message}');
}
Send bulk SMS
final results = await sms.sendBulkSms(
sender: 'MyApp',
phones: ['+237659000001', '+237659000002', '+237659000003'],
message: 'Promotional offer just for you!',
);
for (final r in results) {
print('${r.isSuccess ? "โ
" : "โ"} ${r.message}');
}
Check balance
final balance = await sms.getBalance();
if (balance.isSuccess) {
print('Remaining credits: ${balance.balance} SMS');
}
Error handling
All API methods throw DevcodeSmsException on failure:
| Property | Description |
|---|---|
message |
Human-readable description |
statusCode |
HTTP status code (null for network errors) |
rawResponse |
Raw body from the server (for debugging) |
try {
await sms.sendSms(...);
} on DevcodeSmsException catch (e) {
print('Code: ${e.statusCode}, Message: ${e.message}');
}
Testing
The client accepts an optional httpClient parameter for easy mocking:
import 'package:http/http.dart' as http;
import 'package:mocktail/mocktail.dart';
class MockHttpClient extends Mock implements http.Client {}
final mockClient = MockHttpClient();
final sms = DevcodeSms(apiKey: 'test', httpClient: mockClient);
Run the tests:
flutter test
API Reference
DevcodeSms
| Constructor parameter | Type | Required | Description |
|---|---|---|---|
apiKey |
String |
โ | Your DevCode SMS API key |
httpClient |
http.Client? |
No | Custom HTTP client (testing) |
baseUrl |
String? |
No | Override base URL (testing) |
Methods
| Method | Returns | Description |
|---|---|---|
sendSms(sender, phone, message) |
Future<SmsResponse> |
Send a single SMS |
sendBulkSms(sender, phones, message) |
Future<List<SmsResponse>> |
Send to multiple recipients |
getBalance() |
Future<BalanceResponse> |
Check remaining credits |
Support
- ๐ง support@devcodesms.com
- ๐ (+237) 659 373 726
- ๐ devcodesms.com
Libraries
- devcode_sms
- DevCode SMS โ Flutter plugin for the DevCode SMS API.