# Drug Formulary — Static JSON API

Public static API for ANVISA drug formulary data. **No database** — same idea as [TUSS](https://0ctor.github.io/tuss/) and [CID](https://0ctor.github.io/cid/): one JSON file (or a few) served from GitHub Pages.

**Base URL:** `https://0ctor.github.io/platform-drug-formulary/`  
**JSON endpoint:** `https://0ctor.github.io/platform-drug-formulary/formulary.json`

## Response format (aligned with TUSS/CID)

```json
{
  "columns": ["id", "product_name", "registration_number", "expedient", "company_name", "medication_type"],
  "rows": [
    {
      "id": 1,
      "product_name": "DIPIRONA 500MG",
      "registration_number": "12345000001",
      "expedient": "25351123456",
      "company_name": "Farmacia Exemplo Ltda",
      "medication_type": "Generico"
    }
  ],
  "meta": {
    "schemaVersion": "1.0",
    "source": "ANVISA",
    "rowCount": 3,
    "generatedAt": "2025-03-17T00:00:00Z"
  }
}
```

- **`columns`** — list of field names (for documentation and generic clients).
- **`rows`** — array of medication records.
- **`meta`** — `schemaVersion`, `source`, `rowCount`, `generatedAt`.

## Fields (per row)

| Field | Description |
|-------|-------------|
| `id` | Numeric ID |
| `product_name` | Product name (ANVISA) |
| `registration_number` | Registration number |
| `expedient` | Expedient number |
| `company_name` | Company / holder |
| `medication_type` | e.g. Generico, Referencia |
| `leaflet_pdf_url` | Link para a bula em PDF (ANVISA) |

## Usage examples

**JavaScript — load and filter by name:**

```js
const res = await fetch('https://0ctor.github.io/platform-drug-formulary/formulary.json');
const data = await res.json();
const list = data.rows.filter(r =>
  r.product_name.toLowerCase().includes('paracetamol')
);
```

**JavaScript — find by registration number:**

```js
const med = data.rows.find(r => r.registration_number === '12345000001');
```

**Use `data.columns` and `data.meta`:**

```js
console.log(data.columns);        // field names
console.log(data.meta.rowCount);   // total rows
console.log(data.meta.generatedAt);
```

**cURL:**

```bash
curl -s https://0ctor.github.io/platform-drug-formulary/formulary.json | jq .
```

No authentication, no rate limit. Data is static and is updated when the repo's `gh-pages` branch is updated.

**Bulário (bula):** cada linha tem `leaflet_pdf_url` — link direto para o PDF da bula na ANVISA. O texto completo da bula (extraído) está disponível apenas na **API Rust** deste projeto, após rodar o ingest.

**Preços:** este JSON não inclui preços. Preços oficiais (PMC, PMVG, PF) vêm da **CMED** — listas em [ANVISA/CMED – Preços](https://www.gov.br/anvisa/pt-br/assuntos/medicamentos/cmed/precos) ou em [dados.gov.br – Preço de medicamentos](https://dados.gov.br/dados/conjuntos-dados/preco-de-medicamentos-no-brasil-governo).

## Regenerating the JSON (no DB on Pages)

Because this version has **no database**, the data lives only in the JSON file. To refresh it, use `scripts/generate_formulary_json.py`. A comunidade já mapeou como baixar os dados da ANVISA: **dados abertos (CSV)** em [dados.anvisa.gov.br/dados/](https://dados.anvisa.gov.br/dados/) ou [dados.gov.br](https://dados.gov.br) (conjunto "Medicamentos registrados no Brasil") — baixe o CSV e use `--from-csv`. Outras opções: [anvisa-data-dumper](https://github.com/rafaelmartinsrm/anvisa-data-dumper), [meuremedio-extracao](https://github.com/yagoluiz/meuremedio-extracao), [bulario](https://github.com/iuryLandin/bulario).

1. **From CSV (recomendado):** `python scripts/generate_formulary_json.py --from-csv path/to/medicamentos.csv`
2. **From the Rust API:** `--from-api http://localhost:4949`
3. **From a JSON file:** `--from-file medicamentos.json`
4. **From ANVISA API (bulário):** `--from-anvisa` (pode dar 403/500 fora do Brasil)

Then commit the new `gh-pages/formulary.json` and push to `main`; the workflow will update the `gh-pages` branch and the site.
