Generate a custom search token for the Community Hosted Search Page component

In this article

To override the default search token for your Community Hosted Search Page configuration, you must generate a custom search token to authenticate queries on the Coveo Community Hosted Search Page component. Custom search tokens are typically used to add additional parameters to the search token. See Use search token authentication for details.

To generate a custom search token

  1. Create an Apex class.

  2. In the new Apex class, enter the following template:

    global with sharing class <MY_APEX_CLASS> implements CoveoV2.SearchTokenGeneratorInterface {
        public String generateSearchToken(Map<String, Object> params) {
            Map<String, Object> searchTokenOptions = new Map<String, Object>();
    
            // Make sure to include this part when generating your custom search token.
            if (params.containsKey('searchHub')) {
                String searchHub = (String) params.get('searchHub');
                searchTokenOptions.put('searchHub', searchHub);
            }
    
            // Example of how to add additional user identities to the search token.
            List<CoveoV2.UserIdentity> additionalUserIdentities = new List<CoveoV2.UserIdentity>{
                new CoveoV2.UserIdentity('johndoe@coveo.com'),
                new CoveoV2.UserIdentity('john@doe.com', 'Email Security Provider'),
                new CoveoV2.UserIdentity('john@doe.com', 'Email Security Provider', 'User')
            };
            searchTokenOptions.put('additionalUserIdentities', additionalUserIdentities);
    
            String token = CoveoV2.Globals.generateSearchToken(searchTokenOptions);
            return token;
        }
    }

    Where you replace <MY_APEX_CLASS> with the name you chose for your new Apex class.

    Important

    The Apex class must be global and it must implement CoveoV2.SearchTokenGeneratorInterface. Otherwise, it won’t appear as an option in the Token Generator picklist on the Coveo Community Hosted Search Page component.

  3. (Optional) Override the default search token parameters. See the generateSearchToken reference documentation for details.

    Warning

    The value of the searchHub parameter received when the generateSearchToken function is called will match the name of your hosted search page configuration. This value must not be changed and it must be added to the search token options.

  4. Save the Apex class.

What’s Next?

Once you have an Apex class that implements CoveoV2.SearchTokenGeneratorInterface, it will appear as an option in the Token Generator picklist on the Coveo Community Hosted Search Page component. You can now select your new Apex class to override the default search token for your Community Hosted Search Page configuration.