Method Usage
Await is Must
It is crucial to use await with all methods due to API calls and for better state management.
Return Method Usage
To utilize return methods, assign your method call to a Request or var variable.
var res = await whatsapp.sendMessage(
phoneNumber: 'RECIPIENT_NUMBER',
text: 'text_message',
previewUrl: true,
);
if (res.isSuccess()) {
// when message sent
// Returns the ID of the message
print('Message ID: ${res.getMessageId()}');
// Returns the number where the message was sent
print('Message sent to: ${res.getContactId()}');
// Returns the exact API response body
print('API Response: ${res.getFullResponse()}');
} else {
// Returns the WhatsApp error code
print('HTTP Code: ${res.getErrorCode()}');
// Returns the exact error from the WhatsApp Cloud API
print('API Error: ${res.getErrorMessage()}');
// Returns the WhatsApp error type
print('Request Error: ${res.getErrorType()}');
// Graph API error subcode, when present
print('Error Subcode: ${res.getErrorSubcode()}');
// Meta trace ID for support requests
print('Trace ID: ${res.getFbtraceId()}');
}
API errors throw
Responses above only cover successful HTTP calls. A non-2xx response throws a typed
exception; wrap calls in try/catch for production code.
try {
final res = await whatsapp.sendMessage(
phoneNumber: 'RECIPIENT_NUMBER',
text: 'Hello',
);
print(res.getMessageId());
} on WhatsAppRateLimitException catch (error) {
print('Rate limited: ${error.message}');
} on WhatsAppException catch (error) {
print('API error ${error.errorCode}: ${error.message}');
print('Trace: ${error.fbtraceId}');
} on NetworkException catch (error) {
print('Network error: ${error.message}');
}
Service getters
Every feature service is available from the client without constructing it manually:
await whatsapp.textService.sendMessage('RECIPIENT_NUMBER', 'Hello', false);
await whatsapp.mediaService.getMedia('MEDIA_ID');
await whatsapp.templateService.sendTemplate(
'RECIPIENT_NUMBER',
'welcome',
'en_US',
[
{'type': 'text', 'text': 'Rohit'},
],
);
This is useful when a method exists on a service but has no top-level shortcut on
WhatsApp.