> For the complete documentation index, see [llms.txt](https://outerbase.gitbook.io/untitled/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://outerbase.gitbook.io/untitled/introspection/remote-queries.md).

# Remote queries

Outerbase is incredibly powerful as a data insights tool and the ability to save queries we run and reference often makes jobs easier. Even more so we can take those saved remote queries from Outerbase and have our application code execute them remotely as well.

A huge benefit of executing saved queries is two-fold:

1. Saved queries in Outerbase can have cached responses for increased response times, particularly helpful for queries that are operation intensive or used by many people.
2. When you want run a SQL query from a platform such as your frontend without having to expose the actual SQL statement to the platform itself. It only needs to know the saved queries unique ID.

Let's take a look at how we can trigger saved queries from our application.

## Add script to package

To generate an ENUM map of our queries names & ID values in your project we can add a script to our `package.json` file that handles this automatically.

```json
"scripts" {
    "db:queries": "sync-remote-queries PATH=./queries API_KEY=your_api_key"
}
```

## Run the script

With our script in place all we need to do to generate a new Typescript file containing this map locally is by running

```
npm run db:queries
```

Using the generated map in your code base helps ensure that you are referencing saved queries that do in fact exist in the Outerbase account you have linked via the API key. When they no longer exist and you re-run the script above your project should start throwing errors to alert you.

## Sample output

After running the script above you should see a new `index.ts` file in the path provided in your `package.json` file. Now you can reference your saved queries in a type-safe manner.

```typescript
export enum RemoteQuery {
    'My First Query' = 'ffffffff-c3p0-4cd9-9325-31bfea659ec1',
    'My Second Query' = 'ffffffff-r2d2-4cd9-9325-31bfea659ec1',
}
```

## Requesting a response

With a type-safe enum now in place, we can safely call an SDK function from the `OuterbaseConnection` class to `runSavedQuery` which takes a parameter of a remote saved query ID.

```typescript
const connection = new OuterbaseConnection(
    'YOUR_API_KEY'
)
connection.runSavedQuery(RemoteQuery['My First Query'])
```


---

# 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 dynamically by asking a question.

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

```
GET https://outerbase.gitbook.io/untitled/introspection/remote-queries.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
