> For the complete documentation index, see [llms.txt](https://docs.redivis.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.redivis.com/api/client-libraries/redivis-js/examples/using-in-the-browser.md).

# Using in the browser

The redivis-js library runs in the browser as well as in Node.js, so you can build web applications, dashboards, and interactive notebooks (e.g., on [Observable](https://observablehq.com)) on top of data in Redivis.

### Embed in an HTML page

```html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Redivis query example</title>
</head>
<body>
  <script type="module">
    import redivis from 'https://cdn.jsdelivr.net/npm/redivis/+esm';

    const rows = await redivis
      .query(`
        SELECT FIREYEAR, COUNT(*) AS count
        FROM demo.us_forest_service_fires.us_fire_perimeters
        GROUP BY FIREYEAR
        ORDER BY FIREYEAR
      `)
      .toRowsArray();

    console.log(rows);
  </script>
</body>
</html>
```

### Authenticate users

Public data can be read without authenticating. When a resource requires it, the library opens a popup for the user to sign in to Redivis. Since browsers may block popups that aren't opened in response to a user action, call `authorize()` from a click handler when you know authentication will be needed:

```javascript
signInButton.addEventListener('click', async () => {
  await redivis.authorize();
  const rows = await redivis.table('my_org.my_dataset.my_table').toRowsArray();
});

// Later, e.g. in a sign-out button handler
await redivis.deauthorize();
```

The token obtained is held only for the current tab, and is read-only unless a request requires more access.

### Read data through your own server

To show data that requires authentication to users without a Redivis account — without exposing your credentials — route the library's requests through your own server. The server adds an [API token](https://docs.redivis.com/api/rest-api/authorization.md#api-token) to each request, and forwards it to `https://redivis.com/api/v1`:

```javascript
// In the browser
redivis.setApiProxy('/api/redivis');
const rows = await redivis.table('my_org.my_dataset.my_table').toRowsArray();
```

```javascript
// On your server (e.g., an Express app in Node.js)
import { Readable } from 'node:stream';

// Only forward the requests your application needs (here, GET requests)
app.get('/api/redivis/*', async (req, res) => {
  const response = await fetch(`https://redivis.com/api/v1${req.url.slice('/api/redivis'.length)}`, {
    headers: { Authorization: `Bearer ${process.env.REDIVIS_API_TOKEN}` },
  });
  res.status(response.status).type(response.headers.get('content-type') ?? 'application/octet-stream');
  response.body ? Readable.fromWeb(response.body).pipe(res) : res.end();
});
```

{% hint style="warning" %}
Anyone who can reach your proxy can act with its credentials, so make sure it only forwards the requests your application needs — e.g., `GET` requests for specific tables — and use an API token with the minimum necessary scope.
{% endhint %}

### Memory limits

To protect the browser tab from running out of memory, [`toArrowTable()`](https://docs.redivis.com/api/client-libraries/redivis-js/reference/table/table.toarrowtable.md) and [`toRowsArray()`](https://docs.redivis.com/api/client-libraries/redivis-js/reference/table/table.torowsarray.md) read at most 100MB by default in the browser, throwing a [`ValueError`](https://docs.redivis.com/api/client-libraries/redivis-js/reference/redivis/redivis.errors.md) for larger reads. Select fewer variables or rows, raise the limit with `maxBytes`, or stream the data with [`toArrowBatchIterator()`](https://docs.redivis.com/api/client-libraries/redivis-js/reference/table/table.toarrowbatchiterator.md):

```javascript
// Only the variables you need
const rows = await table.toRowsArray({ variables: ['FIREYEAR', 'GISACRES'] });

// Raise the limit
const arrowTable = await table.toArrowTable({ maxBytes: 500 * 1024 * 1024 });

// Stream the data, keeping only aggregates in memory
let count = 0;
for await (const batch of table.toArrowBatchIterator()) {
  count += batch.numRows;
}
```

### Visualize data

Arrow tables can be passed directly to many visualization and analysis libraries:

```javascript
import * as Plot from 'https://cdn.jsdelivr.net/npm/@observablehq/plot/+esm';

const table = redivis.table('demo.us_forest_service_fires:5k9t:v1_1.us_fire_perimeters:9r58');
const data = await table.toArrowTable({ variables: ['FIREYEAR', 'GISACRES'] });

document.body.append(Plot.dot(data, { x: 'FIREYEAR', y: 'GISACRES' }).plot());
```

See [Reading tabular data](https://docs.redivis.com/api/client-libraries/redivis-js/examples/reading-tabular-data.md#using-other-libraries) for more.

### Cancel reads

Pass an [`AbortSignal`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal) to cancel a read — e.g., when the user navigates away, or changes a filter before the previous read finishes:

```javascript
let controller;

async function load(year) {
  controller?.abort();
  controller = new AbortController();
  return redivis
    .query(`SELECT * FROM demo.us_forest_service_fires.us_fire_perimeters WHERE FIREYEAR = ${Number(year)}`)
    .toRowsArray({ signal: controller.signal });
}
```

### Limitations

Functionality that needs a filesystem is only available in Node.js: [downloading](https://docs.redivis.com/api/client-libraries/redivis-js/reference/table/table.download.md) tables, files, and directories; uploading from a file path; and [adding the files of a local directory](https://docs.redivis.com/api/client-libraries/redivis-js/reference/table/table.addfiles.md) to a table. In the browser, read data and files into memory (or stream them) instead, and upload `File`s and `Blob`s.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.redivis.com/api/client-libraries/redivis-js/examples/using-in-the-browser.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
