Sphinx supports injecting external scripts with arbitrary HTML attributes through the html_js_files config variable in conf.py. Each entry is a two-tuple of (url, attributes_dict).
1

Open conf.py

Open conf.py at the root of your Sphinx project (typically inside the docs/ directory).
2

Add html_js_files

Add or extend the html_js_files list. Pass a tuple with the script URL and a dictionary of HTML attributes:
conf.py
Replace kb_xxx and bq_pk_... with your actual values.If html_js_files already exists, append the tuple to the list:
conf.py
3

Build and verify

Run make html (or sphinx-build -b html . _build/html). Open any generated HTML file in _build/html and search for beforequery-widget.js to confirm the script tag was emitted with the correct attributes.
The html_js_files tuple form with an attributes dict was introduced in Sphinx 1.8. If you are on an older version, upgrade Sphinx or use the theme template override approach: create _templates/layout.html, extend your base theme, and add the <script> tag in an {% block footer %} or {% block extrahead %} block.
Get your keys — your knowledge base ID and public client key (bq_pk_...) are in the BeforeQuery dashboard under Knowledge Base → Integration. Add your Sphinx site’s domain to the key’s origin allowlist. For Read the Docs hosted sites, add your .readthedocs.io subdomain (or custom domain).
For all widget configuration options, see Widget reference.