# Rebrand / Reseller API

Every rebrand has its own API token (visible in the Rebrand Panel -> Settings ->
"Reseller API" card, with a "Regenerate token" button). The token is what
identifies which rebrand the request belongs to - no rebrand ID is sent in the
request body.

**Base URL:** `https://your-domain.com/api/rebrand-api.php`
**Auth:** header `X-Api-Secret: <YOUR_TOKEN>` or query string `?token=<YOUR_TOKEN>`

Requests are POST with JSON body (except `sellauth`, which accepts GET or POST).
Responses are JSON (except `sellauth`, which defaults to plain text so it plugs
straight into storefronts that expect one serial per line).

If the token is missing or wrong you get `401 {"error":"Invalid API token"}`.

---

## SellAuth / storefront delivery (`?r=sellauth`)

Drop this URL into any product's external / serials-API delivery config in
SellAuth (or Sellix, Shoppy, etc.). On every purchase the store calls the URL
and receives fresh key(s) which are delivered to the buyer automatically.

```
GET  https://your-domain.com/api/rebrand-api.php?r=sellauth
     &token=<YOUR_TOKEN>
     &product_id=<ID>
     &days=<DURATION_DAYS>
     &qty={quantity}           # SellAuth substitutes {quantity}
     &format=lines             # lines | json | array   (default: lines)
```

Response formats:

| `format`         | Response body                            |
|------------------|------------------------------------------|
| `lines` (default)| `KEY1\nKEY2\n...` (`text/plain`)         |
| `json`           | `{"serials":["KEY1","KEY2"]}`            |
| `array`          | `["KEY1","KEY2"]`                        |

On error: HTTP 400 with `ERROR: <message>` in plain text.

The exact URL for each of your products is pre-built for you in the Rebrand
Panel -> Settings -> "Reseller API" card. Just copy and paste.

---

## Endpoints (JSON)

### `?r=products` - list products you can sell
Body: `{}`
```json
{ "products": [ { "product_id": 4, "name": "Product Name", "key_limit": 100 } ] }
```

### `?r=generate_key` - generate license key(s)
```json
{ "duration_days": 30, "product_id": 4, "count": 1 }
```
Response (count=1): `{"key":"20260815-A3B8C9D1-K2L4-7XQ-F1"}`
Response (count>1): `{"keys":["...","..."]}`

`count` is clamped to 1..50. `quantity` and `qty` are accepted as aliases.

### `?r=users` - list users
Body: `{}`
```json
{ "users": [ { "id":1, "username":"player1", "hwid":"...", "status":"active",
               "has_sub":1, "sub_expiry":"...", "last_login":"...", "last_ip":"..." } ] }
```

### `?r=user_info` - single-user info
Body: `{"username":"player1"}`
Response includes a `products` array with per-product active/expiry status.

### `?r=create_user`
```json
{ "username":"newplayer", "password":"securepass123", "product_id":4 }
```

### `?r=redeem`
```json
{ "username":"player1", "license_key":"KEY..." }
```

### `?r=reset_hwid`
`{"username":"player1"}`

### `?r=reset_password`
`{"username":"player1","new_password":"newpass"}`

### `?r=ban` / `?r=unban`
`{"username":"player1"}`

### `?r=stats` - dashboard stats
```json
{ "total": 50, "active": 20, "used": 30, "recent": 5,
  "by_duration": [{"duration_days":30,"count":15}],
  "total_users": 25, "active_users": 18 }
```

---

## cURL

```bash
# List users
curl -X POST "https://your-domain.com/api/rebrand-api.php?r=users" \
  -H "Content-Type: application/json" \
  -H "X-Api-Secret: YOUR_TOKEN" -d '{}'

# Generate a 30-day key
curl -X POST "https://your-domain.com/api/rebrand-api.php?r=generate_key" \
  -H "Content-Type: application/json" \
  -H "X-Api-Secret: YOUR_TOKEN" \
  -d '{"duration_days":30,"product_id":4}'

# SellAuth-style single call (URL only, no header)
curl "https://your-domain.com/api/rebrand-api.php?r=sellauth&token=YOUR_TOKEN&product_id=4&days=30&qty=1"
```

---

## Discord bot example (discord.js v14)

```js
const { Client, GatewayIntentBits, SlashCommandBuilder, REST, Routes } = require('discord.js');

const API_URL   = 'https://your-domain.com/api/rebrand-api.php';
const API_TOKEN = 'YOUR_TOKEN';

async function api(endpoint, body = {}) {
  const res = await fetch(`${API_URL}?r=${endpoint}`, {
    method: 'POST',
    headers: { 'Content-Type': 'application/json', 'X-Api-Secret': API_TOKEN },
    body: JSON.stringify(body),
  });
  return res.json();
}

// ...same slash commands as before, using api('...') calls
```

---

## Errors

| HTTP | Meaning                                     |
|------|---------------------------------------------|
| 401  | Missing/invalid token                       |
| 400  | Missing/invalid parameters, no product access, key limit reached |
| 404  | Unknown endpoint (response lists valid ones)|


### Lifetime licenses

Pass the explicit integer `duration_days: 0` to key-generation routes for a lifetime key. Positive durations remain timed licenses. Invalid or negative durations are rejected. Lifetime keys retain the normal product permissions and key quotas.
