@scalar/api-reference

v0.1.5
Generate interactive API documentations from Swagger files

Scalar API Reference

Version Downloads Hits on jsdelivr License Discord

Generate interactive API documentations from Swagger files. Try our Demo

Screenshot of an API Reference

Installation

npm install @scalar/api-reference

Usage

<script setup>
import { ApiReference } from '@scalar/api-reference'
import '@scalar/api-reference/style.css'
</script>

<template>
  <ApiReference />
</template>

CDN

<!doctype html>
<html>
  <head>
    <title>Scalar API Reference</title>
    <meta charset="utf-8" />
    <meta
      name="viewport"
      content="width=device-width, initial-scale=1" />
  </head>
  <body>
    <!-- Add your own OpenAPI/Swagger specification URL here: -->
    <!-- Note: The example is our public proxy (to avoid CORS issues). You can remove the `data-proxy-url` attribute if you don’t need it. -->
    <script
      id="api-reference"
      data-url="https://cdn.jsdelivr.net/npm/@scalar/galaxy/dist/latest.yaml"
      data-proxy-url="https://proxy.scalar.com"></script>

    <!-- Optional: You can set a full configuration object like this: -->
    <script>
      var configuration = {
        theme: 'purple',
      }

      document.getElementById('api-reference').dataset.configuration =
        JSON.stringify(configuration)
    </script>
    <script src="https://cdn.jsdelivr.net/npm/@scalar/api-reference"></script>
  </body>
</html>

You can also use the following syntax to directly pass an OpenAPI specification:

<script
  id="api-reference"
  type="application/json">
  { … }
</script>

If you’d like to add a request proxy for the API client (to avoid CORS issues):

<script
  id="api-reference"
  type="application/json"
  data-proxy-url="https://proxy.scalar.com">
  { … }
</script>

Events [beta]

We have recently added two events to the standalone CDN build only.

scalar:reload-references

Reload the references, this will re-mount the app in case you have switched pages or the dom elements have been removed.

document.dispatchEvent(new Event('scalar:reload-references'))
scalar:update-references-config

If you have updated the config or spec, you can trigger this event with the new payload to update the app. It should update reactively so you do not need to trigger the reload event above after.

import { type ReferenceProps } from './types'

const ev = new CustomEvent('scalar:update-references-config', {
  detail: {
    configuration: {
      spec: {
        url: 'https://cdn.jsdelivr.net/npm/@scalar/galaxy/dist/latest.yaml',
      },
    },
  } satisfies ReferenceProps,
})
document.dispatchEvent(ev)

Vue.js

The API Reference is built in Vue.js. If you’re working in Vue.js, too, you can directly use our Vue components. Install them via npm:

npm install @scalar/api-reference

And import the ApiReference component and style to your app:

<script setup lang="ts">
import { ApiReference } from '@scalar/api-reference'
import '@scalar/api-reference/style.css'
</script>

<template>
  <ApiReference
    :configuration="{
      spec: {
        url: 'https://cdn.jsdelivr.net/npm/@scalar/galaxy/dist/latest.yaml',
      },
    }" />
</template>

You can pass props to customize the API reference.

Community

We are API nerds. You too? Let’s chat on Discord: https://discord.gg/scalar

License

The source code in this repository is licensed under MIT.

npm i @scalar/[email protected]

Metadata

Downloads