PHP code example of kirimi / kirimi-php

1. Go to this page and download the library: Download kirimi/kirimi-php 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/ */

    

kirimi / kirimi-php example snippets




irimi\KirimiClient;

$client = new KirimiClient('YOUR_USER_CODE', 'YOUR_SECRET_KEY');

$client = new KirimiClient($userCode, $secret, $endpoint = 'https://api.kirimi.id');

// Text message only
$result = $client->sendMessage('device_id', '628123456789', 'Hello World!');

// Message with media
$result = $client->sendMessage(
    'device_id',
    '628123456789',
    'Check out this image!',
    'https://example.com/image.jpg'
);

// With advanced options
$result = $client->sendMessage('device_id', '628123456789', 'Hello!', null, [
    'enableTypingEffect' => true,
    'typingSpeedMs'      => 350,       // 100-800
    'quotedMessageId'    => 'MSG_ID',
]);

$result = $client->sendMessageFast('device_id', '628123456789', 'Hello!');

$result = $client->sendMessageFile(
    'device_id',
    '628123456789',
    '/path/to/document.pdf',
    ['message' => 'Here is your invoice', 'fileName' => 'invoice.pdf']
);

$result = $client->broadcastMessage(
    'device_id',
    'promo-juli',                                 // label, max 100 chars
    ['628111111111', '628222222222'],
    'Promo hari ini!',
    ['delay' => 30]                               // seconds, clamped 30-3600
);

$result = $client->sendWabaMessage('waba_id', '628123456789', 'order_update', [
    'variables' => ['Budi', 'ORD-001'],
    'header'    => ['type' => 'text', 'text' => 'Order update'],
]);

// Free-form reply (within the 24h customer service window)
$result = $client->wabaReply('waba_id', '628123456789', [
    'type' => 'text',
    'text' => 'Halo, ada yang bisa dibantu?',
]);

// List conversations
$conversations = $client->wabaConversations(50, 1);   // limit, page

// Refresh template status from Meta
$templates = $client->wabaTemplatesSync('waba_id');

$result = $client->wabaSendOtp('waba_id', '628123456789', 'otp_auth');
$verify = $client->wabaVerifyOtp('waba_id', '628123456789', '123456');

$device  = $client->createDevice(3, 'VOUCHER10');       // package_id, voucher_code
$connect = $client->connectDevice('device_id');          // returns QR/session state
$renew   = $client->renewDevice('device_id', 4, 'VOUCHER10');

$devices  = $client->listDevices(1, 10);                 // page, limit
$status   = $client->deviceStatus('device_id');
$detailed = $client->deviceStatusEnhanced('device_id');

$info = $client->userInfo();

$result = $client->saveContact('John Doe', '628123456789', 'device_id');

$bulk = $client->saveContactsBulk([
    ['nama' => 'John Doe', 'nomor' => '628123456789'],
    ['nama' => 'Jane Doe', 'nomor' => '628987654321'],
], 'device_id');   // max 1000 contacts

// Basic
$result = $client->generateOTP('device_id', '628123456789');

// With options
$result = $client->generateOTP('device_id', '628123456789', [
    'otp_length'       => 6,           // 4-20, default 8
    'otp_type'         => 'numeric',   // numeric | alphabetic | alphanumeric
    'customOtpText'    => 'Your code',
    'customOtpMessage' => 'Your OTP is {otp}. Valid for 5 minutes.',
]);

$result = $client->validateOTP('device_id', '628123456789', '123456');

// Kirimi provider (Rp 595 per delivered OTP)
$result = $client->sendOtpV2('628123456789', [
    'method'   => 'whatsapp',
    'app_name' => 'MyApp',
]);

// Your own connected device (free)
$result = $client->sendOtpV2('628123456789', [
    'method'         => 'device',
    'device_id'      => 'device_id',
    'custom_message' => 'Your OTP is {{otp}}',
]);

// Your own WABA + AUTHENTICATION template (free)
$result = $client->sendOtpV2('628123456789', [
    'method'        => 'waba_user',
    'waba_id'       => 'waba_id',
    'template_name' => 'otp_auth',
]);

$result = $client->verifyOtpV2('628123456789', '123456');

$create = $client->otpReverseCreate('628123456789', 'device_id', [
    'app_name'        => 'MyApp',
    'callback_url'    => 'https://example.com/callback',
    'custom_message'  => 'Send {{token}} from {{phone}} to verify.',
    'success_message' => 'Verified!',
    'failure_message' => 'Verification failed.',
]);

$status = $client->otpReverseStatus($create['token']);   // pending|verified|phone_mismatch|expired

$packages = $client->listPackages();

$deposit = $client->createDeposit(50000);        // min 100
$status  = $client->depositStatus($ref);
$cancel  = $client->cancelDeposit($ref);         // must be unpaid

$all  = $client->listDeposits();
$paid = $client->listDeposits(['status' => 'paid', 'page' => 1, 'limit' => 10]);

$status = $client->healthCheck();
print_r($status);



irimi\KirimiClient;
use Kirimi\KirimiException;

$client = new KirimiClient('your_user_code', 'your_secret');

try {
    $result = $client->sendMessage(
        'your_device_id',
        '628123456789',
        'Welcome to our service! 🎉'
    );
    echo "Message sent successfully: " . json_encode($result) . PHP_EOL;
} catch (KirimiException $e) {
    echo "Failed to send message: " . $e->getMessage() . PHP_EOL;
}



irimi\Services\OTPService;

$otpService = new OTPService('your_user_code', 'your_secret', 'your_device_id');

// Send OTP
$result = $otpService->sendVerificationCode('628123456789');
if ($result['success']) {
    echo "OTP sent successfully!" . PHP_EOL;
} else {
    echo "Failed to send OTP: " . $result['error'] . PHP_EOL;
}

// Verify OTP (user provides the code)
$verifyResult = $otpService->verifyCode('628123456789', '123456');
if ($verifyResult['success'] && $verifyResult['verified']) {
    echo "OTP verified successfully!" . PHP_EOL;
} else {
    echo "OTP verification failed!" . PHP_EOL;
}



irimi\Services\NotificationService;

$notificationService = new NotificationService('your_user_code', 'your_secret', 'your_device_id');

// Send welcome message
$result = $notificationService->sendWelcomeMessage('628123456789', 'John Doe');

// Send order confirmation
$result = $notificationService->sendOrderConfirmation(
    '628123456789',
    'ORD-001',
    ['Product A', 'Product B', 'Product C']
);

// Send invoice with document
$result = $notificationService->sendInvoiceWithDocument(
    '628123456789',
    'INV-001',
    'https://example.com/invoice.pdf'
);

// Send appointment reminder
$result = $notificationService->sendAppointmentReminder(
    '628123456789',
    '2024-01-15',
    '10:00 AM',
    'Main Office'
);



// In your Laravel service provider or controller
use Kirimi\KirimiClient;

class WhatsAppService
{
    private KirimiClient $kirimi;

    public function __construct()
    {
        $this->kirimi = new KirimiClient(
            config('services.kirimi.user_code'),
            config('services.kirimi.secret')
        );
    }

    public function sendNotification(string $phone, string $message): bool
    {
        try {
            $this->kirimi->sendMessage(
                config('services.kirimi.device_id'),
                $phone,
                $message
            );
            return true;
        } catch (KirimiException $e) {
            Log::error('WhatsApp notification failed: ' . $e->getMessage());
            return false;
        }
    }
}

// In config/services.php
return [
    'kirimi' => [
        'user_code' => env('KIRIMI_USER_CODE'),
        'secret' => env('KIRIMI_SECRET_KEY'),
        'device_id' => env('KIRIMI_DEVICE_ID'),
    ],
];

use Kirimi\KirimiException;

try {
    $client->sendMessage('device_id', '628123456789', 'Hello');
} catch (KirimiException $e) {
    switch ($e->getStatusCode()) {
        case 401:
            echo 'Invalid credentials';
            break;
        case 402:
            echo 'Insufficient balance';
            break;
        case 429:
            echo 'Rate limited, retry later';
            break;
        default:
            echo 'Request failed: ' . $e->getMessage();
    }
}

// Good practice: use environment variables
$client = new KirimiClient(
    $_ENV['KIRIMI_USER_CODE'],
    $_ENV['KIRIMI_SECRET_KEY']
);
bash
composer 
bash
# Set your credentials as environment variables
export KIRIMI_USER_CODE="your_user_code"
export KIRIMI_SECRET_KEY="your_secret_key"
export KIRIMI_DEVICE_ID="your_device_id"
export TEST_PHONE="628123456789"

# Run the example
composer run example
# or
php examples/demo.php