1. Go to this page and download the library: Download sendbee/api library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
sendbee / api example snippets
$sendbeeApi = new \Sendbee\Api\Client($public_key, $secret);
// optional parameters
$params = [
'tags' => '', // Filter contacts by tag
'status' => '', // Filter contacts by status
'search_query' => '', // Filter contacts by query string
'page' => 1 // Page number for pagination
];
try {
$response = $sendbeeApi->getContacts($params);
} catch (\Exception $ex) {
// handle exception thrown by GuzzleHttp
// this is most likely due to a network issue
echo "Could not contact backend endpoint. ", $ex->getMessage();
}
if ($response->isSuccess()) {
// everything is OK
$data = $response->getData();
foreach ($data as $contact) {
/**
* @var $contact \Sendbee\Api\Models\Contact
*/
echo "\n ID: ", $contact->id;
echo "\n name: ", $contact->name;
echo "\n phone: ", $contact->phone;
echo "\n whatsapp_user_id: ", $contact->whatsapp_user_id;
echo "\n created_at: ", $contact->created_at;
echo "\n status: ", $contact->status;
echo "\n folder: ", $contact->folder;
foreach ($contact->tags as $tag) {
/**
* @var $tag \Sendbee\Api\Models\ContactTag
*/
echo "\n tag -> id: ", $tag->id;
echo "\n tag -> name: ", $tag->name;
}
foreach ($contact->notes as $note) {
/**
* @var $note \Sendbee\Api\Models\ContactNote
*/
echo "\n note -> value: ", $note->value;
}
foreach ($contact->contact_fields as $contactField) {
/**
* @var $contactField \Sendbee\Api\Models\ContactContactField
*/
echo "\n contact_field -> key: ", $contactField->key;
echo "\n contact_field -> value: ", $contactField->value;
}
}
} else {
/**
* @var $error \Sendbee\Api\Transport\ResponseError
*/
$error = $response->getError();
if ($error) {
echo "\n error type: ", $error->type;
echo "\n error details: ", $error->detail;
}
}
$contactData = [
// contact phone number, MANDATORY
'phone' => '+...',
// feel free to specify other optional contact data here
// WhatsApp Business-scoped user ID (BSUID) of the contact
// example "US.13491208655302741918"
'whatsapp_user_id' => '...',
// tag new contact
// if tag doesn't exist, it will be created
'tags' => ['...',],
// contact name
'name' => '...',
// contact fields
// contact fields must be pre-created in Sendbee Dashboard
// any non-existent field will be ignored
'contact_fields' => ['key' => 'value'],
// your notes about subscriber
'notes' => ['...'],
// prevent sending browser push notification and email
// notification to agents, when new contact subscribes
// (default is True)
'block_notifications' => true,
// prevent sending automated template messages to newly
// subscribed contact (if any is set in Sendbee Dashboard)
// (default is True)
'block_automation' => true
];
try {
$response = $sendbeeApi->subscribeContact($contactData);
} catch (\Sendbee\Api\Support\DataException $ex) {
// handle missing data
// this happens when
$contactData = [
// contact id, MANDATORY
'id' => '...',
// feel free to specify other optional contact data here
// WhatsApp Business-scoped user ID (BSUID) of the contact
// example "US.13491208655302741918"
'whatsapp_user_id' => '...',
// tag new contact
// if tag doesn't exist, it will be created
'tags' => ['...',],
// contact name
'name' => '...',
// contact fields
// contact fields must be pre-created in Sendbee Dashboard
// any non-existent field will be ignored
'contact_fields' => ['...' => '...'],
// your notes about subscriber
// TAKE CARE, notes are not replaced but are instead appended to existing notes
'notes' => ['...'],
// prevent sending browser push notification and email
// notification to agents, when new contact subscribes
// (default is True)
'block_notifications' => true,
// prevent sending automated template messages to newly
// subscribed contact (if any is set in Sendbee Dashboard)
// (default is True)
'block_automation' => true
];
try {
$response = $sendbeeApi->updateContact($contactData);
} catch (\Sendbee\Api\Support\DataException $ex) {
// handle missing data
// this happens when
// optional parameters
$params = [
'name' => '', // Name of the tag
'page' => 1 // Page number for pagination
];
try {
$response = $sendbeeApi->getTags($params);
} catch (\Exception $ex) {
// handle exception thrown by GuzzleHttp
// this is most likely due to a network issue
echo "Could not contact backend endpoint. ", $ex->getMessage();
}
if ($response->isSuccess()) {
// everything is OK
$data = $response->getData();
foreach ($data as $tag) {
/**
* @var $tag \Sendbee\Api\Models\ContactTag
*/
echo "\n ID: ", $tag->id;
echo "\n name: ", $tag->name;
}
} else {
/**
* @var $error \Sendbee\Api\Transport\ResponseError
*/
$error = $response->getError();
if ($error) {
echo "\n error type: ", $error->type;
echo "\n error details: ", $error->detail;
}
}
$data = [
// tag name, MANDATORY
'name' => '...'
];
try {
$response = $sendbeeApi->createTag($data);
} catch (\Sendbee\Api\Support\DataException $ex) {
// handle missing data
// this happens when point. ", $ex->getMessage();
}
if ($response->isSuccess()) {
/**
* @var $tag \Sendbee\Api\Models\ContactTag
*/
$tag = $response->getData();
print_r($tag);
// tag is now created
// $tag contains the newly created tag data
} else {
/**
* @var $error \Sendbee\Api\Transport\ResponseError
*/
$error = $response->getError();
if ($error) {
// handle error
echo "\n error type: ", $error->type;
echo "\n error details: ", $error->detail;
}
}
$data = [
// tag id, MANDATORY
'id' => '...',
// tag name, MANDATORY
'name' => '...'
];
try {
$response = $sendbeeApi->updateTag($data);
} catch (\Sendbee\Api\Support\DataException $ex) {
// handle missing data
// this happens when e->isSuccess()) {
/**
* @var $tag \Sendbee\Api\Models\ContactTag
*/
$tag = $response->getData();
// tag is now updated
// $tag contains the updated tag data
print_r($tag);
} else {
/**
* @var $error \Sendbee\Api\Transport\ResponseError
*/
$error = $response->getError();
if ($error) {
// handle error
echo "\n error type: ", $error->type;
echo "\n error details: ", $error->detail;
}
}
$data = [
// tag id, MANDATORY
'id' => '...',
];
try {
$response = $sendbeeApi->deleteTag($data);
} catch (\Sendbee\Api\Support\DataException $ex) {
// handle missing data
// this happens when endpoint. ", $ex->getMessage();
}
if ($response->isSuccess()) {
/**
* @var $message \Sendbee\Api\Models\ServerMessage
*/
$message = $response->getData();
// record is now deleted
// $message contains server info message
print_r($message);
} else {
/**
* @var $error \Sendbee\Api\Transport\ResponseError
*/
$error = $response->getError();
if ($error) {
// handle error
echo "\n error type: ", $error->type;
echo "\n error details: ", $error->detail;
}
}
$params = [
'search_query' => '', // Filter by query string
'page' => 1 // Page number for pagination
];
try {
$response = $sendbeeApi->getContactFields($params);
} catch (\Exception $ex) {
// handle exception thrown by GuzzleHttp
// this is most likely due to a network issue
echo "Could not contact backend endpoint. ", $ex->getMessage();
}
if ($response->isSuccess()) {
// everything is OK
$data = $response->getData();
foreach ($data as $field) {
/**
* @var $tag \Sendbee\Api\Models\ContactField
*/
echo "\n ID: ", $field->id;
echo "\n type: ", $field->type;
echo "\n name: ", $field->name;
foreach ($field->options as $option) {
/**
* @var $option string
*/
echo "\n field -> option: ", $option;
}
}
} else {
/**
* @var $error \Sendbee\Api\Transport\ResponseError
*/
$error = $response->getError();
if ($error) {
echo "\n error type: ", $error->type;
echo "\n error details: ", $error->detail;
}
}
$data = [
// name, MANDATORY
'name' => 'field name',
// type, one of ['text', 'number', 'list', 'date', 'boolean'], MANDATORY
'type' => 'text',
// List of options. Send it only if the field type is a list.
// values are strings
'options' => []
];
try {
$response = $sendbeeApi->createContactField($data);
} catch (\Sendbee\Api\Support\DataException $ex) {
// handle missing data
// this happens when ontactField contains the newly created contact field data
print_r($contactField);
} else {
/**
* @var $error \Sendbee\Api\Transport\ResponseError
*/
$error = $response->getError();
if ($error) {
// handle error
echo "\n error type: ", $error->type;
echo "\n error details: ", $error->detail;
}
}
$data = [
// id, MANDATORY
'id' => '...',
// name, MANDATORY
'name' => 'field name update',
// type, one of ['text', 'number', 'list', 'date', 'boolean'], MANDATORY
'type' => 'text',
// List of options. Send it only if the field type is a list.
// values are strings
'options' => []
];
try {
$response = $sendbeeApi->updateContactField($data);
} catch (\Sendbee\Api\Support\DataException $ex) {
// handle missing data
// this happens when data
print_r($contactField);
} else {
/**
* @var $error \Sendbee\Api\Transport\ResponseError
*/
$error = $response->getError();
if ($error) {
// handle error
echo "\n error type: ", $error->type;
echo "\n error details: ", $error->detail;
}
}
$data = [
// id, MANDATORY
'id' => '...',
];
try {
$response = $sendbeeApi->deleteContactField($data);
} catch (\Sendbee\Api\Support\DataException $ex) {
// handle missing data
// this happens when int. ", $ex->getMessage();
}
if ($response->isSuccess()) {
/**
* @var $message \Sendbee\Api\Models\ServerMessage
*/
$message = $response->getData();
// record is now deleted
// $message contains server info message
print_r($message);
} else {
/**
* @var $error \Sendbee\Api\Transport\ResponseError
*/
$error = $response->getError();
if ($error) {
// handle error
echo "\n error type: ", $error->type;
echo "\n error details: ", $error->detail;
}
}
// optional parameters
$params = [
// Filter conversations by folder. Specify open, done, spam or notified
'folder' => '',
// Any kind of string that will be used to perform filtering
'search_query' => '',
// Page number for pagination
'page' => 1
];
try {
$response = $sendbeeApi->getConversations($params);
} catch (\Exception $ex) {
// handle exception thrown by GuzzleHttp
// this is most likely due to a network issue
echo "Could not contact backend endpoint. ", $ex->getMessage();
}
if ($response->isSuccess()) {
// everything is OK
$data = $response->getData();
foreach ($data as $conversation) {
/**
* @var $conversation \Sendbee\Api\Models\Conversation
*/
echo "\n ID: ", $conversation->id;
echo "\n folder: ", $conversation->folder;
echo "\n chatbot_active: ", $conversation->chatbot_active;
echo "\n platform: ", $conversation->platform;
echo "\n created_at: ", $conversation->created_at;
echo "\n contact -> id: ", $conversation->contact->id;
echo "\n contact -> name: ", $conversation->contact->name;
echo "\n contact -> phone: ", $conversation->contact->phone;
echo "\n contact -> whatsapp_user_id: ", $conversation->contact->whatsapp_user_id;
echo "\n last_message -> direction: ", $conversation->last_message->direction;
echo "\n last_message -> status: ", $conversation->last_message->status;
echo "\n last_message -> inbound_sent_at: ", $conversation->last_message->inbound_sent_at;
echo "\n last_message -> outbound_sent_at: ", $conversation->last_message->outbound_sent_at;
}
} else {
/**
* @var $error \Sendbee\Api\Transport\ResponseError
*/
$error = $response->getError();
if ($error) {
echo "\n error type: ", $error->type;
echo "\n error details: ", $error->detail;
}
}
$data = [
// phone number to send the message to
// either 'phone' or 'whatsapp_user_id' is MANDATORY
'phone' => '+...',
// WhatsApp Business-scoped user ID (BSUID) of the recipient, example "US.13491208655302741918"
// used instead of 'phone' when the phone number is unknown
// if both are provided, 'phone' takes precedence
'whatsapp_user_id' => '...',
// keyword of an existing template message you are using, MANDATORY
'template_keyword' => '...',
// language code of an existing template message you are using, MANDATORY
'language' => 'en',
// tags, key-value pairs of data that is injected in placeholders, MANDATORY
// example:
// template message is 'Your order {{order}} has been dispatched. Please expect delivery by {{date}}'
// tags are ['order' => 55, 'date' => '2020-12-12']
// final message will be 'Your order 55 has been dispatched. Please expect delivery by 2020-12-12'
'tags' => [],
// Set to true to disable turning-off chatbot
'prevent_bot_off' => true,
// send attachment url for media template mesages
'attachment' => 'http...'
];
try {
$response = $sendbeeApi->sendMessageTemplate($data);
} catch (\Sendbee\Api\Support\DataException $ex) {
// handle missing data
// this happens when
$data = [
// phone number to send the message to
// either 'phone' or 'whatsapp_user_id' is MANDATORY
'phone' => '+...',
// WhatsApp Business-scoped user ID (BSUID) of the recipient, example "US.13491208655302741918"
// used instead of 'phone' when the phone number is unknown
// if both are provided, 'phone' takes precedence
'whatsapp_user_id' => '...',
// message text, MANDATORY
'text' => '...',
// Media URL for media message
'media_url' => '',
// Set to true to disable turning-off chatbot
'prevent_bot_off' => true,
];
try {
$response = $sendbeeApi->sendMessage($data);
} catch (\Sendbee\Api\Support\DataException $ex) {
// handle missing data
// this happens when
$params = [
// Filter teams by member UUID - optional
'member_id' => '...',
// Page number for pagination
'page' => 1
];
try {
$response = $sendbeeApi->getTeams($params);
} catch (\Sendbee\Api\Support\DataException $ex) {
// handle missing data
// this happens when is OK
$data = $response->getData();
foreach ($data as $team) {
/**
* @var $team \Sendbee\Api\Models\Team
*/
echo "\n id: ", $team->id;
echo "\n name: ", $team->name;
foreach ($team->members as $member) {
/**
* @var $member \Sendbee\Api\Models\TeamMember
*/
echo "\n member -> id: ", $member->id;
echo "\n member -> name: ", $member->name;
echo "\n member -> role: ", $member->role;
echo "\n member -> online: ", $member->online;
echo "\n member -> available: ", $member->available;
}
}
} else {
/**
* @var $error \Sendbee\Api\Transport\ResponseError
*/
$error = $response->getError();
if ($error) {
echo "\n error type: ", $error->type;
echo "\n error details: ", $error->detail;
}
}
$params = [
// Filter members by team UUID - optional
'team_id' => '...',
// Page number for pagination
'page' => 1
];
try {
$response = $sendbeeApi->getMembers($params);
} catch (\Sendbee\Api\Support\DataException $ex) {
// handle missing data
// this happens when is OK
$data = $response->getData();
foreach ($data as $member) {
/**
* @var $member \Sendbee\Api\Models\Member
*/
echo "\n id: ", $member->id;
echo "\n name: ", $member->name;
echo "\n role: ", $member->role;
echo "\n online: ", $member->online;
echo "\n available: ", $member->available;
foreach ($member->teams as $team) {
/**
* @var $team \Sendbee\Api\Models\MemberTeam
*/
echo "\n member -> id: ", $team->id;
echo "\n member -> name: ", $team->name;
}
}
} else {
/**
* @var $error \Sendbee\Api\Transport\ResponseError
*/
$error = $response->getError();
if ($error) {
echo "\n error type: ", $error->type;
echo "\n error details: ", $error->detail;
}
}
$data = [
// conversation_id, MANDATORY
'conversation_id' => '...',
// boolean value, true to enable chatbot, false to disable, MANDATORY
'active' => true | false
];
try {
$response = $sendbeeApi->chatbotActivity($data);
} catch (\Sendbee\Api\Support\DataException $ex) {
// handle missing data
// this happens when
$message = $response->getData();
// chatbot activity is now set
// $message contains server info message
print_r($message);
} else {
/**
* @var $error \Sendbee\Api\Transport\ResponseError
*/
$error = $response->getError();
if ($error) {
// handle error
echo "\n error type: ", $error->type;
echo "\n error details: ", $error->detail;
}
}
/**
* @var $response \Sendbee\Api\Transport\Response
*/
$response = $sendbeeApi->getContacts();
// check if request was successful
$success = $response->isSuccess();
// get HTTP status code the server returned
$statusCode = $response->getHttpStatus();
// get data wrapped into appropriate object(s)
$data = $response->getData();
// get pagination data (when available)
$pagination = $response->getMeta();
// get error if API call failed
$error = $response->getError();
// get a warning message sent by API (when available)
$warning = $response->getWarning();
// get response headers
$headers = $response->getHeaders();
// get rate limit information from response headers
$rateLimit = $response->getRateLimitLimit(); // requests per minute (60)
$rateLimitRemaining = $response->getRateLimitRemaining(); // remaining requests
$rateLimitReset = $response->getRateLimitReset(); // timestamp when limit resets
// if you prefer to deal with the raw server response, that is available as well
$rawResponseString = $response->getRawBody();
// No special handling needed - rate limiting is automatic
$response = $sendbeeApi->getContacts();
// Even when paginating through many pages, the client handles rate limits automatically
for ($page = 1; $page <= 126; $page++) {
$response = $sendbeeApi->getContacts(['page' => $page]);
// Process results...
}
$response = $sendbeeApi->getContacts();
// Get rate limit information
$limit = $response->getRateLimitLimit(); // 60 (requests per minute)
$remaining = $response->getRateLimitRemaining(); // How many requests left
$reset = $response->getRateLimitReset(); // Unix timestamp when limit resets
echo "Rate limit: $remaining/$limit requests remaining";
echo "Resets at: " . date('Y-m-d H:i:s', $reset);
/**
* @var $response \Sendbee\Api\Transport\Response
*/
$response = $sendbeeApi->getContacts();
$pagination = $response->getMeta();
echo "\n Total records: ", $pagination->total;
echo "\n Current records from: ", $pagination->from;
echo "\n Current records to: ", $pagination->to;
echo "\n Current page: ", $pagination->current_page;
echo "\n Last page: ", $pagination->last_page;
echo "\n How many records per page: ", $pagination->per_page;
// example of exception checking when calling some API method
// in this example, we are trying to create a new tag
$data = [];
try {
$response = $sendbeeApi->createTag($data);
} catch (\Sendbee\Api\Support\DataException $ex) {
// handle missing data
// this happens when