SendGrid: Cara Pakai API Dengan Authentication Menggunakan PHP dan cURL

SendGrid menyediakan REST API sebagai penghubung antara aplikasi kita dengan layanan yang mereka berikan. Untuk berinteraksi dengan API mereka, kita membutuhkan API KEY yang didapat dari account kita di SendGrid.

Sesuai dengan dokumentasi di website SendGrid, berikut cara panggil API nya menggunakan cURL.

curl --request POST \
  --url https://api.sendgrid.com/v3/mail/batch \
  --header 'authorization: Bearer YOUR_API_KEY' \
  --header 'Content-Type: application/json' \

Contoh diatas untuk mendapatkan Batch id.

Authentication diatas menggunakan Bearer atau token berupa API KEY account kita di SendGrid.

Berikut ketika cURL digunakan pada PHP.

$url = '<url api>';
$apikey = '<api key anda>';
$content = json_encode('<array content anda>');

$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-type: application/json","authorization: Bearer ".$apikey));
curl_setopt($curl, CURLOPT_HTTPGET, 1); //untuk GET method

$json_response = curl_exec($curl);

curl_close($curl);

$response = json_decode($json_response, true);

 

 

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.