# Vitti SQL Formatter — API

Format any SQL into the Vitti "river" style over HTTP — the same engine as the web app and the
browser extension. No API key, CORS open (`*`), rate-limited (~20 req/s).

**Base URL:** `https://identar.lucas.mat.br/api/format`

---

## English

### POST (recommended)

```bash
curl -s -X POST https://identar.lucas.mat.br/api/format \
  -H 'Content-Type: application/json' \
  -d '{"sql":"select a, b from t where x = 1","options":{"useTabs":false}}'
```

Response:

```json
{"formatted":"select      a\n            ,b\n\nfrom        t\n\nwhere       1=1\nand         x = 1\n"}
```

> Options go **nested under `options`**. A flat `useTabs` next to `sql` is ignored.

### GET (quick test / browser)

```bash
curl -s "https://identar.lucas.mat.br/api/format?sql=select%20a,b%20from%20t&useTabs=false&format=text"
```

On GET, options are flat query params, and `format=text` returns the raw SQL instead of JSON.

### Options

| key | values | note |
|---|---|---|
| `useTabs` | `true` / `false` | **`false` = spaces** — use when pasting into Databricks |
| `keywordCase` | `lower` / `upper` / `preserve` | |
| `tabWidth` / `riverWidth` | integer | indent size / operand column (default 12) |
| `aliasColumn` / `joinColumn` / `filterColumn` | integer (`0` = auto/off) | the alignment rulers |
| `where11` | `true` / `false` | open every `where` with `1=1` |

### Notes

- **Response:** `{"formatted": "…"}` (JSON), or raw SQL when `format=text`.
- **Health:** `GET https://identar.lucas.mat.br/api/health` → `{"ok":true}`.
- **Errors:** `{"error":"bad_json","message":"…"}` with HTTP 400 for malformed input.
- Open, no auth, rate-limited — fine to call from a notebook or a script.

### Python

```python
import requests
r = requests.post("https://identar.lucas.mat.br/api/format",
                  json={"sql": "select a, b from t", "options": {"useTabs": False}})
print(r.json()["formatted"])
```

---

## Português (pt-BR)

Formata qualquer SQL no estilo "rio" do Vitti via HTTP — o mesmo motor do site e da extensão de
navegador. Sem chave de API, CORS aberto (`*`), com limite de taxa (~20 req/s).

**URL base:** `https://identar.lucas.mat.br/api/format`

### POST (recomendado)

```bash
curl -s -X POST https://identar.lucas.mat.br/api/format \
  -H 'Content-Type: application/json' \
  -d '{"sql":"select a, b from t where x = 1","options":{"useTabs":false}}'
```

Retorna:

```json
{"formatted":"select      a\n            ,b\n\nfrom        t\n\nwhere       1=1\nand         x = 1\n"}
```

> As opções vão **aninhadas em `options`**. Um `useTabs` solto ao lado de `sql` é ignorado.

### GET (teste rápido / navegador)

```bash
curl -s "https://identar.lucas.mat.br/api/format?sql=select%20a,b%20from%20t&useTabs=false&format=text"
```

No GET, as opções são parâmetros de query, e `format=text` devolve o SQL puro em vez de JSON.

### Opções

| chave | valores | observação |
|---|---|---|
| `useTabs` | `true` / `false` | **`false` = espaços** — use ao colar no Databricks |
| `keywordCase` | `lower` / `upper` / `preserve` | |
| `tabWidth` / `riverWidth` | inteiro | tamanho do recuo / coluna do operando (padrão 12) |
| `aliasColumn` / `joinColumn` / `filterColumn` | inteiro (`0` = auto/desliga) | as réguas de alinhamento |
| `where11` | `true` / `false` | abre todo `where` com `1=1` |

### Observações

- **Resposta:** `{"formatted": "…"}` (JSON), ou SQL puro quando `format=text`.
- **Health:** `GET https://identar.lucas.mat.br/api/health` → `{"ok":true}`.
- **Erros:** `{"error":"bad_json","message":"…"}` com HTTP 400 para entrada malformada.
- Aberto, sem autenticação, com limite de taxa — ok para chamar de um notebook ou script.

### Python

```python
import requests
r = requests.post("https://identar.lucas.mat.br/api/format",
                  json={"sql": "select a, b from t", "options": {"useTabs": False}})
print(r.json()["formatted"])
```
