PHPackages                             sontus/laravel-niaga-sms - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [HTTP &amp; Networking](/categories/http)
4. /
5. sontus/laravel-niaga-sms

ActiveLibrary[HTTP &amp; Networking](/categories/http)

sontus/laravel-niaga-sms
========================

Laravel package for sending SMS using SMS Niaga API

v1.0.0(9mo ago)060MITPHPPHP ^8.2

Since Aug 12Pushed 9mo agoCompare

[ Source](https://github.com/sontus/laravel-niaga-sms)[ Packagist](https://packagist.org/packages/sontus/laravel-niaga-sms)[ RSS](/packages/sontus-laravel-niaga-sms/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (1)Dependencies (4)Versions (2)Used By (0)

Laravel Niaga SMS
=================

[](#laravel-niaga-sms)

[![Latest Version on Packagist](https://camo.githubusercontent.com/989dd5da3774a534cf6dd6d35f1102788fda8a22a6d5feffef07594588a8da4d/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f736f6e7475732f6c61726176656c2d6e696167612d736d732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/sontus/laravel-niaga-sms)[![Total Downloads](https://camo.githubusercontent.com/53029453f6d6f05a23513e6a2094bbc937e87f6ef1afca73d8634a9b53c62a61/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f736f6e7475732f6c61726176656c2d6e696167612d736d732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/sontus/laravel-niaga-sms)[![License](https://camo.githubusercontent.com/bd945824995b45d3a646cb1cc08db8f5ca39d5d35b4454c6cf09eabe4bf5efd8/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f736f6e7475732f6c61726176656c2d6e696167612d736d732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/sontus/laravel-niaga-sms)

A Laravel package for sending SMS messages using the SMS Niaga API. This package provides a clean, fluent interface for integrating SMS functionality into your Laravel applications.

Features
--------

[](#features)

- 🚀 Simple and intuitive API
- 📱 Send SMS to single or multiple recipients
- 🔍 Preview messages before sending
- ✅ Comprehensive validation
- 🎯 Fluent request builder
- 🛡️ Robust error handling
- 📊 Detailed response data
- 🧪 Full test coverage

Requirements
------------

[](#requirements)

- PHP 8.0 or higher
- Laravel 9.0 or higher
- SMS Niaga account with API token and Sender\_id

Installation
------------

[](#installation)

Install the package via Composer:

```
composer require sontus/laravel-niaga-sms
```

### Laravel Auto-Discovery

[](#laravel-auto-discovery)

The package will automatically register its service provider and facade.

### Manual Registration (if needed)

[](#manual-registration-if-needed)

If auto-discovery is disabled, manually register the service provider in `config/app.php`:

```
'providers' => [
    // ...
    Sontus\LaravelNiagaSms\NiagaSmsServiceProvider::class,
],

'aliases' => [
    // ...
    'NiagaSms' => Sontus\LaravelNiagaSms\Facades\NiagaSms::class,
],
```

Configuration
-------------

[](#configuration)

Publish the configuration file:

```
php artisan vendor:publish --provider="Sontus\LaravelNiagaSms\NiagaSmsServiceProvider" --tag="config"
```

Add your SMS Niaga API credentials to your `.env` file:

```
SMS_NIAGA_BASE_URL=https://manage.niagaSms.xyz
SMS_NIAGA_API_TOKEN=your-api-token-here
SMS_NIAGA_TIMEOUT=30
NIAGA_SMS_SENDER_ID=your-sender_id-here
```

> **Note:** Get your API token from your SMS Niaga dashboard under Profile &gt; API Token.

Quick Start
-----------

[](#quick-start)

### Send SMS to a Single Number

[](#send-sms-to-a-single-number)

```
use Sontus\LaravelNiagaSms\Facades\NiagaSms;

$response = NiagaSms::sendToSingle('60123456789', 'Hello World!', 'SENDER');

if ($response->isSuccessful()) {
    echo "SMS sent successfully!";
    echo "UUID: " . $response->getUuid();
    echo "Cost: RM" . $response->getTotalCharge();
}
```

### Send SMS to Multiple Numbers

[](#send-sms-to-multiple-numbers)

```
$phones = ['60123456789', '60987654321', '60555666777'];
$response = NiagaSms::sendToMultiple($phones, 'Bulk message to everyone!', 'BULK');

echo "Sent to " . $response->getTotalNumbers() . " numbers";
echo "Total cost: RM" . $response->getTotalCharge();
```

Advanced Usage
--------------

[](#advanced-usage)

### Using SmsRequest Builder

[](#using-smsrequest-builder)

```
use Sontus\LaravelNiagaSms\DataObjects\SmsRequest;
use Sontus\LaravelNiagaSms\Facades\NiagaSms;

$request = SmsRequest::create()
    ->setBody('Your verification code is: 123456')
    ->addPhone('60123456789')
    ->addPhone('60987654321')
    ->setSenderId('VERIFY');

$response = NiagaSms::send($request);
```

### Preview Messages (No SMS Sent)

[](#preview-messages-no-sms-sent)

```
$request = SmsRequest::create()
    ->setBody('This is a preview message')
    ->addPhone('60123456789')
    ->setSenderId('PREVIEW');

$response = NiagaSms::preview($request);

// Check message details without sending
echo "Message pages: " . $response->getMessagePage();
echo "Character count: " . $response->getCharCounts();
echo "Estimated cost: RM" . $response->getTotalCharge();
```

### Error Handling

[](#error-handling)

```
use Sontus\LaravelNiagaSms\Exceptions\NiagaSmsException;

try {
    $response = NiagaSms::sendToSingle('60123456789', 'Hello World!');

    if ($response->isSuccessful()) {
        // Success logic
        $uuid = $response->getUuid();
        $cost = $response->getTotalCharge();
        $balance = $response->getCreditBalanceAfter();
    } else {
        // API returned error
        echo "Error: " . $response->getResponseMessage();
    }

} catch (NiagaSmsException $e) {
    // Network or HTTP errors
    echo "Exception: " . $e->getMessage();

    if ($errorData = $e->getErrorData()) {
        // Detailed error information from API
        var_dump($errorData);
    }
}
```

Controller Examples
-------------------

[](#controller-examples)

### Basic SMS Controller

[](#basic-sms-controller)

```
