---
title: "Configure the response of a headless experience"
slug: "configure-the-api-response-of-a-headless-experience"
abstract: "Define the structured response that SitecoreAI returns to your front end for a headless experience."
---
# Configure the response of a headless experience

> [!important]
> Feature availability is part of a phased rollout. Your organization may not see this functionality yet. It will become available when your environment is included in the rollout.

After you create a headless [personalization](https://doc.sitecore.com/sai/en/users/sitecoreai/headless-personalization-and-abn-testing/create-a-headless-personalization.html) or [A/B/n test](https://doc.sitecore.com/sai/en/users/sitecoreai/headless-personalization-and-abn-testing/create-a-headless-abn-test.html), define the data your application should receive from SitecoreAI.

You configure the response as JavaScript that returns an object. SitecoreAI sends that object to your application, which uses the returned values to render the experience.

For a personalization, configure one response for the experience. For an A/B/n test, configure a response for each variant.

## Response guidelines

Keep the following in mind when you define the response:

- Use **`profile`** to access visitor profile data, such as contact or address information.
- Use **`request`** to access request context, such as `channel` and `pointOfSale`.
- Handle missing profile values because some visitors might be anonymous.
- Keep responses small and data-oriented. Return content values or identifiers rather than markup or styling.
- For an A/B/n test, keep the same response structure across variants and change only the values you want to test.

You can also use **template variables** for values that you want to change without editing the response code.

You can configure the response in either of the following ways:

- use [Code Assistant](https://doc.sitecore.com/sai/en/users/sitecoreai/headless-personalization-and-abn-testing/configure-the-api-response-of-a-headless-experience.html#configure-the-api-response-with-code-assistant)
- [write JavaScript](https://doc.sitecore.com/sai/en/users/sitecoreai/headless-personalization-and-abn-testing/configure-the-api-response-of-a-headless-experience.html#configure-the-api-response-manually) manually

## Configure the API response with Code Assistant

Use the Code Assistant to generate the API response from a natural-language prompt.

To configure the API response with the Code Assistant:

1. On the **Configuration** page, in the **API response** section, click **Configure**.

2. In the **Code Assistant** pane, do one of the following:
   - Enter a prompt describing the response you want to create.
   - Select one of the suggested prompts.

   For example:

   - *Return `offer_code` as `SPEAKERS10` if the visitor has viewed the "Speakers" page.*
   - *Return `offer_code` as `BIRTHDAY20` if the visitor's birthday is within three days of today.*

   The Code Assistant generates JavaScript based on your prompt and any code already in the editor.

3. To preview the generated code, click **View**.

4. In the dialog, do one of the following:
   - Click **Replace code** to insert the generated code into the editor.
   - Click **Copy code** to copy it to your clipboard.

   > [!caution]
   > AI-generated suggestions can contain inaccuracies and can vary between runs. Review the generated code before using it.

5. Optionally, edit the generated code directly in the editor or click **Undo** :mdiArrowULeftTop: to revert changes.

6. Click **Save and close**.

The API response appears in the **API response** section of the **Configuration** page.

## Configure the API response manually

If you are familiar with JavaScript, you can define the API response directly in the code editor.

To configure the API response manually:

1. On the **Configuration** page, in the **API response** section, click **Configure**.

2. In the code editor, enter or edit the JavaScript that defines the response. The object returned by the code becomes the data your application receives.

    For example, the following returns a welcome message, discount, and feature list. It reads the city from the request when the application sends one, and otherwise from the visitor profile. `channel` is the touchpoint the application sends, such as `WEB` or `MOBILE_APP`.


    > [!note]
    > City and channel fall back to `Unknown` when a value is unavailable. A city on `profile.contact.address` is not present for every visitor. Check session geolocation and `homeCity` before using `Unknown`.

  
    ```js
    (function () {
      var city = "Unknown";
      if (request && request.params && request.params.geo && request.params.geo.city) {
        city = request.params.geo.city;
      } else if (profile && profile.contact && profile.contact.address && profile.contact.address.city) {
        city = profile.contact.address.city;
      } else if (profile && profile.sessions && profile.sessions.length && profile.sessions[0].geolocation && profile.sessions[0].geolocation.city) {
        city = profile.sessions[0].geolocation.city;
      } else if (profile && profile.traits && profile.traits.profile && profile.traits.profile.homeCity) {
        city = profile.traits.profile.homeCity;
      }
      var channel = request && request.channel ? String(request.channel).toUpperCase() : "Unknown";

      return {
        message: "Standard Welcome",
        discount: 0,
        features: ["basic"],
        city: city,
        channel: channel
      };
    })();
    ```

3. Click **Save and close**.

The API response appears in the **API response** section of the **Configuration** page.



## Next steps

After you configure the API response, you can:

- Optionally, [create a headless A/B/n test](https://doc.sitecore.com/sai/en/users/sitecoreai/headless-personalization-and-abn-testing/create-a-headless-abn-test.html) by adding one or more test variants.
- Optionally, [add a condition](https://doc.sitecore.com/sai/en/users/sitecoreai/headless-personalization-and-abn-testing/add-a-condition-to-a-headless-experience.html) to control who is eligible.
- [Add a goal](https://doc.sitecore.com/sai/en/users/sitecoreai/headless-personalization-and-abn-testing/add-a-goal-to-the-headless-optimization.md) to measure performance.
- Use **Preview API** to [test the API response](https://doc.sitecore.com/sai/en/users/sitecoreai/headless-personalization-and-abn-testing/test-the-api-response-of-a-headless-experience).
