# Examples

### Reading table data

```javascript
import * as redivis from 'redivis'

const rows = await redivis
  .organization('demo')
  .dataset('nyt covid us cases deaths')
  .table('covid_us states')
  // max_results and variables parameters are optional
  // If not specified will return all records and variables
  .listRows({ max_results: 100, variables: ['state', 'cases'] }) 
```

### **Execute a query**

```javascript
// Execute a query against fully qualified tables on Redivis. 
// Dataset at https://redivis.com/Demo/datasets/1921

import * as redivis from 'redivis'

const rows = await redivis
  .query(`
    SELECT * 
    FROM demo.nyt_covid_us_cases_deaths.covid_us_counties 
    WHERE state = 'California'
  `)
  .listRows()
```

### **Execute a scoped query**

```javascript
// Perform a query scoped to the "nyt covid us cases deaths" dataset. 
// Table at https://redivis.com/Demo/datasets/1921/tables

// We don't need to include fully-qualified table names 
//    if we scope our query to the appropriate dataset or workflow

import * as redivis from 'redivis'

const rows = await redivis
  .organization('demo')
  .dataset('nyt covid us cases_deaths')
  .query(`
    SELECT * 
    FROM covid_us_states
    WHERE state = 'California'
  `)
  .listRows()

```

### Embedding 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 * as redivis from 'https://cdn.jsdelivr.net/npm/redivis/+esm';

    const rows = await redivis
      .query(`
        SELECT *
        FROM demo.nyt_covid_us_cases_deaths.covid_us_counties
        WHERE state = 'California'
      `)
      .listRows();

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

### Authorization

{% hint style="info" %}
Authorization will happen automatically when querying data, but you can force an authorization flow via this method.
{% endhint %}

```javascript
import * as redivis from 'redivis'

redivis.authorize()
  .then(() => {
    // Authorization was successful!
  })
  .catch((e) => {
    // Authorization failed :/
  })
```


---

# Agent Instructions: Querying This Documentation

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

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.redivis.com/api/client-libraries/redivis-js/examples.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
