vonage/client
Wrapper package for the Vonage PHP SDK that keeps Vonage functionality separate from the HTTP client. Requires PHP 8+. If you have conflicts with the guzzle6-adapter, use vonage/client-core plus any php-http client implementation.
Install the Package
composer require vonage/client
Verify installation by checking composer.json for the dependency.
Basic Initialization
use Vonage\Client\Credentials\Basic;
use Vonage\Client;
$credentials = new Basic('YOUR_API_KEY', 'YOUR_API_SECRET');
$client = new Client($credentials);
YOUR_API_KEY and YOUR_API_SECRET with your Vonage credentials (found in the Vonage Dashboard)..env file) and avoid hardcoding.First API Call (SMS Example)
$response = $client->sms()->send([
'to' => '15551234567',
'from' => 'YourApp',
'text' => 'Hello from Vonage!'
]);
$response->successful() to verify the request worked.\Log::debug('SMS Response:', $response->getData());
Key Documentation Links
Sending SMS
$client->sms()->send([
'to' => '15551234567',
'from' => 'YourBrand',
'text' => 'Your message here',
'type' => 'promo', // Optional: 'promo', 'transactional', etc.
'unicode' => true, // For non-ASCII characters
]);
type for compliance with carrier rules (e.g., promo for marketing messages).if (!$response->successful()) {
\Log::error('SMS failed:', $response->getErrors());
// Retry logic or notify admin
}
Checking SMS Status
$response = $client->sms()->status('SMS_MESSAGE_UUID');
$status = $response->getData()['status']['smsMessageData']['status'];
Making a Call
$response = $client->voice()->createCall([
'to' => ['type' => 'phone', 'number' => '15551234567'],
'from' => ['type' => 'phone', 'number' => '15559876543'],
'answerUrl' => 'https://your-app.com/answer',
'eventUrl' => 'https://your-app.com/events',
]);
answerUrl for IVR logic (e.g., TwiML responses).uuid for tracking:
$callUuid = $response->getData()['uuid'];
Recording Calls
$response = $client->voice()->createCall([
'to' => ['type' => 'phone', 'number' => '15551234567'],
'record' => true,
'recordFileFormat' => 'mp3',
]);
$response = $client->numberInsights()->get('15551234567');
$status = $response->getData()['numberType'];
$response = $client->verify()->start([
'number' => '15551234567',
'brand' => 'YourApp',
]);
$requestId = $response->getData()['request_id'];
$response = $client->verify()->check($requestId);
$status = $response->getData()['status'];
Rate Limiting Vonage enforces rate limits (e.g., 1 SMS/second by default). Implement exponential backoff:
use Vonage\Client\Exceptions\RateLimitExceeded;
try {
$response = $client->sms()->send([...]);
} catch (RateLimitExceeded $e) {
sleep($e->getRetryAfter());
retry();
}
Webhooks Configure webhooks in the Vonage Dashboard to handle events (e.g., SMS delivery reports, call events). Example Laravel route:
Route::post('/vonage/webhook', function (Request $request) {
$payload = $request->json()->all();
// Validate payload (e.g., check signature)
// Process event (e.g., update DB, send notification)
});
Testing Use Vonage’s sandbox environment for testing:
$sandboxCredentials = new Basic('YOUR_SANDBOX_KEY', 'YOUR_SANDBOX_SECRET');
$sandboxClient = new Client($sandboxCredentials);
Logging Enable debug logging for troubleshooting:
$client = new Client($credentials, [
'log' => [
'enabled' => true,
'file' => storage_path('logs/vonage.log'),
]
]);
Credentials Leaks
.env and config/services.php:
VONAGE_KEY=your_api_key
VONAGE_SECRET=your_api_secret
// config/services.php
'vonage' => [
'key' => env('VONAGE_KEY'),
'secret' => env('VONAGE_SECRET'),
];
laravel/env-editor to manage secrets safely.Timeouts
$client = new Client($credentials, [
'timeout' => 30, // Default is 10 seconds
]);
Webhook Verification
X-Vonage-Signature header:
$signature = $request->header('X-Vonage-Signature');
$payload = $request->getContent();
$expected = hash_hmac('sha256', $payload, config('services.vonage.secret'));
if (!hash_equals($expected, $signature)) {
abort(403, 'Invalid signature');
}
Number Format
+15551234567 for US numbers). Validate with:
use Vonage\Client\Helpers\NumberHelper;
$validNumber = NumberHelper::format($number, 'E164');
Idempotency
idempotencyKey option:
$response = $client->sms()->send([
'to' => '15551234567',
'from' => 'YourApp',
'text' => 'Hello',
], [
'idempotencyKey' => Str::uuid()->toString(),
]);
Enable Debug Mode
$client = new Client($credentials, [
'debug' => true,
]);
storage/logs/vonage.log.Inspect Raw Responses
$response = $client->sms()->send([...]);
\Log::debug('Raw Response:', $response->getRawData());
Common Errors | Error | Cause
How can I help you explore Laravel packages today?