Skip to content

PHP SDK

Official PHP SDK for backend integration with the Uchara Chat Platform.

  • PHP 8.0+
  • Composer
  • ext-json
Terminal window
composer require uchara/sdk
<?php
require 'vendor/autoload.php';
use Uchara\SDK\ServerSDK;
use Uchara\SDK\UcharaException;
$client = new ServerSDK(
apiUrl: 'https://api.uchara.com',
apiKey: getenv('UCHARA_API_KEY')
);
try {
// Send a message
$message = $client->sendMessage('conv_abc123', [
'content' => 'Your order has shipped! 🚚',
'sender_type' => 'bot',
]);
echo "Message sent: {$message['id']}\n";
// List open conversations
$conversations = $client->listConversations([
'status' => 'open',
'limit' => 10,
]);
foreach ($conversations as $conv) {
echo "{$conv['id']} - {$conv['contact_name']}\n";
}
// Upsert contact (match by external_id)
$contact = $client->upsertContact([
'external_id' => 'user_123',
'name' => 'John Doe',
'email' => 'john@example.com',
]);
echo "Contact: {$contact['id']}\n";
} catch (UcharaException $e) {
echo "Error ({$e->getCode()}): {$e->getMessage()}\n";
if ($e->getDetails()) {
print_r($e->getDetails());
}
}
// Contacts
$client->upsertContact(array $data): array
$client->getContact(string $id): array
$client->listContacts(array $options = []): array
// Conversations
$client->getConversation(string $id): array
$client->listConversations(array $filters = []): array
$client->assignConversation(string $id, ?string $agentId): array
$client->resolveConversation(string $id): array
// Messages
$client->sendMessage(string $convId, array $data): array
$client->getMessages(string $convId, array $options = []): array
// Channels & Members
$client->listChannels(): array
$client->listMembers(): array
config/services.php
return [
'uchara' => [
'api_url' => env('UCHARA_API_URL', 'https://api.uchara.com'),
'api_key' => env('UCHARA_API_KEY'),
],
];
// app/Providers/AppServiceProvider.php
public function register(): void
{
$this->app->singleton(ServerSDK::class, fn() => new ServerSDK(
apiUrl: config('services.uchara.api_url'),
apiKey: config('services.uchara.api_key'),
));
}
// In a controller / job
public function notify(ServerSDK $uchara, Order $order): void
{
$uchara->sendMessage($order->conversation_id, [
'content' => "Order #{$order->id} has shipped!",
'sender_type' => 'bot',
]);
}