# WhatsApp Gemini Gateway - API Documentation

## Base URL
```
http://api.the-k.net
```

---

## 1. GET /status
**Cek status koneksi WhatsApp**

### Response Success (200)
```json
{
  "status": true,
  "connection": "connected"
}
```

### Response Error (200)
```json
{
  "status": true,
  "connection": "disconnected"
}
```

### Connection Status Values
- `connected` - WhatsApp sudah terhubung
- `disconnected` - WhatsApp terputus
- `connecting` - WhatsApp sedang menyambung
- `qr` - Perlu scan QR code

### Contoh cURL
```bash
curl http://api.the-k.net/status
```

---

## 2. GET /qr
**Dapatkan QR code untuk scan WhatsApp**

### Response Success (200)
Menampilkan halaman HTML dengan QR code image

### Response Error (404)
```json
{
  "status": false,
  "message": "QR tidak tersedia. Mungkin sudah terhubung atau belum digenerate.",
  "connection": "connected"
}
```

### Contoh
```
Buka di browser: http://api.the-k.net/qr
Scan QR code dengan WhatsApp (Settings > Linked Devices > Link a Device)
```

---

## 3. POST /send-message
**Kirim pesan teks ke WhatsApp**

### Request
```
POST http://api.the-k.net/send-message
Content-Type: application/json

{
  "number": "628123456789",
  "message": "Halo, ini test pesan"
}
```

### Parameter
- `number` (string, required): Nomor WhatsApp tujuan
  - Format: `628123456789` atau `08123456789`
  - Atau: `628123456789@s.whatsapp.net`
- `message` (string, required): Isi pesan

### Response Success (200)
```json
{
  "status": true,
  "message": "Pesan terkirim",
  "data": {
    "key": {
      "remoteJid": "628123456789@s.whatsapp.net",
      "fromMe": true,
      "id": "XXXXXX"
    }
  }
}
```

### Response Error (400)
```json
{
  "status": false,
  "message": "Field \"number\" dan \"message\" wajib diisi"
}
```

### Response Error (500)
```json
{
  "status": false,
  "message": "WhatsApp belum terhubung. Scan QR dulu lewat GET /qr"
}
```

### Contoh cURL
```bash
curl -X POST http://api.the-k.net/send-message \
  -H "Content-Type: application/json" \
  -d '{
    "number": "628123456789",
    "message": "Halo"
  }'
```

### Contoh JavaScript
```javascript
fetch('http://api.the-k.net/send-message', {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({
    number: '628123456789',
    message: 'Halo'
  })
})
.then(res => res.json())
.then(data => console.log(data))
```

---

## 4. POST /auto-reply
**Nyalakan atau matikan auto-reply otomatis**

### Request
```
POST http://api.the-k.net/auto-reply
Content-Type: application/json

{
  "enabled": true
}
```

### Parameter
- `enabled` (boolean, required): `true` untuk nyalakan, `false` untuk matikan

### Response Success (200)
```json
{
  "status": true,
  "autoReplyEnabled": true
}
```

### Response Error (400)
```json
{
  "status": false,
  "message": "Field \"enabled\" harus true/false"
}
```

### Contoh cURL
```bash
# Nyalakan auto-reply
curl -X POST http://api.the-k.net/auto-reply \
  -H "Content-Type: application/json" \
  -d '{"enabled": true}'

# Matikan auto-reply
curl -X POST http://api.the-k.net/auto-reply \
  -H "Content-Type: application/json" \
  -d '{"enabled": false}'
```

---

## 5. GET /auto-reply
**Cek status auto-reply sekarang**

### Response Success (200)
```json
{
  "status": true,
  "autoReplyEnabled": true
}
```

### Contoh cURL
```bash
curl http://api.the-k.net/auto-reply
```

---

## 6. POST /reset-session
**Reset percakapan Gemini untuk nomor tertentu (mulai obrolan baru)**

### Request
```
POST http://api.the-k.net/reset-session
Content-Type: application/json

{
  "number": "628123456789"
}
```

### Parameter
- `number` (string, required): Nomor WhatsApp
  - Format: `628123456789`, `08123456789`, atau `628123456789@s.whatsapp.net`

### Response Success (200)
```json
{
  "status": true,
  "message": "Sesi percakapan direset"
}
```

### Response Error (400)
```json
{
  "status": false,
  "message": "Field \"number\" wajib diisi"
}
```

### Contoh cURL
```bash
curl -X POST http://api.the-k.net/reset-session \
  -H "Content-Type: application/json" \
  -d '{"number": "628123456789"}'
```

---

## 7. POST /logout
**Logout dari WhatsApp (perlu scan QR ulang setelah ini)**

### Request
```
POST http://api.the-k.net/logout
```

### Response Success (200)
```json
{
  "status": true,
  "message": "Berhasil logout"
}
```

### Contoh cURL
```bash
curl -X POST http://api.the-k.net/logout
```

---

## Error Handling

Semua error response memiliki format:
```json
{
  "status": false,
  "message": "Deskripsi error"
}
```

### HTTP Status Codes
- `200` - Request berhasil
- `400` - Request tidak valid (missing parameter, format salah)
- `401` - API key tidak valid (jika API_KEY dikonfigurasi di .env)
- `404` - Resource tidak ditemukan
- `500` - Server error

---

## Contoh Integrasi Laravel

### 1. Kirim Pesan
```php
$response = Http::post('http://api.the-k.net/send-message', [
    'number' => '628123456789',
    'message' => 'Halo dari Laravel'
]);

if ($response->json('status')) {
    echo "Pesan berhasil terkirim";
}
```

### 2. Cek Status
```php
$status = Http::get('http://api.the-k.net/status')->json();

if ($status['connection'] === 'connected') {
    echo "WhatsApp terhubung";
}
```

### 3. Nyalakan Auto-Reply
```php
Http::post('http://api.the-k.net/auto-reply', [
    'enabled' => true
]);
```

---

## Environment Variables (.env)

```env
PORT=3000
GEMINI_API_KEY=YOUR_API_KEY
GEMINI_MODEL=gemini-2.5-flash
GEMINI_SYSTEM_INSTRUCTION=Kamu adalah asisten yang ramah...
AUTO_REPLY_ENABLED=true
REPLY_IN_GROUP=false
API_KEY=
```

---

## Troubleshooting

### WhatsApp terputus terus
- Scan QR code ulang di `/qr`
- Pastikan `.env` setting benar
- Restart server

### Auto-reply tidak bekerja
- Cek `/auto-reply` apakah enabled
- Pastikan Gemini API key valid
- Cek logs di terminal/hosting

### QR code tidak muncul
- Hapus folder `credentials`
- Restart server
- Buka `/qr` ulang

### Nomor tujuan tidak valid
- Gunakan format: `628xxxxxxxxx` (tanpa 0 di depan)
- Contoh: `628123456789` bukan `08123456789`

---

## Rate Limiting

Tidak ada rate limit yang dikonfigurasi. Untuk production, tambahkan rate limiting di `.env` atau ubah `routes.js`.

---

## Support

Dokumentasi ini valid untuk WhatsApp Gateway v1.0.0
Diupdate: 2025
