{"openapi":"3.1.0","info":{"title":"VietFuel API","version":"2.0.0","description":"Fast, lightweight Vietnam retail fuel price REST API aggregating 12 official distributors and 63 legacy provinces (34 post-merger, Region 1 & Region 2).","license":{"name":"MIT","url":"https://github.com/TranQui004/vietfuel-api/blob/main/LICENSE"}},"servers":[{"url":"https://vietfuel-api.tranqui.workers.dev","description":"Production (Cloudflare Workers)"},{"url":"http://localhost:8787","description":"Local (Wrangler Dev)"},{"url":"http://localhost:3000","description":"Local (Node.js / Docker)"}],"components":{"securitySchemes":{"bearerAuth":{"type":"http","scheme":"bearer","description":"Admin token required for POST /api/v1/admin/* and ?refresh=1 in production."}},"schemas":{"HealthResponse":{"type":"object","properties":{"success":{"type":"boolean","enum":[true]},"status":{"type":"string","enum":["healthy","degraded"]},"version":{"type":"string"},"storage":{"type":"object","properties":{"cacheType":{"type":"string"},"dbType":{"type":"string"}},"required":["cacheType","dbType"]},"sources":{"type":"object","additionalProperties":{"type":"object","properties":{"label":{"type":"string"},"populated":{"type":"boolean"},"scrapedAt":{"type":["string","null"]},"ttlRemainingSeconds":{"type":["integer","null"]},"isStale":{"type":"boolean"}},"required":["label","populated","scrapedAt","ttlRemainingSeconds","isStale"]}},"endpoints":{"type":"object","properties":{"nationalSources":{"type":"array","items":{"type":"string"}},"provinceCount":{"type":"integer"},"apiVersion":{"type":"string"},"openapi":{"type":"string"}},"required":["nationalSources","provinceCount","apiVersion","openapi"]},"timestamp":{"type":"string"}},"required":["success","status","version","storage","sources","endpoints","timestamp"]},"SourcesResponse":{"type":"object","properties":{"success":{"type":"boolean","enum":[true]},"status":{"type":"string","enum":["ok"]},"meta":{"type":"object","properties":{"total":{"type":"integer"},"populated":{"type":"integer"}},"required":["total","populated"]},"data":{"type":"array","items":{"$ref":"#/components/schemas/SourceSummary"}}},"required":["success","status","meta","data"]},"SourceSummary":{"type":"object","properties":{"id":{"type":"string"},"label":{"type":"string"},"shortCode":{"type":"string"},"url":{"type":"string"},"isMirror":{"type":"boolean"},"populated":{"type":"boolean"},"scrapedAt":{"type":["string","null"]},"ttlRemainingSeconds":{"type":["integer","null"]},"isStale":{"type":"boolean"}},"required":["id","label","shortCode","url","isMirror","populated","scrapedAt","ttlRemainingSeconds","isStale"]},"ProvincesResponse":{"type":"object","properties":{"success":{"type":"boolean","enum":[true]},"status":{"type":"string","enum":["ok"]},"meta":{"type":"object","properties":{"total":{"type":"integer"},"region1Count":{"type":"integer"},"region2Count":{"type":"integer"},"filterApplied":{"type":["string","null"]}},"required":["total","region1Count","region2Count","filterApplied"]},"data":{"type":"array","items":{"$ref":"#/components/schemas/ProvinceItem"}}},"required":["success","status","meta","data"]},"ProvinceItem":{"type":"object","properties":{"id":{"type":"string"},"name":{"type":"string"},"slug":{"type":"string"},"region":{"type":"string","enum":["1","2"]},"partial":{"type":"boolean"},"region2Districts":{"type":"array","items":{"type":"string"}}},"required":["id","name","slug","region"]},"AggregatedPricesResponse":{"type":"object","properties":{"success":{"type":"boolean","enum":[true]},"status":{"type":"string","enum":["ok"]},"disclaimer":{"$ref":"#/components/schemas/Disclaimer"},"meta":{"type":"object","properties":{"primarySourceId":{"type":"string"},"primarySource":{"type":"string"},"primarySourceUrl":{"type":["string","null"]},"dataSources":{"type":"array","items":{"type":"string"}},"sourceCount":{"type":"integer"},"scrapedAt":{"type":"string"},"priceDate":{"type":["string","null"]},"priceDateDisplay":{"type":["string","null"]},"cacheHit":{"type":"boolean"},"cacheTtlRemainingSeconds":{"type":"integer"},"isStale":{"type":"boolean"},"totalItems":{"type":"integer"}},"required":["primarySourceId","primarySource","primarySourceUrl","dataSources","sourceCount","scrapedAt","priceDate","priceDateDisplay","cacheHit","cacheTtlRemainingSeconds","isStale","totalItems"]},"data":{"type":"array","items":{"$ref":"#/components/schemas/AggregatedFuelItem"}}},"required":["success","status","disclaimer","meta","data"]},"Disclaimer":{"type":"object","properties":{"vi":{"type":"string"},"en":{"type":"string"}},"required":["vi","en"]},"AggregatedFuelItem":{"type":"object","properties":{"name":{"type":"string","example":"Xăng RON 95-III"},"unit":{"type":"string","example":"VND/lít"},"region1":{"type":["number","null"],"example":20540},"region2":{"type":["number","null"],"example":20950},"sources":{"type":"array","items":{"type":"object","properties":{"source":{"type":"string","example":"petrolimex"},"region1":{"type":["number","null"]},"region2":{"type":["number","null"]},"price":{"type":["number","null"]}},"required":["source","region1","region2","price"]}}},"required":["name","unit","region1","region2","sources"]},"ErrorResponse":{"type":"object","properties":{"success":{"type":"boolean","enum":[false]},"error":{"type":"object","properties":{"code":{"type":"string"},"message":{"type":"string"},"details":{}},"required":["code","message"]}},"required":["success","error"]},"ProvincePricesResponse":{"type":"object","properties":{"success":{"type":"boolean","enum":[true]},"status":{"type":"string","enum":["ok"]},"disclaimer":{"$ref":"#/components/schemas/Disclaimer"},"meta":{"type":"object","properties":{"province":{"type":"string"},"slug":{"type":"string"},"region":{"type":"string","enum":["1","2"]},"source":{"type":"string"},"sourceUrl":{"type":"string"},"scrapedAt":{"type":"string"},"priceDate":{"type":["string","null"]},"priceDateDisplay":{"type":["string","null"]},"cacheHit":{"type":"boolean"},"cacheTtlRemainingSeconds":{"type":"integer"},"totalItems":{"type":"integer"}},"required":["province","slug","region","source","sourceUrl","scrapedAt","priceDate","priceDateDisplay","cacheHit","cacheTtlRemainingSeconds","totalItems"]},"data":{"type":"array","items":{"$ref":"#/components/schemas/FuelPriceItem"}}},"required":["success","status","disclaimer","meta","data"]},"FuelPriceItem":{"type":"object","properties":{"name":{"type":"string","example":"Xăng RON 95-III"},"region1":{"type":["number","null"],"example":20540},"region2":{"type":["number","null"],"example":20950},"price":{"type":["number","null"],"example":null},"unit":{"type":"string","example":"VND/lít"}},"required":["name","region1","region2","price","unit"]},"SourcePricesResponse":{"type":"object","properties":{"success":{"type":"boolean","enum":[true]},"status":{"type":"string","enum":["ok"]},"disclaimer":{"$ref":"#/components/schemas/Disclaimer"},"meta":{"type":"object","properties":{"sourceId":{"type":"string"},"source":{"type":"string"},"sourceUrl":{"type":["string","null"]},"scrapedAt":{"type":"string"},"priceDate":{"type":["string","null"]},"priceDateDisplay":{"type":["string","null"]},"priceDateSource":{"type":["string","null"]},"priceAnnouncedAt":{"type":["string","null"]},"blockedByProtection":{"type":"boolean"},"cacheHit":{"type":"boolean"},"cacheTtlRemainingSeconds":{"type":["integer","null"]},"isStale":{"type":"boolean"},"totalItems":{"type":"integer"}},"required":["sourceId","source","sourceUrl","scrapedAt","priceDate","priceDateDisplay","priceDateSource","priceAnnouncedAt","blockedByProtection","cacheHit","cacheTtlRemainingSeconds","isStale","totalItems"]},"data":{"type":"array","items":{"$ref":"#/components/schemas/FuelPriceItem"}}},"required":["success","status","disclaimer","meta","data"]},"HistoryResponse":{"type":"object","properties":{"success":{"type":"boolean","enum":[true]},"meta":{"type":"object","properties":{"fuelName":{"type":"string"},"source":{"type":["string","null"]},"from":{"type":["string","null"]},"to":{"type":["string","null"]},"limit":{"type":"integer"},"offset":{"type":"integer"},"count":{"type":"integer"}},"required":["fuelName","source","from","to","limit","offset","count"]},"data":{"type":"array","items":{"$ref":"#/components/schemas/HistoryRecord"}}},"required":["success","meta","data"]},"HistoryRecord":{"type":"object","properties":{"id":{"type":"integer"},"fuelName":{"type":"string"},"region1":{"type":["number","null"]},"region2":{"type":["number","null"]},"isFlat":{"type":"boolean","description":"True when the source publishes one flat price (value in region1)"},"source":{"type":"string"},"priceDate":{"type":["string","null"]},"scrapedAt":{"type":["string","null"]},"createdAt":{"type":"string"}},"required":["id","fuelName","region1","region2","isFlat","source","priceDate","scrapedAt","createdAt"]}},"parameters":{}},"paths":{"/api/v1/health":{"get":{"tags":["System"],"summary":"Check API health, cache population status, and active storage backends","responses":{"200":{"description":"Health report","content":{"application/json":{"schema":{"$ref":"#/components/schemas/HealthResponse"}}}}}}},"/api/v1/sources":{"get":{"tags":["Metadata"],"summary":"List all 12 supported Vietnam fuel distributors and their cache status","responses":{"200":{"description":"List of distributor sources","content":{"application/json":{"schema":{"$ref":"#/components/schemas/SourcesResponse"}}}}}}},"/api/v1/provinces":{"get":{"tags":["Metadata"],"summary":"List all 63 legacy Vietnamese provinces/cities (34 post-merger) with Region 1 / Region 2 pricing zones","parameters":[{"schema":{"type":"string","enum":["1","2"],"description":"Filter by price zone (1 or 2)"},"required":false,"description":"Filter by price zone (1 or 2)","name":"region","in":"query"},{"schema":{"type":"string","maxLength":60,"description":"Search by province name or slug (case-insensitive)"},"required":false,"description":"Search by province name or slug (case-insensitive)","name":"q","in":"query"}],"responses":{"200":{"description":"List of provinces","content":{"application/json":{"schema":{"$ref":"#/components/schemas/ProvincesResponse"}}}}}}},"/api/v1/fuel-prices":{"get":{"tags":["Fuel Prices"],"summary":"Get aggregated Vietnam retail fuel prices across all 12 distributors","responses":{"200":{"description":"Aggregated retail fuel prices grouped by normalized product name","content":{"application/json":{"schema":{"$ref":"#/components/schemas/AggregatedPricesResponse"}}}},"503":{"description":"Upstream data temporarily unavailable","content":{"application/json":{"schema":{"$ref":"#/components/schemas/ErrorResponse"}}}}}}},"/api/v1/fuel-prices/province/{slug}":{"get":{"tags":["Fuel Prices"],"summary":"Get retail fuel prices for a specific Vietnamese province/city (63 legacy units / 34 post-merger)","parameters":[{"schema":{"type":"string","minLength":1,"example":"ha-noi"},"required":true,"name":"slug","in":"path"}],"responses":{"200":{"description":"Province fuel prices with Region 1 / Region 2 classification","content":{"application/json":{"schema":{"$ref":"#/components/schemas/ProvincePricesResponse"}}}},"404":{"description":"Province slug not found","content":{"application/json":{"schema":{"$ref":"#/components/schemas/ErrorResponse"}}}},"502":{"description":"Failed to fetch upstream province prices","content":{"application/json":{"schema":{"$ref":"#/components/schemas/ErrorResponse"}}}}}}},"/api/v1/fuel-prices/{source}":{"get":{"tags":["Fuel Prices"],"summary":"Get retail fuel prices from a single distributor source","parameters":[{"schema":{"type":"string","minLength":1,"example":"petrolimex"},"required":true,"name":"source","in":"path"},{"schema":{"type":"string","enum":["0","1"],"description":"Set to 1 with valid Bearer ADMIN_TOKEN to force cache refresh"},"required":false,"description":"Set to 1 with valid Bearer ADMIN_TOKEN to force cache refresh","name":"refresh","in":"query"}],"responses":{"200":{"description":"Fuel prices from the specified distributor","content":{"application/json":{"schema":{"$ref":"#/components/schemas/SourcePricesResponse"}}}},"400":{"description":"Invalid source identifier","content":{"application/json":{"schema":{"$ref":"#/components/schemas/ErrorResponse"}}}},"401":{"description":"Unauthorized force refresh attempt","content":{"application/json":{"schema":{"$ref":"#/components/schemas/ErrorResponse"}}}},"503":{"description":"Source temporarily unavailable","content":{"application/json":{"schema":{"$ref":"#/components/schemas/ErrorResponse"}}}}}}},"/api/v1/history":{"get":{"tags":["History"],"summary":"Query historical fuel prices with filtering and pagination","parameters":[{"schema":{"type":"string","maxLength":80,"description":"Filter by fuel name substring"},"required":false,"description":"Filter by fuel name substring","name":"fuel","in":"query"},{"schema":{"type":"string","maxLength":40,"description":"Filter by distributor source ID"},"required":false,"description":"Filter by distributor source ID","name":"source","in":"query"},{"schema":{"type":"string","pattern":"^\\d{4}-\\d{2}-\\d{2}$","example":"2026-01-01"},"required":false,"name":"from","in":"query"},{"schema":{"type":"string","pattern":"^\\d{4}-\\d{2}-\\d{2}$","example":"2026-12-31"},"required":false,"name":"to","in":"query"},{"schema":{"type":"integer","minimum":1,"maximum":500,"default":100},"required":false,"name":"limit","in":"query"},{"schema":{"type":["integer","null"],"minimum":0,"default":0},"required":false,"name":"offset","in":"query"}],"responses":{"200":{"description":"Historical fuel price records","content":{"application/json":{"schema":{"$ref":"#/components/schemas/HistoryResponse"}}}},"400":{"description":"Invalid query parameters","content":{"application/json":{"schema":{"$ref":"#/components/schemas/ErrorResponse"}}}},"501":{"description":"Database binding not configured","content":{"application/json":{"schema":{"$ref":"#/components/schemas/ErrorResponse"}}}}}}},"/api/v1/history/{fuel_name}":{"get":{"tags":["History"],"summary":"Query historical fuel prices filtered by fuel name path parameter","parameters":[{"schema":{"type":"string","minLength":1,"maxLength":80,"example":"RON 95"},"required":true,"name":"fuel_name","in":"path"},{"schema":{"type":"string","maxLength":40,"description":"Filter by distributor source ID"},"required":false,"description":"Filter by distributor source ID","name":"source","in":"query"},{"schema":{"type":"string","pattern":"^\\d{4}-\\d{2}-\\d{2}$","example":"2026-01-01"},"required":false,"name":"from","in":"query"},{"schema":{"type":"string","pattern":"^\\d{4}-\\d{2}-\\d{2}$","example":"2026-12-31"},"required":false,"name":"to","in":"query"},{"schema":{"type":"integer","minimum":1,"maximum":500,"default":100},"required":false,"name":"limit","in":"query"},{"schema":{"type":["integer","null"],"minimum":0,"default":0},"required":false,"name":"offset","in":"query"}],"responses":{"200":{"description":"Historical fuel price records","content":{"application/json":{"schema":{"$ref":"#/components/schemas/HistoryResponse"}}}},"400":{"description":"Invalid query parameters","content":{"application/json":{"schema":{"$ref":"#/components/schemas/ErrorResponse"}}}},"501":{"description":"Database binding not configured","content":{"application/json":{"schema":{"$ref":"#/components/schemas/ErrorResponse"}}}}}}},"/api/v1/admin/cache/clear":{"post":{"tags":["Admin"],"summary":"Clear all cached fuel and province entries (requires Bearer ADMIN_TOKEN)","responses":{"200":{"description":"Cache cleared","content":{"application/json":{"schema":{"type":"object","properties":{"success":{"type":"boolean","enum":[true]},"clearedCount":{"type":"integer"},"clearedKeys":{"type":"array","items":{"type":"string"}},"timestamp":{"type":"string"}},"required":["success","clearedCount","clearedKeys","timestamp"]}}}},"401":{"description":"Unauthorized","content":{"application/json":{"schema":{"$ref":"#/components/schemas/ErrorResponse"}}}}}}},"/api/v1/admin/scrape":{"post":{"tags":["Admin"],"summary":"Force scrape all sources or a single source (requires Bearer ADMIN_TOKEN)","parameters":[{"schema":{"type":"string","description":"Optional single source ID to refresh"},"required":false,"description":"Optional single source ID to refresh","name":"source","in":"query"}],"responses":{"200":{"description":"Scrape completed","content":{"application/json":{"schema":{"type":"object","properties":{"success":{"type":"boolean","enum":[true]},"succeeded":{"type":"array","items":{"type":"string"}},"failed":{"type":"array","items":{"type":"object","properties":{"source":{"type":"string"},"error":{"type":"string"}},"required":["source","error"]}},"durationMs":{"type":"integer"}},"required":["success","succeeded","failed","durationMs"]}}}},"400":{"description":"Invalid source ID","content":{"application/json":{"schema":{"$ref":"#/components/schemas/ErrorResponse"}}}},"401":{"description":"Unauthorized","content":{"application/json":{"schema":{"$ref":"#/components/schemas/ErrorResponse"}}}}}}},"/api/health":{"get":{"tags":["System"],"summary":"Check API health, cache population status, and active storage backends","responses":{"200":{"description":"Health report","content":{"application/json":{"schema":{"$ref":"#/components/schemas/HealthResponse"}}}}}}},"/api/sources":{"get":{"tags":["Metadata"],"summary":"List all 12 supported Vietnam fuel distributors and their cache status","responses":{"200":{"description":"List of distributor sources","content":{"application/json":{"schema":{"$ref":"#/components/schemas/SourcesResponse"}}}}}}},"/api/provinces":{"get":{"tags":["Metadata"],"summary":"List all 63 legacy Vietnamese provinces/cities (34 post-merger) with Region 1 / Region 2 pricing zones","parameters":[{"schema":{"type":"string","enum":["1","2"],"description":"Filter by price zone (1 or 2)"},"required":false,"description":"Filter by price zone (1 or 2)","name":"region","in":"query"},{"schema":{"type":"string","maxLength":60,"description":"Search by province name or slug (case-insensitive)"},"required":false,"description":"Search by province name or slug (case-insensitive)","name":"q","in":"query"}],"responses":{"200":{"description":"List of provinces","content":{"application/json":{"schema":{"$ref":"#/components/schemas/ProvincesResponse"}}}}}}},"/api/fuel-prices":{"get":{"tags":["Fuel Prices"],"summary":"Get aggregated Vietnam retail fuel prices across all 12 distributors","responses":{"200":{"description":"Aggregated retail fuel prices grouped by normalized product name","content":{"application/json":{"schema":{"$ref":"#/components/schemas/AggregatedPricesResponse"}}}},"503":{"description":"Upstream data temporarily unavailable","content":{"application/json":{"schema":{"$ref":"#/components/schemas/ErrorResponse"}}}}}}},"/api/fuel-prices/province/{slug}":{"get":{"tags":["Fuel Prices"],"summary":"Get retail fuel prices for a specific Vietnamese province/city (63 legacy units / 34 post-merger)","parameters":[{"schema":{"type":"string","minLength":1,"example":"ha-noi"},"required":true,"name":"slug","in":"path"}],"responses":{"200":{"description":"Province fuel prices with Region 1 / Region 2 classification","content":{"application/json":{"schema":{"$ref":"#/components/schemas/ProvincePricesResponse"}}}},"404":{"description":"Province slug not found","content":{"application/json":{"schema":{"$ref":"#/components/schemas/ErrorResponse"}}}},"502":{"description":"Failed to fetch upstream province prices","content":{"application/json":{"schema":{"$ref":"#/components/schemas/ErrorResponse"}}}}}}},"/api/fuel-prices/{source}":{"get":{"tags":["Fuel Prices"],"summary":"Get retail fuel prices from a single distributor source","parameters":[{"schema":{"type":"string","minLength":1,"example":"petrolimex"},"required":true,"name":"source","in":"path"},{"schema":{"type":"string","enum":["0","1"],"description":"Set to 1 with valid Bearer ADMIN_TOKEN to force cache refresh"},"required":false,"description":"Set to 1 with valid Bearer ADMIN_TOKEN to force cache refresh","name":"refresh","in":"query"}],"responses":{"200":{"description":"Fuel prices from the specified distributor","content":{"application/json":{"schema":{"$ref":"#/components/schemas/SourcePricesResponse"}}}},"400":{"description":"Invalid source identifier","content":{"application/json":{"schema":{"$ref":"#/components/schemas/ErrorResponse"}}}},"401":{"description":"Unauthorized force refresh attempt","content":{"application/json":{"schema":{"$ref":"#/components/schemas/ErrorResponse"}}}},"503":{"description":"Source temporarily unavailable","content":{"application/json":{"schema":{"$ref":"#/components/schemas/ErrorResponse"}}}}}}},"/api/history":{"get":{"tags":["History"],"summary":"Query historical fuel prices with filtering and pagination","parameters":[{"schema":{"type":"string","maxLength":80,"description":"Filter by fuel name substring"},"required":false,"description":"Filter by fuel name substring","name":"fuel","in":"query"},{"schema":{"type":"string","maxLength":40,"description":"Filter by distributor source ID"},"required":false,"description":"Filter by distributor source ID","name":"source","in":"query"},{"schema":{"type":"string","pattern":"^\\d{4}-\\d{2}-\\d{2}$","example":"2026-01-01"},"required":false,"name":"from","in":"query"},{"schema":{"type":"string","pattern":"^\\d{4}-\\d{2}-\\d{2}$","example":"2026-12-31"},"required":false,"name":"to","in":"query"},{"schema":{"type":"integer","minimum":1,"maximum":500,"default":100},"required":false,"name":"limit","in":"query"},{"schema":{"type":["integer","null"],"minimum":0,"default":0},"required":false,"name":"offset","in":"query"}],"responses":{"200":{"description":"Historical fuel price records","content":{"application/json":{"schema":{"$ref":"#/components/schemas/HistoryResponse"}}}},"400":{"description":"Invalid query parameters","content":{"application/json":{"schema":{"$ref":"#/components/schemas/ErrorResponse"}}}},"501":{"description":"Database binding not configured","content":{"application/json":{"schema":{"$ref":"#/components/schemas/ErrorResponse"}}}}}}},"/api/history/{fuel_name}":{"get":{"tags":["History"],"summary":"Query historical fuel prices filtered by fuel name path parameter","parameters":[{"schema":{"type":"string","minLength":1,"maxLength":80,"example":"RON 95"},"required":true,"name":"fuel_name","in":"path"},{"schema":{"type":"string","maxLength":40,"description":"Filter by distributor source ID"},"required":false,"description":"Filter by distributor source ID","name":"source","in":"query"},{"schema":{"type":"string","pattern":"^\\d{4}-\\d{2}-\\d{2}$","example":"2026-01-01"},"required":false,"name":"from","in":"query"},{"schema":{"type":"string","pattern":"^\\d{4}-\\d{2}-\\d{2}$","example":"2026-12-31"},"required":false,"name":"to","in":"query"},{"schema":{"type":"integer","minimum":1,"maximum":500,"default":100},"required":false,"name":"limit","in":"query"},{"schema":{"type":["integer","null"],"minimum":0,"default":0},"required":false,"name":"offset","in":"query"}],"responses":{"200":{"description":"Historical fuel price records","content":{"application/json":{"schema":{"$ref":"#/components/schemas/HistoryResponse"}}}},"400":{"description":"Invalid query parameters","content":{"application/json":{"schema":{"$ref":"#/components/schemas/ErrorResponse"}}}},"501":{"description":"Database binding not configured","content":{"application/json":{"schema":{"$ref":"#/components/schemas/ErrorResponse"}}}}}}}},"webhooks":{}}