Request Context

The request context enables you to provide additional information about a request that is being made to the Content Delivery API.

Supported Context Attributes

The attributes which can be provided in the request context are as follows:

Attribute Name Auth Required? Description
audienceAttributes No Accepts an object containing the values of any of your configured audience attributes.
preview Yes Accepts an object containing the values of any of your configured audience attributes.

audienceAttributes

The audienceAttributes context attribute is used for setting the values for any of your configured audience attributes.

These values are then used to calculate which audiences the request falls into based on the conditions which have been configured within each audience.

It is important to note that audience attributes are created with an explicit type.

As a result, in order for your audiences to be evaluated correctly, you must ensure that all provided attribute values are of the same type as the attributes themselves.

As an example, the below code snippet shows what a request context object may look like for a returning customer who is 25 years old and is also located in Great Britain.

{
  audienceAttributes: {
    returningCustomer: true,
    age: 25,
    location: 'GB'
  }
}

preview

Use of this attribute requires that your request be authenticated with an API Key which has permission to retrieve preview content. For more information on sending authenticated requests, please see the Authentication documentation.

The preview context attribute enables you to control the date/time that you would like to retrieve content for and also whether or not you would like draft variations to be included in the response.

This attribute accepts the following properties:

Name Type Description
includeDraftVariations Boolean Whether or not the response should include unpublished variations. (Default: false)
timestamp Integer A UNIX timestamp which specifies the date/time that this request should be made in the context of. (Default: Current timestamp)

The snippet below shows and example of what a request context could look like when attempting to preview content for 1st December 2020 at 1pm, including any variations which are yet to be published.

{
  preview: {
    includeDraftVariations: true,
    timestamp: 1606827600
  }
}

Using the Request Context

When sending a request to the Content Delivery API, the request context can be provided by setting the x-lexascms-context header.

The value of this header should be an object which has been JSON encoded and then Base64 encoded.

The code snippet below shows an example of sending a request which includes a request context to the Content Delivery API:

const axios = require('axios');
const base64 = require('base-64');

// Define a JSON encoded LexasCMS Request Context
let lexascmsRequestContext = JSON.stringify({
  audienceAttributes: {
    returningCustomer: true,
    age: 25,
    location: 'GB'
  }
});

// Base64 encode LexasCMS Request Context
lexascmsRequestContext = base64.encode(lexascmsRequestContext);

const requestOptions = {
  baseURL: 'https://{SPACE_ID}.spaces.lexascms.com/delivery/jsonapi',
  headers: {
    // ...
    'x-lexascms-context': lexascmsRequestContext
  }
};

axios.get('/book', requestOptions).then(response => {
  // ...
});
Integrates with your favourite tools and frameworks

© 2022 Status200 Ltd.