# Libraries

> Read and write .glx files from your own code: the Rust crate geolith-glx and the npm package @geolith/glx, with install lines and runnable examples.

The `.glx` reader and writer come as a Rust crate and as an npm package. Both are Apache-2.0, so you can use them in any project. The npm package is the Rust reader built to WebAssembly, so the two give the same answers.

| Language   | Package                                                      | Use it to                                                                 |
| ---------- | ------------------------------------------------------------ | ------------------------------------------------------------------------- |
| Rust       | [`geolith-glx`](https://crates.io/crates/geolith-glx)        | read and write `.glx` files, and run the command line tools               |
| TypeScript | [`@geolith/glx`](https://www.npmjs.com/package/@geolith/glx) | query a file from Node, a browser or Cloudflare Workers, over range reads |

To serve the Pelias-compatible geocoding API from a file, use [geolith Search](/search/self-hosting/libraries). To route over a file, use [geolith Routing](/routing/self-hosting). Both read through these two packages.

## Rust

```bash
cargo add geolith-glx
```

The crate is published as `geolith-glx` because the name `glx` is taken on crates.io. The library is imported as `glx`. It is the `.glx` container (file header, part table, checksum, `parts` merge and append, `ContainerWriter`) plus one cargo feature per part format:

| Feature   | Adds                                                                                      |
| --------- | ----------------------------------------------------------------------------------------- |
| `search`  | The `SRCH` part: the geocoding index format, reader and writer. On by default.            |
| `routing` | The `ROUT` part: the routing graph tile format and its sections. It needs no search code. |
| `pelias`  | Pelias-shaped GeoJSON answers, as the Search server returns them.                         |
| `build`   | The `glx-build` and `glx-query` tools.                                                    |
| `wasm`    | The WebAssembly entry points for Cloudflare Workers.                                      |
| `parser`  | The trained query parser. It adds about 170 KB to the WebAssembly reader.                 |

For the routing part only, with no search code:

```bash
cargo add geolith-glx --no-default-features --features routing
```

The tools install from crates.io. See [Command line](/glx/cli).

```bash
cargo install geolith-glx --features build
echo '{"text":"india gate"}' | glx-query --file places.glx --text
```

`glx-query` prints one JSON line per query: the ids and ranks of the hits and the cost of the read.

## TypeScript

```bash
pnpm add @geolith/glx
```

The package is ESM only. It needs Node 26 or later, Cloudflare Workers or a modern browser. This example opens a local file and asks for a place and the places around a point:

```ts
import { openAsBlob } from 'node:fs';
import { readFile } from 'node:fs/promises';
import { fileURLToPath } from 'node:url';
import { FileSource, GlxFile, init } from '@geolith/glx';

init(
  await readFile(fileURLToPath(import.meta.resolve('@geolith/glx/glx.wasm'))),
);

const file = await GlxFile.open(new FileSource(await openAsBlob('places.glx')));
const { collection } = await file.search({ text: 'india gate' });
console.log(collection.features[0].properties.name); // India Gate

const near = await file.reverse({ lon: 77.2295, lat: 28.6129, radiusKm: 1 });
console.log(near.collection.features[0].properties.label); // India Gate, New Delhi, India
```

Open the file once and reuse the `GlxFile`: opening reads the header and the small sections that stay in memory. A file has `search`, `autocomplete`, `reverse`, `nearby` and `place` methods; each takes the same parameters as the [API](/search/api/search).

### Categories and names in another language

Every query takes `categories` and `lang`, the same as the [hosted API](/search/hosted/overview#categories-and-language):

```ts
await file.search({ text: 'coffee', categories: ['food'] });
await file.nearby({ lon: 77.2295, lat: 28.6129, radiusKm: 1, lang: 'hi' });
await file.place(['geolith:venue:101'], { lang: 'hi' });
```

`categories` holds at most 8 names: Pelias taxonomy names (`education`, `health`, `food`, `transport`, `religion`, `finance`, `accommodation`, `retail`) or category words (`college`, `bakery`). `lang` is a two- or three-letter code. A bad value throws `GlxQueryError`.

### Storage

| Source        | Reads                                         |
| ------------- | --------------------------------------------- |
| `FileSource`  | A local `File` or `Blob`                      |
| `FetchSource` | Any HTTP server that answers `Range` requests |
| `R2Source`    | A Cloudflare R2 binding                       |

In a Cloudflare Worker, pass the reader as a module and read from R2:

```ts
import wasm from '@geolith/glx/glx.wasm';
import { GlxFile, R2Source, init } from '@geolith/glx';

init(wasm);

export default {
  async fetch(request: Request, env: { GLX: R2Bucket }): Promise<Response> {
    const file = await GlxFile.open(new R2Source(env.GLX, 'planet.glx'));
    const text = new URL(request.url).searchParams.get('text') ?? '';
    const { collection } = await file.search({ text });
    return Response.json(collection);
  },
};
```

Over HTTP, from Node or a browser, use `FetchSource` with a URL. The server must answer `Range` requests. In a browser, fetch the `.wasm` bytes (for example with a bundler `?url` import) and pass them to `init`.

The package ships two readers: `@geolith/glx/glx.wasm` and `@geolith/glx/glx-parser.wasm`, which adds the trained query parser. They have the same API; pass either to `init`.
