Disclaimer: This website requires Please enable JavaScript in your browser settings for the best experience.

HomeDev guideRecipesAPI Reference
Dev guideUser GuidesLegal TermsNuGetDev CommunityOptimizely AcademySubmit a ticketLog In
Dev guide

Synchronize content data

How to synchronize custom data sources with Optimizely Graph.

Optimizely Graph provides endpoints to let you manage custom data in your graph service. The custom data can be queried in the same way as content indexed automatically.

Synchronize content data

📘

Note

Refer to the Sync content data API reference for information.

The endpoint lets you send your content data to the Graph service:

https://cg.optimizely.com/api/content/v2/data?id=<content source id>

The id query parameter is the ID of your content source. It should be the same as the one used when defining the content types for the content source.

🚧

Important

Content Source IDs cannot be more than 4 characters, must be lowercase, and only contain a-z and 0-9.

Example

To index data, you must send data according to the NdJSON format. NdJSON is regular JSON separated by new lines. It is useful for streaming large amounts of data without having to parse everything as one large JSON string.

Each data item you want to index must be prepended with a separate action line:
{"index": {"_id": 1, "language_routing": "en" }}

This means you want to index (the action) the next line in the NdJSON stream. It should have an _id of 1 and is in English. Later, you can get the _id from the content using GraphQL.

🚧

Important

Each line of data goes in a pair with actions. If you omit the action line and have several consecutive data lines, the result is unpredicted, and not all data lines are added.

You will not get an error message when sending data like this. You will get an error message if the data cannot be parsed as JSON.

In the following example, information is posted for two specific products in English to a content source with id: com (short for "commerce"):

{    "index": {        "_id": 1,        "language_routing": "en"    }}
{    "Id": "1",    "Name": "Man shoes",    "Quantity": 10,    "size": 43,    "Color": "Black",    "Language": {        "DisplayName": "English",        "Name": "en"    },    "ContentType": [        "Catelog",        "Product"    ], "Status": "Published", "RolesWithReadAccess":"Everyone"}
{    "index": {        "_id": 2,        "language_routing": "en"    }}
{    "Id": "2",    "Name": "Women shoes",    "Quantity": 20,    "size": 38,    "Color": "Pink",    "Language": {        "DisplayName": "English",        "Name": "en"    },    "ContentType": [        "Catelog",        "Product"    ], "Status": "Published", "RolesWithReadAccess": "Everyone"}
curl --location 'https://cg.optimizely.com/api/content/v2/data?id=com' \
--header 'Content-Type: text/plain' \
--header 'Authorization: Basic V25KQmNtUlpSNTFYaDJuSmE1UWV5eEkwWWxOUVpFdWsxMDRWV1BYZkcydlcxZUluOkYwaWRhcDRPQ1ZOc0JSQTJmdVpCVDNtYXVoaVl1QWtRcDM5SHduazB2dW1XblpoZytjbmg4QnpHVU5VQlVIYTM=' \
--data '{    "index": {        "_id": 1,        "language_routing": "sv"    }}
{    "Id": "1",    "Name": "Man shoes",    "Quantity": 10,    "size": 43,    "Color": "Black",    "Language": {        "DisplayName": "English",        "Name": "en"    },    "ContentType": [        "Catelog",        "Product"    ],"Status": "Published","RolesWithReadAccess":"Everyone"}
{    "index": {        "_id": 2,        "language_routing": "sv"    }}
{    "Id": "2",    "Name": "Women shoes",    "Quantity": 20,    "size": 38,    "Color": "Pink",    "Language": {        "DisplayName": "English",        "Name": "en"    },    "ContentType": [        "Catelog",        "Product"    ],"Status": "Published","RolesWithReadAccess": "Everyone"}'

Authorization

The acceptable authorization is Basic and epi-hmac:

Basic:

  • Username – AppKey
  • Password – Secret

📘

Note

In Postman, you can get the Basic Authentication token by filling in the Username and Password with the AppKey and Secret respectively in the Authorization option. You will see your basic authentication token, which you can reuse, by hovering over the "Code snippet" on the right side. The value of the basic authentication token starts with Basic.

epi-hmac:

  • the AppKey and the secret are signed by the HMAC algorithm.

The following example is a POST request to update content items:

var crypto = require("crypto-js");
var sdk = require('postman-collection');
// This script uses 2 variables.
//
// EPTSKey
// EPTSSecret
//
var method = pm.request.method;
var key = pm.variables.get("EPTSKey");
var secret = CryptoJS.enc.Base64.parse(pm.variables.get("EPTSSecret"));
var target = new sdk.Url(request.url).getPathWithQuery();
var timestamp = (new Date()).getTime();
var nonce = Math.random().toString(36).substring(7);
var body = "";
if( pm.request.body )
{
    body = pm.request.body.raw;
}
var bodybase64 = crypto.MD5(body).toString(CryptoJS.enc.Base64);
var hmac = crypto.HmacSHA256(key + method + target + timestamp + nonce + bodybase64, secret);
var base64hmac = CryptoJS.enc.Base64.stringify(hmac);
var header = "epi-hmac " + key + ":" + timestamp +":" + nonce + ":" + base64hmac;
pm.request.headers.add(header, "Authorization")

Purge content data

📘

Note

For information, refer to the Purge content data API Reference.

The purge content data endpoint lets you delete your data from the content source.

Example

The following example deletes all content from a content source with the id cms for the en and sv languages.

curl --location --request DELETE 'https://cg.optimizely.com/api/content/v2/data?id=cms&languages=en&languages=sv' \
--header 'Authorization: Basic V25KQmNtUlpSNTFYaDJuSmE1UWV5eEkwWWxOUVpFdWsxMDRWV1BYZkcydlcxZUluOkYwaWRhcDRPQ1ZOc0JSQTJmdVpCVDNtYXVoaVl1QWtRcDM5SHduazB2dW1XblpoZytjbmg4QnpHVU5VQlVIYTM='
curl --location --request DELETE 'http://cg.optimizely.com/api/content/v2/data?id=cms&languages=en&languages=sv' \
--header 'Authorization: {{authHeader}}

📘

Note

authHeader = signed HMAC keys pair on the previous postman script example in the Authorization section.