Websites

Search widget

Add Hayfork search to any website with one script tag. Visitors get a search box that opens a search dialog, with results that link straight to the right page. It works on any site, including Docusaurus (which also has a theme package), Starlight, VitePress, MkDocs and hand-written HTML, and needs no build step.

1. Create a website key

The widget runs in your visitors' browsers, so anyone can read the key in your page source. A website key is made for that. It can only search, only the collections you choose, and only from the websites you list. A copied key does not work on anyone else's site.

Create one from the API with an admin key. List every address the widget will run on, and the collection it should search:

curl https://api.hayfork.dev/api-keys \
  -H "Authorization: Bearer $ADMIN_KEY" -H "Content-Type: application/json" \
  -d '{
    "name": "Docs search widget",
    "scopes": ["search"],
    "allowed_origins": ["https://docs.example.com", "http://localhost:3000"],
    "collection_ids": ["COLLECTION_ID"],
    "expires_in_days": 365
  }'

The response includes the key in a key field, once. Find the collection id on the console's Collections page.

2. Add the script

Put this where the search box should appear, usually in your site header:

<script src="https://hayfork.dev/widget.js"
  data-key="YOUR_WEBSITE_KEY"
  data-collection="COLLECTION_ID"
  async></script>

The search box appears where the script tag is. To place it somewhere else, point data-target at an element, such as data-target="#search", and load the script anywhere on the page.

Visitors open the dialog by clicking the box, or with ⌘K on a Mac, Ctrl K elsewhere, or /. Arrow keys move through results, Enter opens one and Esc closes the dialog.

Options

AttributeDefaultNotes
data-keyrequiredA website key.
data-collectionrequiredThe collection id to search. It must be one of the key's collections.
data-placeholderSearchText in the search box and the dialog.
data-targetnoneA CSS selector for the element to put the search box in. Without it, the box goes where the script tag is.
data-themeautolight, dark, or auto to follow the visitor's system setting.
data-shortcuttruefalse turns off the ⌘K, Ctrl K and / shortcuts.
data-results8Results to show, 1 to 20.

The widget draws inside a shadow root, so your site's CSS does not change it and its CSS does not leak into your site. To open it from your own button, call open() on the element it creates:

document.querySelector("[data-hayfork-search]").hayfork.open();

Usage and limits

Security

Troubleshooting

What you seeWhat to do
No search box, and The search widget needs data-key and data-collection in the browser consoleAdd both attributes to the script tag.
"This search box is not set up for this website."The key was refused. Check that the page's exact address, with https:// and any www., is in the key's allowed_origins, that the collection is one of the key's collection_ids, and that the key has not expired or been revoked.
"Search is busy. Try again in a minute."Either one visitor passed the per-visitor limit, which resets after a minute, or the plan's searches for the month are used up. The browser console says which. Check usage in the console.
"Search is unavailable right now."The API could not be reached. Check your network, or try again shortly.