Skip to content

Control It 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. The full list of methods is in the API reference.

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

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').

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

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

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

Section titled “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.

// 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.

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

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

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.

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

Scrolls to a section, tag or operation and opens it. Takes an element ID or an operation’s method and path:

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

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

<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.

Methods for MCP export (generateMcpServer, downloadMcpServer, getMcpBudget) are described on MCP Server, and bindNavBar in Pre-rendering.