1. Go to this page and download the library: Download sashalenz/chatwoot-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/ */
sashalenz / chatwoot-api example snippets
use Sashalenz\ChatwootApi\ChatwootApi;
// Create a contact. When `inbox_id` is supplied, the returned DTO also carries
// `sourceId` — the stable contact ↔ inbox key.
$contact = ChatwootApi::contacts()->create([
'name' => 'Jane Doe',
'phone_number' => '+15551234567',
'inbox_id' => 1,
'custom_attributes' => ['plan' => 'pro'],
]);
// Open a conversation for that contact-inbox.
$conversation = ChatwootApi::conversations()->create($contact->sourceId, inboxId: 1);
// Post a message into the conversation.
ChatwootApi::messages()->create(
conversationId: $conversation->id,
content: 'Hello there!',
messageType: 'incoming',
);
// Hand the conversation off to a human agent.
ChatwootApi::conversations()->toggleStatus($conversation->id, 'open');
$page = ChatwootApi::contacts()->list(['page' => 1]);
$page->count(); // items on this page
$page->currentPage(); // 1
$page->totalCount(); // total across all pages (when Chatwoot returns it)
foreach ($page->payload as $contact) {
echo $contact->name, ' ', $contact->phoneNumber, PHP_EOL;
}
use Sashalenz\ChatwootApi\ChatwootApi;
// 1) Upsert the contact (pass `identifier` to keep it stable across sessions).
$contact = ChatwootApi::client()->createContact([
'identifier' => 'viber:01234567890A=',
'name' => 'Petro',
'custom_attributes' => ['client_id' => 42],
]);
$sourceId = $contact->sourceId;
// 2) Open a conversation, 3) push the incoming message.
$conversation = ChatwootApi::client()->createConversation($sourceId);
ChatwootApi::client()->createMessage($sourceId, $conversation->id, 'Привіт');
// Per-call inbox override + identity hash (when the inbox enables validation):
ChatwootApi::client('other-inbox')->createContact([
'identifier' => 'viber:xyz',
'identifier_hash' => ChatwootApi::client()->identifierHash('viber:xyz'),
]);
use Sashalenz\ChatwootApi\Data\WebhookEvent;
$event = WebhookEvent::fromArray($request->all()); // or ::fromJson($request->getContent())
if ($event->isMessageCreated() && $event->isOutgoing()) {
$message = $event->message(); // MessageData
$conversation = $event->conversation(); // ConversationData|null
// …deliver $message->content to your transport
}
ChatwootApi::messages()
->accountId(2)
->token($otherToken)
->create($conversationId, 'Hi from account 2', 'outgoing');