# Control It from JavaScript

> Load specs, set credentials, select servers and navigate <rapi-doc> from JavaScript.

`<rapi-doc>` is a DOM element, so you control it from JavaScript like any other: set attributes, call its methods and listen to its [events](/docs/rapidoc/programmatic-api/event-hooks/). The full list of methods is in the [API reference](/docs/rapidoc/reference/methods/).

## Wait until it is ready

Methods that work on the spec need the spec to be loaded. Wait for `spec-loaded`:

```javascript
const doc = document.querySelector('rapi-doc');
doc.addEventListener('spec-loaded', (e) => {
  console.log('Loaded', e.detail.info.title);
});
```

If your script may run before the RapiDoc bundle, wait for the element to be defined first: `await customElements.whenDefined('rapi-doc')`.

## Load a spec: `loadSpec(specOrUrl)`

Takes a spec object or a URL. Useful when you fetch the spec yourself, or switch between specs:

```javascript
async function showTenant(tenantId) {
  const res = await fetch(`/api/specs/${tenantId}.json`);
  doc.loadSpec(await res.json());
}
```

## Set credentials: `setApiKey(schemeId, value, options?)`

Fills in the credential of a security scheme, so readers don't have to. `schemeId` is the scheme's name under `components.securitySchemes`. For an HTTP bearer scheme, RapiDoc adds the `Bearer ` prefix.

```javascript
// After your app signs the user in
doc.setApiKey('BearerAuth', sessionToken);
```

Pass `{ note }` to say where the credential came from. The Authentication section then shows the note instead of that scheme's form, with a **Use a different token** link that opens the form. Changing or removing the key removes the note.

```javascript
doc.setApiKey('BearerAuth', sessionToken, { note: 'Using your sign-in (ann@example.com).' });
```

Related methods: `setHttpUserNameAndPassword(schemeId, username, password)` for HTTP Basic, and `removeAllSecurityKeys()`.

## Select a server: `setApiServer(url)`

Selects one of the spec's `servers` by URL, as if the reader had picked it. It returns `false` and changes nothing when the URL is not in the spec's `servers`. To send requests to a URL that isn't listed, set the `server-url` attribute instead.

```javascript
document.getElementById('env-select').addEventListener('change', (e) => {
  doc.setApiServer(e.target.value);
});
```

## Navigate: `scrollToPath(id)`

Scrolls to a section, tag or operation and opens it. Takes an [element ID](/docs/rapidoc/customizing/linking/#element-ids) or an operation's method and path:

```javascript
doc.scrollToPath('get-pet-by-id');     // by operation ID
doc.scrollToPath('get-/pet/{petId}');  // by method and path
doc.scrollToPath('tag--store');        // a tag
```

## Example: your own toolbar

With `show-header="false"`, you can build the controls into your own page:

```html
<div class="toolbar">
  <select id="env-picker">
    <option value="https://petstore.swagger.io/v2">Production</option>
    <option value="http://petstore.swagger.io/v2">Production (HTTP)</option>
  </select>
  <button id="jump-login">Jump to login</button>
</div>

<rapi-doc
  id="the-doc"
  spec-url="https://petstore.swagger.io/v2/swagger.json"
  render-style="focused"
  show-header="false"
  style="height: calc(100vh - 50px)"
></rapi-doc>

<script>
  const doc = document.getElementById('the-doc');
  document.getElementById('env-picker').addEventListener('change', (e) => doc.setApiServer(e.target.value));
  document.getElementById('jump-login').addEventListener('click', () => doc.scrollToPath('get-/user/login'));
</script>
```

The options must be URLs from the spec's `servers` list.

> **Tip:**
> Methods for MCP export (`generateMcpServer`, `downloadMcpServer`, `getMcpBudget`) are described on [MCP Server](/docs/rapidoc/mcp-export/#from-javascript), and `bindNavBar` in [Pre-rendering](/docs/prerender/#a-static-sidebar).
