bnspell-api 0.1.0

HTTP API for the Bengali spell checker
Documentation
# bnspell-api

HTTP API for the Bengali spell checker. The process loads the dictionary, then serves `GET /health`, `GET /api/health`, `POST /api/check`, and `GET /api/suggest`. `SIGINT` and `SIGTERM` stop the server after in-flight requests finish.

From the repository root, after the dictionary exists (`pnpm --filter bengali-spell-checker build:dictionary`):

```sh
cargo run -p bnspell-api
```

`cargo install bnspell-api` installs the same binary. The crate does not embed the word list. Set `BNSPELL_DICT` to a directory from the npm package `bengali-spell-checker` (`dictionary/` inside the package) or from a repository build (`packages/bengali-spell-checker/dictionary`).

## Endpoints

`GET /health` and `GET /api/health` return `{"ready": true}` once the process is listening. The dictionary is loaded before the socket opens, so a successful response means the checker is ready. Neither path counts toward the rate limit.

`POST /api/check` takes JSON:

| Field | Type | |
| --- | --- | --- |
| `text` | string, required | At most 100000 UTF-16 code units. Longer text is `413` with `{"error":"text must be at most 100000 characters"}`. |
| `suggestions` | boolean, optional | Defaults to `true`. |
| `strict` | boolean, optional | Defaults to `false`. When `true`, a widely used variant is a misspelling and the Bangla Academy form is the first suggestion. |
| `limit` | integer, optional | From 1 to 20. Defaults to 5. |

A field with the wrong JSON type is `400`. The body is JSON:

```json
{ "misspellings": [{ "word": "বাংলাদেস", "start": 5, "end": 13, "suggestions": ["বাংলাদেশ"] }] }
```

`start` and `end` are UTF-16 code unit offsets into `text`, the same indexes JavaScript uses. `suggestions` is omitted when the request set `"suggestions": false`.

`GET /api/suggest` takes `word` (required, at most 1000 UTF-16 code units). A longer word is `422` with `{"error":"word must be at most 1000 characters"}`. `limit` is the same integer as on `/api/check`. `strict` is the query value `true` or `false` and defaults to `false`. Any other `strict` value is `400`.

```json
{ "word": "বাংলাদেস", "correct": false, "suggestions": ["বাংলাদেশ"] }
```

Every error body is `{"error": "..."}`.

| Status | When |
| --- | --- |
| 400 | The JSON or a field is invalid. |
| 408 | The request exceeded `BNSPELL_REQUEST_TIMEOUT_SECS` (default 30). |
| 413 | The body is larger than `BNSPELL_MAX_BODY_BYTES` (default 1048576, at most 8388608), or `text` is longer than 100000 UTF-16 code units. |
| 422 | `word` is longer than 1000 UTF-16 code units. |
| 429 | The client passed the per-IP rate limit. `Retry-After` is the seconds left in the window. |

## Environment

| Variable | Default | |
| --- | --- | --- |
| `HOST` | `0.0.0.0` | Bind address. |
| `PORT` | `8787` | Bind port. |
| `BNSPELL_DICT` | walk up to the workspace dictionary | Directory of `bn_BD.aff` and `bn_BD.dic`. Optional files in that directory: `bn_BD.freq`, `bn_BD.variants`, `bn_BD.removals`, `bn_BD.names`, `bn_BD.compounds`. |
| `BNSPELL_CORS_ORIGINS` | localhost only | Comma-separated exact origins, for example `https://app.example.com, http://localhost:3000`. Unset allows any `http` or `https` origin whose host is `localhost`, `127.0.0.1`, or `::1`. A wildcard is rejected. Setting the variable replaces the localhost default; it does not add to it. |
| `BNSPELL_TRUSTED_PROXY` | `false` | Set to `true` or `1` only when every request arrives through one reverse proxy. The client address is then the rightmost `X-Forwarded-For` entry, which is the peer that proxy accepted. Leave this unset when the port is reachable directly: clients can otherwise pick their own rate-limit bucket. |
| `BNSPELL_MAX_BODY_BYTES` | `1048576` | Request body cap. `1` through `8388608`. |
| `BNSPELL_REQUEST_TIMEOUT_SECS` | `30` | Per-request timeout. `1` through `86400`. |
| `BNSPELL_RATE_LIMIT` | `60` | Requests per window per client. `0` disables the limiter. `/health` and `/api/health` are never counted. |
| `BNSPELL_RATE_WINDOW_SECS` | `60` | Window length. `1` through `86400`. |

The limiter is in memory and per process. Two replicas do not share counters.

## Docker

Build from the repository root. The image builds the dictionary in a Node stage, compiles the binary, and runs as the unprivileged user `bnspell`. A `HEALTHCHECK` calls `GET /health`.

```sh
docker build -f crates/bnspell-api/Dockerfile -t bnspell-api .
docker run --rm -p 8787:8787 bnspell-api
curl -fsS http://127.0.0.1:8787/health
curl -fsS -X POST http://127.0.0.1:8787/api/check -H 'content-type: application/json' -d '{"text":"ঢাকা বাংলাদেস।"}'
```

The image is `linux/amd64`. `BNSPELL_DICT` inside the image is `/opt/bnspell/dictionary`.

### Dokploy

Create an application from this GitHub repository.

- Build type: Dockerfile
- Dockerfile path: `crates/bnspell-api/Dockerfile`
- Docker context: the repository root (`.`)
- Container port: `8787`
- Environment: `BNSPELL_TRUSTED_PROXY=true` and `BNSPELL_CORS_ORIGINS` set to the site that will call the API. Dokploy's proxy is the trusted hop, so the rightmost `X-Forwarded-For` entry is the client.
- Health check: HTTP `GET /health` on port `8787`. The image already has a Docker `HEALTHCHECK` for the same path.

Attach a domain in Dokploy and let it terminate TLS. Do not publish port `8787` on the host if the proxy is the only client you want; with `BNSPELL_TRUSTED_PROXY=true`, anything that can connect directly can forge `X-Forwarded-For`.

The release workflow also pushes `ghcr.io/habibium/bnspell-api` (tags `0.1.0`, `0.1`, and `latest` for a non-prerelease `v0.1.0`). A Dokploy application can use that image instead of building from the Dockerfile. GitHub Container Registry packages are private until you change the package visibility to public.

## Credits and licenses

The image builds the dictionary from the word lists in this repository. The project license is [GPL-3.0-only](../../LICENSE).

The base dictionary is the Bengali (Bangladesh) spellchecking dictionary by Jacob Thomas, version 0.01 (2 August 2022), licensed GPL-2.0-or-later. See its [Copyright](../../dictionaries/bn_BD.Jacob-Thomas.02-08-2022.v0.01/Copyright) file. Source: http://banglatypefoundry.com/spellchecker/

Word frequencies and the Wikipedia typo set are derived from Bengali Wikipedia ([CC BY-SA 4.0](https://creativecommons.org/licenses/by-sa/4.0/)). Most of the name list comes from [Wikidata](https://www.wikidata.org/) labels ([CC0](https://creativecommons.org/publicdomain/zero/1.0/)).