NgirimWA Docs
API

Contoh Kode

Wrapper sederhana untuk API NgirimWA dalam JavaScript/Node.js, Python, dan PHP.

Snippet siap pakai untuk endpoint paling sering: kirim teks, kirim media, kirim template, cek status. Untuk endpoint lain (tombol, reaction, dll) cara pakainya identik — ganti path & body sesuai dokumentasi tiap endpoint.

JavaScript / Node.js

import axios from 'axios';
import { randomUUID } from 'crypto';

const api = axios.create({
  baseURL: 'https://dash.ngirimwa.com/api/v1',
  headers: { 'x-api-key': process.env.NGIRIMWA_API_KEY }
});

// Helper — tambahkan Idempotency-Key di setiap POST
const send = (path, body) =>
  api.post(path, body, { headers: { 'Idempotency-Key': randomUUID() } }).then(r => r.data);

// 1. Cek status device
const status = await api.get('/devices/status').then(r => r.data);
if (status.data.status !== 'connected') throw new Error('Device offline');

// 2. Kirim teks
await send('/messages/send', { to: '628123456789', message: 'Halo!' });

// 3. Kirim media
await send('/messages/send', {
  to: '628123456789',
  media: 'https://domain-anda.com/produk.jpg',
  media_type: 'image',
  message: 'Produk terbaru!'
});

// 4. Kirim template dengan variabel
await send('/messages/send-template', {
  to: '628123456789',
  template_id: '550e8400-e29b-41d4-a716-446655440000',
  variables: { nama: 'Budi', nomor_pesanan: 'ORD-001' }
});

// 5. Kirim tombol quick-reply
await send('/messages/quick-reply', {
  to: '628123456789',
  text: 'Konfirmasi pesanan?',
  buttons: [
    { kind: 'reply', text: 'Ya', id: 'yes' },
    { kind: 'reply', text: 'Tidak', id: 'no' }
  ]
});

Python

import os, uuid, requests

API_KEY = os.environ['NGIRIMWA_API_KEY']
BASE    = 'https://dash.ngirimwa.com/api/v1'

def _headers(idempotent=False):
    h = {'x-api-key': API_KEY, 'Content-Type': 'application/json'}
    if idempotent:
        h['Idempotency-Key'] = str(uuid.uuid4())
    return h

def status() -> dict:
    return requests.get(f'{BASE}/devices/status', headers=_headers()).json()

def send(path: str, body: dict) -> dict:
    return requests.post(f'{BASE}{path}', json=body, headers=_headers(idempotent=True)).json()

# Contoh penggunaan
assert status()['data']['status'] == 'connected', 'Device offline'

send('/messages/send', {'to': '628123456789', 'message': 'Halo dari Python!'})

send('/messages/send-template', {
    'to': '628123456789',
    'template_id': '550e8400-e29b-41d4-a716-446655440000',
    'variables': {'nama': 'Budi'}
})

send('/messages/quick-reply', {
    'to': '628123456789',
    'text': 'Mau lanjut order?',
    'buttons': [
        {'kind': 'reply', 'text': 'Ya', 'id': 'yes'},
        {'kind': 'url',   'text': 'Web', 'url': 'https://toko.com'}
    ]
})

PHP

<?php
class NgirimWA {
    public function __construct(
        private string $apiKey,
        private string $baseUrl = 'https://dash.ngirimwa.com/api/v1'
    ) {}

    private function request(string $method, string $path, ?array $body = null): array {
        $ch = curl_init($this->baseUrl . $path);
        $headers = [
            'x-api-key: ' . $this->apiKey,
            'Content-Type: application/json',
        ];
        if ($method === 'POST' || $method === 'DELETE') {
            $headers[] = 'Idempotency-Key: ' . bin2hex(random_bytes(16));
        }
        curl_setopt_array($ch, [
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_CUSTOMREQUEST  => $method,
            CURLOPT_HTTPHEADER     => $headers,
            CURLOPT_POSTFIELDS     => $body ? json_encode($body) : null,
        ]);
        $res = curl_exec($ch);
        curl_close($ch);
        return json_decode($res, true);
    }

    public function status(): array { return $this->request('GET', '/devices/status'); }

    public function send(string $to, string $message): array {
        return $this->request('POST', '/messages/send', compact('to', 'message'));
    }

    public function sendMedia(string $to, string $mediaUrl, string $type, string $caption = ''): array {
        return $this->request('POST', '/messages/send', [
            'to' => $to, 'media' => $mediaUrl, 'media_type' => $type, 'message' => $caption
        ]);
    }

    public function sendTemplate(string $to, string $templateId, array $variables = []): array {
        return $this->request('POST', '/messages/send-template', [
            'to' => $to, 'template_id' => $templateId, 'variables' => $variables
        ]);
    }
}

// Pemakaian
$wa = new NgirimWA($_ENV['NGIRIMWA_API_KEY']);
if ($wa->status()['data']['status'] === 'connected') {
    $wa->send('628123456789', 'Halo dari PHP!');
    $wa->sendTemplate('628123456789', '550e8400-...', ['nama' => 'Budi']);
}

Pola Wajib

Apapun bahasa yang Anda pakai, terapkan 3 hal ini:

  1. Cek /devices/status sebelum kirim massal — hindari ratusan request gagal karena device offline.
  2. Selalu kirim Idempotency-Key pada POST — pakai UUID v4 per request. Mencegah pesan dobel saat network retry.
  3. Handle 429 (rate limit + quota) — back-off eksponen atau queue. Limit: 200 request/jam per IP + kuota bulanan sesuai paket.

Dukungan

  • 📧 Email: support@yudhavro.com
  • 📞 WhatsApp: 6285183380099
  • 💬 Live chat: tersedia di dashboard

On this page