sendinblue/api-v3-sdk
Deprecated PHP SDK for SendinBlue API v3 (auto-generated from OpenAPI). Provides client wrappers for SendinBlue features with API key/partner key auth. Install via Composer (sendinblue/api-v3-sdk 8.x) and use included API classes to call endpoints.
Installation:
composer require sendinblue/api-v3-sdk:^8.0
Add to composer.json if needed:
"autoload": {
"psr-4": {
"App\\": "app/",
"SendinBlue\\": "vendor/sendinblue/api-v3-sdk"
}
}
First Use Case:
Fetch account details (replace YOUR_API_KEY):
use SendinBlue\Client\Api\AccountApi;
use SendinBlue\Client\ApiException;
use SendinBlue\Client\Configuration;
$config = Configuration::getDefaultConfiguration()
->setApiKey('api-key', 'YOUR_API_KEY');
$api = new AccountApi(null, $config);
try {
$account = $api->getAccount();
dd($account); // Debug account data
} catch (ApiException $e) {
dd($e->getResponseBody());
}
ContactsApi, EcommerceApi).SendinBlue\Client\Configuration.SendinBlue\Client\Model\.Wrap API calls in a Laravel service class for reusability:
namespace App\Services;
use SendinBlue\Client\Api\ContactsApi;
use SendinBlue\Client\Model\Contact;
class ContactService {
protected $api;
public function __construct() {
$this->api = new ContactsApi(null, config('services.sendinblue'));
}
public function createContact(array $data): Contact {
return $this->api->createContact(new Contact($data));
}
}
Use batch endpoints for efficiency (e.g., bulk contact updates):
$batchData = [
['id' => 1, 'email' => 'new1@example.com'],
['id' => 2, 'email' => 'new2@example.com']
];
$response = $this->api->updateBatchContacts($batchData);
Listen for SendinBlue webhooks (e.g., contact updates) via Laravel events:
// routes/web.php
Route::post('/sendinblue/webhook', [WebhookController::class, 'handle']);
// app/Http/Controllers/WebhookController.php
public function handle(Request $request) {
event(new SendinBlueWebhookReceived($request->all()));
}
Store API keys in .env and bind to Laravel config:
SENDINBLUE_API_KEY=your_api_key_here
SENDINBLUE_PARTNER_KEY=your_partner_key_here
// config/services.php
'sendinblue' => [
'api_key' => env('SENDINBLUE_API_KEY'),
'partner_key' => env('SENDINBLUE_PARTNER_KEY'),
],
Centralize API error responses:
try {
$result = $api->getContacts();
} catch (ApiException $e) {
$error = json_decode($e->getResponseBody(), true);
throw new \RuntimeException($error['message'] ?? $e->getMessage());
}
api-key and partner-key). Ensure you use the correct one for your endpoint (e.g., partner-key for transactional emails).config() helper to dynamically switch keys:
$config = Configuration::getDefaultConfiguration()
->setApiKey('api-key', config('services.sendinblue.api_key'));
429 Too Many Requests.use GuzzleHttp\Exception\RequestException;
try {
$response = $api->getContacts();
} catch (RequestException $e) {
if ($e->getCode() === 429) {
sleep(2); // Retry after delay
return $this->getContacts();
}
throw $e;
}
getContacts()) paginate by default. Use limit and offset parameters:
$contacts = $api->getContacts(['limit' => 50, 'offset' => 0]);
$lastId = null;
do {
$params = ['limit' => 100];
if ($lastId) $params['offset'] = $lastId;
$contacts = $api->getContacts($params);
$lastId = end($contacts)->getId();
} while ($contacts);
X-Signature header:
public function handle(Request $request) {
$signature = $request->header('X-Signature');
$payload = $request->getContent();
$expectedSignature = hash_hmac('sha256', $payload, config('services.sendinblue.webhook_secret'));
if (!hash_equals($signature, $expectedSignature)) {
abort(403, 'Invalid signature');
}
}
email without @) may silently fail.use Illuminate\Support\Facades\Validator;
$validator = Validator::make($data, [
'email' => 'required|email',
'firstName' => 'sometimes|string',
]);
if ($validator->fails()) {
throw new \InvalidArgumentException($validator->errors()->first());
}
SendinBlue\Client\Api classes in tests to avoid real API calls:
$mockApi = Mockery::mock(SendinBlue\Client\Api\ContactsApi::class);
$mockApi->shouldReceive('createContact')
->once()
->andReturn(new Contact(['id' => 123]));
$service = new ContactService($mockApi);
Http facade to stub Guzzle requests:
Http::fake([
'api.sendinblue.com/*' => Http::response(['success' => true], 200),
]);
$client = new GuzzleHttp\Client([
'timeout' => 30,
'headers' => ['User-Agent' => 'MyApp/1.0'],
]);
$api = new ContactsApi($client, $config);
spatie/laravel-sendinblue).How can I help you explore Laravel packages today?