# Dokumentasi API REXYZ

Beli nomor virtual dan ambil OTP dari aplikasi atau bot kamu sendiri. Semua respons berformat JSON.

- Base URL: `https://rexyzotpid.com/api/v1`
- Dokumentasi online: https://rexyzotpid.com/developer/docs

## Mulai cepat

1. Daftar/masuk, lalu buat API key di menu **Lainnya -> API Key**. Isi saldo wallet dulu.
2. Ambil negara, layanan, dan produk dari `GET /catalog`.
3. Beli nomor dengan `POST /orders`. Saldo terpotong sebesar harga API.
4. Cek `GET /orders/{id}` tiap 3-5 detik sampai `otpCode` terisi.
5. Kalau OTP tidak datang, batalkan atau tunggu kedaluwarsa. Saldo kembali otomatis.

## Autentikasi

Kirim API key di setiap request, lewat salah satu header:

```
Authorization: Bearer rxz_xxxxxxxxxxxxxxxx
X-API-Key: rxz_xxxxxxxxxxxxxxxx
```

Key hanya tampil sekali saat dibuat dan bisa dicabut kapan saja. Simpan di server kamu, jangan ditaruh di aplikasi publik (web atau APK).
Batas **120 request per menit** per key; lewat dari itu kamu dapat HTTP 429 dan header `Retry-After`.

## Endpoint

| Method | Path | Keterangan |
|---|---|---|
| GET | /balance | Saldo wallet pemilik key |
| GET | /catalog?server=1 | Daftar negara (server 1 = SMSCODE, 2 = Grizzly) |
| GET | /catalog?server=1&country_id=X | Daftar layanan |
| GET | /catalog?server=1&country_id=X&service_id=Y | Operator + produk; kolom `price` = harga API yang kamu bayar |
| POST | /orders | Beli nomor |
| GET | /orders | Riwayat order milik key ini |
| GET | /orders/{id} | Status, nomor, dan OTP satu order |
| POST | /orders/{id} | Aksi: `cancel`, `finish`, `resend` |

### POST /orders

Body JSON:

| Field | Keterangan |
|---|---|
| productId | wajib, id produk dari katalog |
| countryId | wajib, id negara |
| platformId | wajib, id layanan (service_id di katalog) |
| operatorId | opsional |
| server | 1 atau 2 (wajib kalau admin memakai dua server) |
| quantity | opsional, 1-5 nomor sekaligus; hasilnya ada di array `orders` |
| idempotencyKey | sangat disarankan: teks unik (huruf, angka, _ atau -, maks 128). Request ulang dengan key sama tidak membuat order ganda |

Contoh respons:

```json
{
  "success": true,
  "order": {
    "id": "b3f1c0de-1111-2222-3333-444455556666",
    "status": "PROCESSING",
    "productName": "WhatsApp - Indonesia",
    "phoneNumber": "628123456789",
    "otpCode": null,
    "otpMessage": null,
    "price": 3372,
    "notes": null,
    "expiresAt": "2026-10-30T12:30:00.000Z",
    "createdAt": "2026-10-30T12:20:00.000Z",
    "updatedAt": "2026-10-30T12:20:01.000Z"
  }
}
```

### POST /orders/{id}

Body `{"action":"cancel"}`. Aksi lain: `finish` (selesai) dan `resend` (minta kirim ulang SMS, jika didukung).
Pembatalan hanya bisa setelah jeda minimum dari provider; sebelum itu kamu dapat HTTP 409 berisi sisa detiknya.

## Status order

| Status | Arti | Akhir? |
|---|---|---|
| PENDING | Order dibuat, sedang diverifikasi sistem / belum dapat nomor | Tidak |
| PROCESSING | Nomor sudah didapat, menunggu OTP | Tidak |
| OTP_RECEIVED | OTP sudah masuk, baca di otpCode | Tidak |
| SUCCESS | Selesai, OTP terpakai | Ya |
| CANCELLED | Dibatalkan sebelum OTP masuk, saldo kembali | Ya |
| EXPIRED | Waktu habis tanpa OTP, saldo kembali | Ya |
| FAILED | Gagal diproses provider, saldo kembali | Ya |
| REFUNDED | Saldo sudah dikembalikan | Ya |

Kalau status akhir tanpa otpCode (CANCELLED, EXPIRED, FAILED, REFUNDED), berhenti menunggu: tidak akan ada OTP dan kamu tidak dikenai biaya.

## Kode error

Saat gagal, respons berbentuk `{ "success": false, "message": "..." }` dengan kode HTTP berikut:

| HTTP | Penyebab | Yang dilakukan |
|---|---|---|
| 400 | Data tidak valid | Periksa body dan parameter |
| 401 | API key salah, dicabut, atau akun nonaktif | Cek key; buat yang baru jika perlu |
| 402 | Saldo tidak cukup | Deposit dulu, lalu ulangi |
| 404 | Order tidak ditemukan / bukan milik key ini | Cek id order |
| 409 | Aksi belum bisa (mis. batal terlalu cepat) | Coba lagi setelah beberapa detik |
| 429 | Melebihi 120 request/menit atau terlalu banyak order | Tunggu sesuai header Retry-After |
| 502 | Provider gagal / stok habis; saldo sudah dikembalikan | Coba produk atau server lain |
| 503 | API dimatikan, server tidak tersedia, atau maintenance | Coba lagi nanti |
| 500 | Gangguan di sisi server | Ulangi dengan idempotencyKey yang sama |

## Contoh kode

### curl

```bash
KEY=rxz_KEYKAMU

# 1) saldo
curl -H "Authorization: Bearer $KEY" https://rexyzotpid.com/api/v1/balance

# 2) negara -> layanan -> produk
curl -H "Authorization: Bearer $KEY" "https://rexyzotpid.com/api/v1/catalog?server=1"
curl -H "Authorization: Bearer $KEY" "https://rexyzotpid.com/api/v1/catalog?server=1&country_id=6"
curl -H "Authorization: Bearer $KEY" "https://rexyzotpid.com/api/v1/catalog?server=1&country_id=6&service_id=1"

# 3) beli nomor
curl -X POST https://rexyzotpid.com/api/v1/orders -H "Authorization: Bearer $KEY" -H "Content-Type: application/json" \
  -d '{"server":1,"countryId":6,"platformId":1,"productId":123,"idempotencyKey":"order-0001"}'

# 4) cek status + OTP
curl -H "Authorization: Bearer $KEY" https://rexyzotpid.com/api/v1/orders/ORDER_ID

# 5) batalkan (saldo kembali kalau OTP belum masuk)
curl -X POST https://rexyzotpid.com/api/v1/orders/ORDER_ID -H "Authorization: Bearer $KEY" -H "Content-Type: application/json" -d '{"action":"cancel"}'
```

### Python

```python
import time, uuid, requests

BASE = "https://rexyzotpid.com/api/v1"
H = {"Authorization": "Bearer rxz_KEYKAMU"}

def beli(server, country_id, service_id, product_id):
    r = requests.post(BASE + "/orders", headers=H, timeout=30, json={
        "server": server, "countryId": country_id, "platformId": service_id,
        "productId": product_id, "idempotencyKey": str(uuid.uuid4()),
    })
    data = r.json()
    if not data.get("success"):
        raise RuntimeError(data.get("message"))
    return data["order"]

def tunggu_otp(order_id, batas=300):
    mulai = time.time()
    while time.time() - mulai < batas:
        order = requests.get(BASE + "/orders/" + order_id, headers=H, timeout=30).json()["order"]
        if order["otpCode"]:
            return order["otpCode"]
        if order["status"] in ("FAILED", "CANCELLED", "EXPIRED", "REFUNDED"):
            return None
        time.sleep(4)
    return None

order = beli(1, 6, 1, 123)
print("Nomor:", order["phoneNumber"])
print("OTP:", tunggu_otp(order["id"]))
```

### Node.js (18+)

```js
const BASE = "https://rexyzotpid.com/api/v1";
const HEADERS = { Authorization: "Bearer rxz_KEYKAMU", "Content-Type": "application/json" };
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));

async function beli(server, countryId, platformId, productId) {
  const res = await fetch(BASE + "/orders", {
    method: "POST", headers: HEADERS,
    body: JSON.stringify({ server, countryId, platformId, productId, idempotencyKey: crypto.randomUUID() }),
  });
  const data = await res.json();
  if (!data.success) throw new Error(data.message);
  return data.order;
}

async function tungguOtp(id, batasMs = 300000) {
  const mulai = Date.now();
  while (Date.now() - mulai < batasMs) {
    const { order } = await (await fetch(BASE + "/orders/" + id, { headers: HEADERS })).json();
    if (order.otpCode) return order.otpCode;
    if (["FAILED", "CANCELLED", "EXPIRED", "REFUNDED"].includes(order.status)) return null;
    await sleep(4000);
  }
  return null;
}

const order = await beli(1, 6, 1, 123);
console.log("Nomor:", order.phoneNumber);
console.log("OTP:", await tungguOtp(order.id));
```

## Tips

- Selalu kirim `idempotencyKey` unik tiap order. Kalau koneksi putus, ulangi request dengan key yang sama; kamu tidak akan dikenai biaya dua kali.
- Cek status tiap 3-5 detik, jangan lebih rapat. Jangan menembak katalog tiap order; simpan hasilnya beberapa menit.
- Harga bisa berubah mengikuti stok provider. Ambil harga terbaru dari `/catalog` sebelum membeli.
- Order PENDING yang tidak pernah mendapat nomor otomatis dikembalikan saldonya oleh sistem.
- Key bocor? Cabut di menu API Key dan buat yang baru.
