Skip to content

CustomEvents dispatched by <rapi-doc>. Listen with addEventListener; the payload is in event.detail. How to use them is in React to Events.

const doc = document.querySelector('rapi-doc');
// Before a Try request is sent: add a header
doc.addEventListener('before-try', (e) => {
e.detail.request.headers.append('X-Tenant-ID', 'acme');
});
// The reader picked another server
doc.addEventListener('api-server-change', (e) => {
console.log('Server:', e.detail.selectedServer);
});
EventDescription
before-render
Dispatched before rendering starts. Provides the parsed OpenAPI specification object via e.detail.spec, allowing programmatic modifications before the UI tree is built.
spec-loaded
Dispatched after the OpenAPI specification has been fully fetched, parsed, resolved, and rendered into the DOM. Provides the resolved spec object in e.detail.
before-try
Dispatched when a user clicks "TRY" before the network request is initiated. Provides a mutable Request-like object via e.detail.request to append headers, rewrite parameters, or modify payloads, along with an AbortController via e.detail.controller to abort requests conditionally.

Example: Append custom authorization headers before sending:
 rapidocEl.addEventListener('before-try', (e) => {
  if (e.detail.request.method === 'POST') {
    e.detail.request.headers.append('X-Tenant-ID', 'corp-acme-123');
  }
}); 
Example: Append query parameters to all GET requests:
 rapidocEl.addEventListener('before-try', (e) => {
  if (e.detail.request.method === 'GET') {
    const url = new URL(e.detail.request.url);
    url.searchParams.append('client_version', '2.4.0');
    e.detail.request.url = url.toString();
  }
}); 
Example: Abort specific requests conditionally:
 rapidocEl.addEventListener('before-try', (e) => {
  if (e.detail.request.method === 'DELETE') {
    alert('DELETE operations are disabled in this sandbox environment.');
    e.detail.controller.abort();
  }
}); 
after-try
Dispatched after a try request completes. Provides e.detail.request and e.detail.response. In the event of a network or protocol error, provides request and error objects.
request-aborted
Dispatched when an in-flight API try request is aborted by the user or programmatically via AbortController.
api-server-change
Dispatched when the active target API server is modified in the dropdown.
before-authorize
Dispatched immediately before opening the OAuth authorization popup window. Provides e.detail.securitySchemeId, e.detail.flowType, e.detail.authUrl, and mutable e.detail.authParams (URLSearchParams). Calling e.preventDefault() cancels opening the authorization window.

Example: Dynamically inject a launch token or proxy parameter:
 rapidocEl.addEventListener('before-authorize', (e) => {
  e.detail.authParams.set('launch', 'xyz123');
}); 
before-token-request
Dispatched immediately before sending the POST request to the OAuth2 token endpoint. Provides e.detail.securitySchemeId, e.detail.grantType, e.detail.tokenUrl, mutable e.detail.params (URLSearchParams), e.detail.headers (Headers), and mutable e.detail.request. Calling e.preventDefault() cancels the token request.

Example: Add a custom token audience or header:
 rapidocEl.addEventListener('before-token-request', (e) => {
  e.detail.params.set('audience', 'https://api.mycompany.com');
  e.detail.headers.set('X-Client-Version', '1.0.0');
});