# VietFuel API — Terminal CLI Documentation

> Bilingual command-line interface (Vietnamese default, `--lang en` for English) for Vietnam retail fuel prices across 12 distributors & 63 legacy provinces (34 post-merger). Includes an interactive menu, responsive Unicode tables, and `--json` output for scripts and AI agents.

Vietnamese version: `/cli.md`. Full API docs: `/docs.en.md`. LLM index: `/llms.txt`.

## Install & Quick Start

```bash
# Install globally via npm (zero dependencies, Node >= 18)
npm install -g vietfuel

# Or run directly without installing
npx vietfuel prices

# Launch interactive menu (auto-detects :8787 -> :3000 -> production)
vietfuel --lang en

# One-shot command with raw JSON output
vietfuel prices --json
```

> When developing inside the repository, you can also run `npm run cli -- <command>`.

## Environment Variables & Auto-Detection

| Variable | Default | Description |
| --- | --- | --- |
| `VIETFUEL_API_URL` | Auto-probes `:8787` → `:3000` → production | Override target API base URL |
| `VIETFUEL_LANG` | `vi` | CLI language (`vi` or `en`) |
| `ADMIN_TOKEN` | *(unset)* | Bearer token for admin commands in production (not required in local Wrangler dev) |

## Subcommands & Flags

| Command | Flags | Description |
| --- | --- | --- |
| `npm run cli` | `--lang vi\|en`, `--url` | Interactive numbered menu (1–7) |
| `prices` | `--zone both\|1\|2`, `--json` | Aggregated retail prices across all 12 sources |
| `source <id>` | `--json` | Single distributor prices (`petrolimex`, `pvoil`, `luatvietnam`, etc.) |
| `province <slug>` | `--json` | Retail prices for any of the 63 legacy provinces (`ha-noi`, `ho-chi-minh`, etc.) |
| `provinces` | `--region 1\|2`, `--q <text>` | List & filter 63 legacy provinces (34 post-merger) |
| `history` | `--fuel`, `--source`, `--from`, `--to`, `--limit` | Query 90-day price history from D1 / SQLite |
| `health` | `--json` | System health & cache TTL across all 12 sources |
| `scrape [source]` | `--token` | Force immediate scrape for all sources or one source |
| `clear-cache` | `--token` | Clear all KV cache entries |

## Real Captured Outputs

### Interactive Menu (`npm run cli -- --lang en`)

```text
╭──────────────────────────────────────────────────────╮
│  VIETFUEL API v2 — INTERACTIVE CONSOLE               │
├──────────────────────────────────────────────────────┤
│  Target API: http://127.0.0.1:8787                   │
│  Docs: http://127.0.0.1:8787/docs                    │
╰──────────────────────────────────────────────────────╯

  Prices
  1. Live prices — all distributors (V1/V2)
  2. One distributor — pick from list
  3. One province — search or pick from 63

  Data
  4. Price history — filter by fuel, source, rows
  5. System health & source URLs

  Admin
  6. Clear cache
  7. Refresh prices now (all or one source)

  0. Exit

Select option (0-7): 
```

### Help (`npm run cli -- --help --lang en`)

```text
╭───────────────────────────────────────────────────────────────────────╮
│  VIETFUEL API v2 — TERMINAL CLI                                       │
├───────────────────────────────────────────────────────────────────────┤
│  Usage: npm run cli [-- <command> [args] [options]]                   │
│  Default: Auto-detects :8787 (Wrangler), :3000 (Node), or production  │
╰───────────────────────────────────────────────────────────────────────╯

╭─────────────────────────────────────┬────────────────────────────────────────────────────────╮
│ Command                             │ Description                                            │
├─────────────────────────────────────┼────────────────────────────────────────────────────────┤
│ prices [--zone 1|2|both]            │ Aggregated prices across all distributors (V1/V2)      │
│ source <sourceId>                   │ Single distributor prices (petrolimex, pvoil, comeco…) │
│ province <slug>                     │ Local province retail prices (ha-noi, ho-chi-minh…)    │
│ provinces [--region 1|2] [--q text] │ Search & filter all 63 provinces                       │
│ history [--fuel X] [--source Y]     │ Query D1/SQLite historical snapshots                   │
│ health                              │ Inspect API health, storage backends & cache TTLs      │
│ clear-cache [--token T]             │ Admin: clear KV cache (POST /admin/cache/clear)        │
│ scrape [--source id] [--token T]    │ Admin: trigger manual scrape (POST /admin/scrape)      │
╰─────────────────────────────────────┴────────────────────────────────────────────────────────╯

Global flags: --url <baseUrl>  --token <ADMIN_TOKEN>  --json  --lang vi|en  --help
```

### 1. Aggregated Prices (`npm run cli -- prices --lang en`)

```text
╭──────────────────────────────────────────────────────────╮
│  VIETNAM RETAIL FUEL PRICES (VND/lít)                    │
├──────────────────────────────────────────────────────────┤
│  Effective Date: 03/12/2026                              │
│  Sources Active: 12/12  (TTL: 2024s)                     │
│  Zone Display: V1 / V2 (Dual-zone + Single)              │
│  API Endpoint: http://127.0.0.1:8787/api/v1/fuel-prices  │
╰──────────────────────────────────────────────────────────╯

Group 1/2: Petrolimex · COMECO · WebGia (Mirror) · GiaXangHomNay · PVOil
╭─────────────────────┬───────────────┬────────┬───────────────┬───────────────┬────────╮
│ Fuel Product        │           PLX │    COM │           WEG │           GXH │    PVO │
├─────────────────────┼───────────────┼────────┼───────────────┼───────────────┼────────┤
│ Xăng E10 RON 95-V   │ 20,880/21,290 │ 20,880 │ 20,880/21,290 │ 20,880/21,290 │      — │
│ Xăng E10 RON 95-III │ 18,580/18,950 │ 18,580 │ 18,580/18,950 │ 18,580/18,950 │ 18,580 │
│ Xăng E5 RON 92-II   │ 17,840/18,190 │ 17,840 │ 17,840/18,190 │ 17,840/18,190 │ 17,840 │
│ DO 0,001S-V         │ 26,880/27,410 │ 26,880 │ 26,880/27,410 │ 26,880/27,410 │ 26,880 │
│ DO 0,05S-II         │ 24,780/25,270 │ 24,780 │ 24,780/25,270 │ 24,780/25,270 │ 24,780 │
│ Dầu hỏa 2-K         │ 24,300/24,780 │      — │ 24,300/24,780 │ 24,300/24,780 │      — │
╰─────────────────────┴───────────────┴────────┴───────────────┴───────────────┴────────╯

Group 2/2: Mipec · Saigon Petro · Petro Times · LuatVietnam
╭─────────────────────┬───────────────┬────────┬───────────────┬────────╮
│ Fuel Product        │           MIP │    SGP │           PTT │    LVN │
├─────────────────────┼───────────────┼────────┼───────────────┼────────┤
│ Xăng E10 RON 95-V   │             — │      — │             — │      — │
│ Xăng E10 RON 95-III │ 18,580/18,950 │ 18,580 │ 19,050/19,430 │ 18,580 │
│ Xăng E5 RON 92-II   │ 17,840/18,190 │ 17,840 │ 18,390/18,750 │ 17,840 │
│ DO 0,001S-V         │ 26,880/27,410 │ 26,880 │ 27,060/27,600 │      — │
│ DO 0,05S-II         │ 24,780/25,270 │ 24,780 │ 24,960/25,450 │ 24,780 │
│ Dầu hỏa 2-K         │             — │      — │             — │      — │
╰─────────────────────┴───────────────┴────────┴───────────────┴────────╯

Legend: Green = Region 1 · Yellow = Region 2 · White = Flat price (one nationwide price)
Source verification URLs:
  PLX    Petrolimex       https://www.petrolimex.com.vn
  COM    COMECO           https://comeco.vn
  WEG    WebGia (Mirror)  https://webgia.com/gia-xang-dau/petrolimex/
  GXH    GiaXangHomNay    https://giaxanghomnay.com
  PVO    PVOil            https://www.pvoil.com.vn/tin-gia-xang-dau
  MIP    Mipec            https://www.mipec.com.vn/pages/gia-xang-dau-ban-le
  SGP    Saigon Petro     https://saigonpetro.com.vn/ban-le-xang-dau
  PTT    Petro Times      https://petrotimesgroup.com
  LVN    LuatVietnam      https://luatvietnam.vn/bang-gia-xang-dau-hom-nay.html
```

### 2. Single Distributor (`npm run cli -- source petrolimex --lang en`)

```text
╭──────────────────────────────────────────────────────╮
│  PETROLIMEX — RETAIL PRICES                          │
├──────────────────────────────────────────────────────┤
│  Effective Date: 03/12/2026                          │
│  Verify Source: https://www.petrolimex.com.vn        │
│  Cache Status: HIT                                   │
╰──────────────────────────────────────────────────────╯

╭─────────────────────┬──────────┬──────────┬──────────────╮
│ Fuel Product        │ Region 1 │ Region 2 │ Single Price │
├─────────────────────┼──────────┼──────────┼──────────────┤
│ Xăng E10 RON 95-V   │   20,880 │   21,290 │            — │
│ Xăng E10 RON 95-III │   18,580 │   18,950 │            — │
│ Xăng E5 RON 92-II   │   17,840 │   18,190 │            — │
│ DO 0,001S-V         │   26,880 │   27,410 │            — │
│ DO 0,05S-II         │   24,780 │   25,270 │            — │
│ Dầu hỏa 2-K         │   24,300 │   24,780 │            — │
╰─────────────────────┴──────────┴──────────┴──────────────╯
```

### 3. Province Lookup (`npm run cli -- province ha-noi --lang en`)

```text
╭───────────────────────────────────────────────────────────╮
│  HÀ NỘI (REGION 1)                                        │
├───────────────────────────────────────────────────────────┤
│  Effective Date: 05/12/2026                               │
│  Verify Source: https://giaxanghomnay.com/tinh-tp/ha-noi  │
│  Cache Status: HIT                                        │
╰───────────────────────────────────────────────────────────╯

╭─────────────────────┬────────────────╮
│ Fuel Product        │         Zone 1 │
├─────────────────────┼────────────────┤
│ Xăng E10 RON 95-V   │ 20,880 VND/lít │
│ Xăng E10 RON 95-III │ 18,580 VND/lít │
│ Xăng E5 RON 92-II   │ 17,840 VND/lít │
│ DO 0,001S-V         │ 26,880 VND/lít │
│ DO 0,05S-II         │ 24,780 VND/lít │
│ Dầu hỏa 2-K         │ 24,300 VND/lít │
╰─────────────────────┴────────────────╯
```

### 4. Filter 63 Provinces (`npm run cli -- provinces --region 2 --q lao --lang en`)

```text
╭──────────────────────────────────────────────────────╮
│  VIETNAM PROVINCES (1)                               │
├──────────────────────────────────────────────────────┤
│  Filter: region=2&q=lao                              │
│  Tip: Run: npm run cli -- province <slug>            │
╰──────────────────────────────────────────────────────╯

╭─────────┬─────────────────┬────────╮
│ Slug    │ Province / City │   Zone │
├─────────┼─────────────────┼────────┤
│ lao-cai │ Lào Cai         │ Zone 2 │
╰─────────┴─────────────────┴────────╯
```

### 5. Price History (`npm run cli -- history --fuel "RON 95" --limit 5 --lang en`)

```text
╭──────────────────────────────────────────────────────╮
│  PRICE HISTORY (5 records)                           │
├──────────────────────────────────────────────────────┤
│  Fuel Filter: RON 95                                 │
│  Source Filter: All                                  │
╰──────────────────────────────────────────────────────╯

╭────────────┬─────────────────┬─────────────────────┬────────┬────────╮
│ Date       │ Distributor     │ Fuel Product        │ Zone 1 │ Zone 2 │
├────────────┼─────────────────┼─────────────────────┼────────┼────────┤
│ 2026-12-05 │ Mipec           │ Xăng E10 RON 95-III │ 18,580 │ 18,950 │
│ 2026-12-05 │ Petro Times     │ Xăng E10 RON 95-III │ 19,050 │ 19,430 │
│ 2026-12-05 │ WebGia (Mirror) │ Xăng E10 RON 95-III │ 18,580 │ 18,950 │
│ 2026-12-05 │ WebGia (Mirror) │ Xăng E10 RON 95-V   │ 20,880 │ 21,290 │
│ 2026-12-05 │ PVOil           │ Xăng E10 RON 95-III │ 18,580 │      — │
╰────────────┴─────────────────┴─────────────────────┴────────┴────────╯
```

### 6. System Health (`npm run cli -- health --lang en`)

```text
╭───────────────────────────────────────────────────────╮
│  VIETFUEL API HEALTH — v2.0.0                         │
├───────────────────────────────────────────────────────┤
│  Status: ● HEALTHY                                    │
│  Endpoint: http://127.0.0.1:8787                      │
│  Cache: Cloudflare KV Namespace (FUEL_CACHE)          │
│  Database: Cloudflare D1 Database (vietfuel-history)  │
╰───────────────────────────────────────────────────────╯

╭────────────────────┬─────────────────────┬─────────┬───────╮
│ Source ID          │ Distributor         │ Cache   │   TTL │
├────────────────────┼─────────────────────┼─────────┼───────┤
│ petrolimex         │ Petrolimex          │ ● Ready │ 2020s │
│ kv2_petrolimex     │ Petrolimex KV2      │ ● Ready │ 2020s │
│ saigon_petrolimex  │ Petrolimex Sài Gòn  │ ● Ready │ 2020s │
│ vungtau_petrolimex │ Petrolimex Vũng Tàu │ ● Ready │ 2020s │
│ pvoil              │ PVOil               │ ● Ready │ 2021s │
│ mipec              │ Mipec               │ ● Ready │ 2021s │
│ comeco             │ COMECO              │ ● Ready │ 2022s │
│ saigonpetro        │ Saigon Petro        │ ● Ready │ 2021s │
│ petrotimes         │ Petro Times         │ ● Ready │ 2021s │
│ webgia             │ WebGia (Mirror)     │ ● Ready │ 2021s │
│ giaxanghomnay      │ GiaXangHomNay       │ ● Ready │ 2021s │
│ luatvietnam        │ LuatVietnam         │ ● Ready │ 2021s │
╰────────────────────┴─────────────────────┴─────────┴───────╯
```

### 7. Admin Scrape (`npm run cli -- scrape luatvietnam --lang en`)

```text
╭──────────────────────────────────────────────────────╮
│  SCRAPE COMPLETED                                    │
├──────────────────────────────────────────────────────┤
│  Duration: 1.82s                                     │
│  Succeeded: 12                                       │
│  Failed: 0                                           │
╰──────────────────────────────────────────────────────╯

╭────────────────────┬─────────────────────┬────────┬────────╮
│ Source ID          │ Distributor         │ Result │ Detail │
├────────────────────┼─────────────────────┼────────┼────────┤
│ petrolimex         │ Petrolimex          │ ✓ OK   │ —      │
│ kv2_petrolimex     │ Petrolimex KV2      │ ✓ OK   │ —      │
│ saigon_petrolimex  │ Petrolimex Sài Gòn  │ ✓ OK   │ —      │
│ vungtau_petrolimex │ Petrolimex Vũng Tàu │ ✓ OK   │ —      │
│ pvoil              │ PVOil               │ ✓ OK   │ —      │
│ mipec              │ Mipec               │ ✓ OK   │ —      │
│ comeco             │ COMECO              │ ✓ OK   │ —      │
│ saigonpetro        │ Saigon Petro        │ ✓ OK   │ —      │
│ petrotimes         │ Petro Times         │ ✓ OK   │ —      │
│ webgia             │ WebGia (Mirror)     │ ✓ OK   │ —      │
│ giaxanghomnay      │ GiaXangHomNay       │ ✓ OK   │ —      │
│ luatvietnam        │ LuatVietnam         │ ✓ OK   │ —      │
╰────────────────────┴─────────────────────┴────────┴────────╯
```

### JSON Output (`npm run cli -- prices --json`)

```json
{
  "success": true,
  "status": "ok",
  "meta": {
    "primarySourceId": "petrolimex",
    "primarySource": "Petrolimex",
    "primarySourceUrl": null,
    "dataSources": [
      "petrolimex",
      "kv2_petrolimex",
      "saigon_petrolimex",
      "vungtau_petrolimex",
      "pvoil",
      "mipec",
      "comeco",
      "saigonpetro",
      "petrotimes",
      "webgia",
      "giaxanghomnay",
      "luatvietnam"
    ],
    "sourceCount": 12,
    "scrapedAt": "2026-12-05T07:39:40.623Z",
    "priceDate": "2026-12-03",
    "priceDateDisplay": "03/12/2026",
    "cacheHit": true,
    "cacheTtlRemainingSeconds": 2020,
    "isStale": false,
    "totalItems": 6
  },
  "data": [
    {
      "name": "Xăng E10 RON 95-V",
      "unit": "VND/lít",
      "region1": 20880,
      "region2": 21290,
      "sources": [
        {
          "source": "petrolimex",
          "region1": 20880,
          "region2": 21290,
          "price": null
        },
        {
          "source": "kv2_petrolimex",
          "region1": 20880,
          "region2": 21290,
          "price": null
        },
        {
          "source": "saigon_petrolimex",
          "region1": 20880,
          "region2": 21290,
          "price": null
        },
        {
          "source": "vungtau_petrolimex",
          "region1": 20880,
          "region2": 21290,
          "price": null
        },
        {
          "source": "comeco",
          "region1": null,
          "region2": null,
          "price": 20880
        },
        {
          "source": "webgia",
          "region1": 20880,
          "region2": 21290,
          "price": null
        },
        {
          "source": "giaxanghomnay",
          "region1": 20880,
          "region2": 21290,
          "price": null
        }
      ]
    },
    {
      "name": "Xăng E10 RON 95-III",
      "unit": "VND/lít",
      "region1": 18580,
      "region2": 18950,
      "sources": [
        {
          "source": "petrolimex",
          "region1": 18580,
          "region2": 18950,
          "price": null
        },
        {
          "source": "kv2_petrolimex",
          "region1": 18580,
          "region2": 18950,
          "price": null
        },
        {
          "source": "saigon_petrolimex",
          "region1": 18580,
          "region2": 18950,
          "price": null
        },
        {
          "source": "vungtau_petrolimex",
          "region1": 18580,
          "region2": 18950,
          "price": null
        },
        {
          "source": "pvoil",
          "region1": null,
          "region2": null,
          "price": 18580
        },
        {
          "source": "mipec",
          "region1": 18580,
          "region2": 18950,
          "price": null
        },
        {
          "source": "comeco",
          "region1": null,
          "region2": null,
          "price": 18580
        },
        {
          "source": "saigonpetro",
          "region1": null,
          "region2": null,
          "price": 18580
        },
        {
          "source": "petrotimes",
          "region1": 19050,
          "region2": 19430,
          "price": null
        },
        {
          "source": "webgia",
          "region1": 18580,
          "region2": 18950,
          "price": null
        },
        {
          "source": "giaxanghomnay",
          "region1": 18580,
          "region2": 18950,
          "price": null
        },
        {
          "source": "luatvietnam",
          "region1": null,
          "region2": null,
          "price": 18580
        }
      ]
    },
    {
      "name": "Xăng E5 RON 92-II",
      "unit": "VND/lít",
      "region1": 17840,
      "region2": 18190,
      "sources": [
        {
          "source": "petrolimex",
          "region1": 17840,
          "region2": 18190,
          "price": null
        },
        {
          "source": "kv2_petrolimex",
          "region1": 17840,
          "region2": 18190,
          "price": null
        },
        {
          "source": "saigon_petrolimex",
          "region1": 17840,
          "region2": 18190,
          "price": null
        },
        {
          "source": "vungtau_petrolimex",
          "region1": 17840,
          "region2": 18190,
          "price": null
        },
        {
          "source": "pvoil",
          "region1": null,
          "region2": null,
          "price": 17840
        },
        {
          "source": "mipec",
          "region1": 17840,
          "region2": 18190,
          "price": null
        },
        {
          "source": "comeco",
          "region1": null,
          "region2": null,
          "price": 17840
        },
        {
          "source": "saigonpetro",
          "region1": null,
          "region2": null,
          "price": 17840
        },
        {
          "source": "petrotimes",
          "region1": 18390,
          "region2": 18750,
          "price": null
        },
        {
          "source": "webgia",
          "region1": 17840,
          "region2": 18190,
          "price": null
        },
        {
          "source": "giaxanghomnay",
          "region1": 17840,
          "region2": 18190,
          "price": null
        },
        {
          "source": "luatvietnam",
          "region1": null,
          "region2": null,
          "price": 17840
        }
      ]
    }
  ]
}
```
