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 or A/B/n test, 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:

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 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 also uses profile and request data to return the visitor's city and the channel making the request.

    Note

    In this example, city and channel fall back to 'Unknown' when a value is unavailable.

    (function() {
    var city = profile && profile.contact && profile.contact.address
              ? profile.contact.address.city : 'Unknown';
    var channel = request && request.channel ? request.channel : '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:

If you have suggestions for improving this article, let us know!