Calling the Search and Recommendation API

In Sitecore Search, HTTPS protocol is required when using the Search and Recommendation API. The API accepts requests in JSON format.

API URL and methods

You can get the API URL in Sitecore Search, in the Developer Resources section, on the API Access tab.

Use the POST method to send requests to the Search and Recommendation API.

Authentication

You must authenticate to access Sitecore Search REST APIs. Use an API key or access token in the request header.

Mandatory objects

When you send requests to the API, you must include these objects to get a response:

  • rfk.domainId - add this to the header of your request to specify the domain for which you want to make the request.
  • widget.items.rfkd_id - add this key to specify the rfkid of the widget you want to call.
  • widget.items.entity - add this key to specify the entity you want to use for this request.
  • context.locale - If you have a multi-locale domain, add this object to specify the country and language you want to use for the results.

When you send requests to the API, we recommend that you add the following object:

  • context.user.uuid - an anonymous unique identifier of the visitor who visits the page. Add this object so that Sitecore Search can give your visitors relevant search results. If you do not pass the uuid, the search results you see are not tailored to that specific visitor. To learn how to generate a uuid, see Using a UUID to track site visitors.
Note

If your custom UUID does not follow the above format, ensure that your custom free-form ID is passed in context.user.user_id instead of context.user.uuid in your Search request.

Sample request

The following is a sample POST request to the Search and Recommendation API to get content for the key phrase cloud computing:

{
    "widget": {
        "items": [
            {
                "rfk_id": "rfkid_7",
                "entity": "content",
                "search": {
                    "content": {},
                    "query": {
                        "keyphrase": "cloud computing"
                    }
                }
            }
        ]
    },
    "context": {
        "locale": {
            "country": "us",
            "language": "en"
        }
        "user": {
      	    "uuid": "string" //or "user_id": "string" if your uuid does not follow the example here (https://doc.sitecore.com/search/en/developers/search-developer-guide/using-a-uuid-to-track-site-visitors.html)"uuid": "string"
        }
    }
}

Error Codes

The following table lists common errors that you might encounter when calling Sitecore Search APIs with an API key or access token.

Table 1. Error Codes

Status codeErrorPossible causeResolution
401 Unauthorized{"message":"Unauthorized"}The access token is missing, invalid, malformed, or expired. The Authorization header might be missing.Verify that you are sending a valid API key or bearer token. If the token has expired, generate a new access token.
------------
403 ForbiddenForbidden responseThe credentials do not have permission to access the requested API. This can occur when the required scope is not assigned to the API key or token. Some scope-related issues currently return a 401 response instead of 403.Verify that the API key includes the required scopes and generate a new access token if necessary.
404 Domain not founddomain_id_not_foundThe domain ID in the request does not exist or belongs to a different environment.Verify that you are using the correct domain ID and endpoint for the target environment.
429 Too Many RequestsRate limit exceededToo many requests were sent in a short period of time.Reduce request volume and retry according to your organization's retry policy.
5xx Server ErrorInternal server error or service unavailableA temporary service issue occurred.Retry the request according to your organization's retry policy. If the issue persists, contact Sitecore Support.
If you have suggestions for improving this article, let us know!