Access Headless through Atomic

This is for:

Developer

The Atomic library uses Coveo Headless to interface with Coveo. For advanced customizations not possible in Atomic components, you can leverage the Headless library directly through Atomic.

The following example code shows how to use Headless to modify the advanced query expression in an Atomic search interface.

<!DOCTYPE html>
<html dir="ltr" lang="en">
  <head>
    <script type="module" src="https://static.cloud.coveo.com/atomic/v2/atomic.esm.js"></script>
    <link rel="stylesheet" href="https://static.cloud.coveo.com/atomic/v2/themes/coveo.css" />
    <script type="module">
      import {
        buildRedirectionTrigger,
        loadAdvancedSearchQueryActions
      } from 'https://static.cloud.coveo.com/atomic/v2/headless/headless.esm.js'; 1

      (async () => {
        await customElements.whenDefined('atomic-search-interface');
        const searchInterface = document.querySelector('atomic-search-interface');
        await searchInterface.initialize({
          accessToken: 'xx564559b1-0045-48e1-953c-3addd1ee4457',
          organizationId: 'searchuisamples',
          organizationEndpoints: await searchInterface.getOrganizationEndpoints('searchuisamples'),
        });
 
        const engine = searchInterface.engine; 2

        const controller = buildRedirectionTrigger(engine); 3
        controller.subscribe(() => { 4
          const { redirectTo } = controller.state;
          if (redirectTo) {
            window.location.replace(redirectTo);
          }
        });

        const action = loadAdvancedSearchQueryActions(engine).updateAdvancedSearchQueries({ 5
          aq: '@author="BBC News"',
        });
        engine.dispatch(action); 6
 
        searchInterface.executeFirstSearch();
      })();
    </script>
  </head>
  <body>
    <atomic-search-interface id="search">
      <atomic-search-box></atomic-search-box>
      <atomic-query-summary></atomic-query-summary>
      <atomic-result-list>
        <!-- ... -->
      </atomic-result-list>
    </atomic-search-interface>
  </body>
</html>
1 Import the target Headless controllers or methods via CDN.
2 After initializing the search interface, access and store the engine in a variable. You’ll need it to use Headless.
3 Use the engine and the Headless controller builder you imported above in order to create a controller that can subscribe to the engine and dispatch actions.
4 Subscribe to the engine and handle the redirection triggers.
5 Use the engine and the Headless action creator you imported above in order to create an action that modifies the advanced query.
6 Dispatch the action.

In your browser developer tools, if you inspect the search request sent to the https://platform.cloud.coveo.com/rest/search/v2 endpoint, you’ll see that the request payload includes the following search request parameter, as desired.

{
  ...
  "aq": "@author=\"BBC News\"",
  ...
}

You can find a more complex example in the Atomic project repository.