---
title: "Make your first query"
slug: "make-your-first-query"
abstract: "Describes creating and dispatching a search request using Sitecore Search JS Data package request and widget objects."
---
# Make your first query

Before an application can dispatch a search request to Sitecore Search, it needs to be authenticated. The built-in `setup` method handles authorization success and failure. This walkthrough covers procedures from installation to handling the response of your first search query.

This walkthrough describes how to:

1. [Install the JS Data package](https://doc.sitecore.com/search/en/developers/search-developer-guide/integrating-using-search-js-data-package/make-your-first-query.html#UUID-440f946c-08f9-4e28-8507-d9702944d2e3_N1753954203978 "Install the JS Data package")
2. [Authenticate a session](https://doc.sitecore.com/search/en/developers/search-developer-guide/integrating-using-search-js-data-package/make-your-first-query.html#UUID-440f946c-08f9-4e28-8507-d9702944d2e3_section-idm4527382676712034134551292225 "Authenticate a session")
3. [Make a widget request object](https://doc.sitecore.com/search/en/developers/search-developer-guide/integrating-using-search-js-data-package/make-your-first-query.html#UUID-440f946c-08f9-4e28-8507-d9702944d2e3_section-idm4639433122916834134555124374 "Make a widget request object")
4. [Make a search request](https://doc.sitecore.com/search/en/developers/search-developer-guide/integrating-using-search-js-data-package/make-your-first-query.html#UUID-440f946c-08f9-4e28-8507-d9702944d2e3_N1753954245152 "Make a search request")
5. [Dispatch a search request](https://doc.sitecore.com/search/en/developers/search-developer-guide/integrating-using-search-js-data-package/make-your-first-query.html#UUID-440f946c-08f9-4e28-8507-d9702944d2e3_section-idm4560442156166434134565359414 "Dispatch a search request")

## Install the JS Data package

Before you can begin development, you need to install the JS Data package from NPM. You can do this either directly in your IDE or using the terminal.

To install Search JS Data package to your project:

- In your project, in the terminal, run the following:

    ```js
    npm install --save @sitecore-search/data
    ```

## Authenticate a session

All requests to Search must be authenticated. You can use the built-in `setup` method to authenticate a session.

To authenticate a session:

1. In a TypeScript file named `index.ts`, copy and paste the following code block:

    ```csharp
    import {
      setup,
    } from "@sitecore-search/data";

    setup({
      customerKey: '<YOUR_CUSTOMER_KEY>',
      apiKey:'<YOUR_API_KEY>',
    });
    ```

2. In the Sitecore Search user interface, in the main menu, click **Developer Resources**.
3. On the **API Access** tab, copy your *Customer key* and *API key* values.
4. In the `index.ts` file, in the `setup` function, replace `<YOUR_CUSTOMER_KEY>` and `<YOUR_API_KEY>` with the values you copied for the *Customer key* and the *API key*.

## Make a widget request object

Widget-specific parameters are sent to Search in `WidgetRequest` objects.

To make a widget request object:

1. In the `index.ts` file, to create widget request objects of widget and entity pairs, paste the following code block, then adjust the imports:

    ```csharp
    import {
      setup,
      WidgetRequest,
    } from "@sitecore-search/data";

    const searchResultsWidgetRequest = new WidgetRequest("rfkid_7");
    searchResultsWidgetRequest.setEntity("content");
    const previewSearchWidgetRequest = new WidgetRequest("rfkid_6");
    previewSearchWidgetRequest.setEntity("content");
    ```

2. In the `WidgetRequest` objects, pass query terms to the request in the form of content fields or attribute names, then adjust the imports:

    ```

    searchResultsWidgetRequest.addSearchContentField('attribute_name');
    searchResultsWidgetRequest.setSearchQueryKeyphrase("search_term");
    ```

3. In the `WidgetRequest` objects, to set pagination parameters, paste the following code block, then adjust the imports:

    ```

    searchResultsWidgetRequest.setSearchLimit(10); 
    searchResultsWidgetRequest.setSearchOffset(30);

    previewSearchWidgetRequest.setSearchLimit(10); 
    previewSearchWidgetRequest.setSearchOffset(30);
    ```

## Make a search request

The `Request` object containing context and widget information, is sent to Search. Its parameters include those that apply to all included widgets.

To make a search request and add widget request objects:

1. In the `index.ts` file, to create a request with locale as context, paste the following code block in the file, then adjust the imports:

    ```csharp
    import {
      setup,
      Request,
    } from "@sitecore-search/data";

    const request = new Request();
    request.setContextLocaleCountry("us");
    request.setContextLocaleLanguage("en");
    ```

2. In the `request` object, you can add `WidgetRequest` objects individually or in and array:
    - To add `WidgetRequest` objects individually paste the following code block, then adjust the imports:

        ```

        request.addWidgetItem(searchResultsWidgetRequest.toJson());
        request.addWidgetItem(previewSearchWidgetRequest.toJson());
        ```

    - Alternatively, to add `WidgetRequest` objects in an array, paste the following code block, then adjust the imports:

        ```text
        request.setWidgetItems( [ searchResultsWidgetRequest.toJson(), previewSearchWidgetRequest.toJson() ] );
        ```

## Dispatch a search request

The `DataProvider` is a singleton that orchestrates all communication between the UI and Search.

To dispatch a search request using the `DataProvider` singleton:

- In the `index.ts` file, paste the following code block, then adjust the imports:

    ```csharp
    import {
      setup,
      DataProvider,
      Request,
      WidgetRequest,
    } from "@sitecore-search/data";

    DataProvider.get(request.toJson())
      .then((response) => console.log(JSON.stringify(response, null, 4)))
      .catch((error) => console.log(JSON.stringify(error, null, 4)));
    ```