---
title: Send Apex context to a JavaScript search interface (Deprecated)
slug: '1169'
canonical_url: https://docs.coveo.com/en/1169/
collection: coveo-for-salesforce
source_format: adoc
---
# Send Apex context to a JavaScript search interface (Deprecated)

Salesforce Classic

> **Deprecated**
>
> This feature is still available, but no longer supported as of the August 2023 release of Coveo for Salesforce version [5.2](https://docs.coveo.com/en/n5bj0150.md#august-2023-release-v5-2-initial-release).

You may want to have access to some Apex variables in your JavaScript Search interface.
You can use the search context, a map of Apex values to do this as shown in the following example.

**Example**

In an on-premises Coveo REST Search API, you define a trusted application with its `secret key`.
You want to use the `secret key` to request a search token from your Apex page, and then use the received search token in your JavaScript search interface to set the `accessToken` to authorize the page to communicate with the REST Search API.

Here is how to do that:

1. In Salesforce, create an Apex class (see [Adding an Apex Class](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_qs_class.htm)) that allows you to get the token and create a search context with the token in it.

   ```apex
   public class MySearchController {
   ​
     class UserId{
       public String name {get; set;}
       public String provider {get; set;}
       public String type {get; set;}
       public UserId(String name, String provider, String type){
           this.name = name;
           this.provider = provider;
           this.type = type;
       }
     }
   ​
     class TokenResponse {
       public String token;
     }
   ​
     public SearchContext getContext(){
       HttpRequest req = new HttpRequest();
       req.setEndpoint('https://YouRestApiUrl/rest/search/token');
       req.setMethod('POST');
       req.setHeader('Authorization', 'Bearer the_secret_key_for_the_application');
       req.setHeader('Content-Type', 'application/json');
   ​
       List<UserId> userIds = new List<UserId>();
       userIds.add(new UserId('foo@bar.com', 'Email Security Provider', 'User'));
   ​
       Map<String,Object> body = new Map<String, Object>();
       body.put('userIds', userIds);
       req.setBody(JSON.serialize(body));
   ​
       req.setTimeout(10000);
   ​
       Http http = new Http();
       HTTPResponse res = http.send(req);
   ​
       System.debug(res.getBody());
   ​
       String token = ((TokenResponse)JSON.deserialize(res.getBody(), TokenResponse.class)).token;
   ​
       SearchContext context = new SearchContext();
       context.put('token',token);
       return context;
     }
   }
   ```

1. Create a new Visualforce page (see [Defining Visualforce Pages](https://help.salesforce.com/apex/HTViewHelpDoc?id=pages_creating.htm)) that adds the search context in the component from the controller.

   ```xml
   <apex:page standardstylesheets="false" sidebar="false" docType="html-5.0" controller="MySearchController">
       <Coveo:SearchInterface mobile="false" searchContext="{!context}" />
   </apex:page>
   ```

1. Using the Interface Editor (see [Accessing the Interface Editor for a Search API Hosted Page](https://www.coveo.com/go?dest=adminhelp70&lcid=9&context=1032)):

.. Select the **Advanced Mode**, and then select the **Code** tab.

.. In the page code, add the following code to set the token in the search interface endpoint options.

   ```html
   <script type="text/javascript">
     $(function(){
       Coveo.Rest.SearchEndpoint.endpoints['default'].options.accessToken = Coveo.context.token
     })
   </script>
   ```