Example API call - JavaScript
Below is the example code for creating a Market Test experiment using JavaScript. A POST is sent to the API to create a market test experiment. From there, the code checks the status of job until completion.
var launch = async function (domain, token, type, text, productType, description = '') {
const isOneStimulus = type => ['product-concept-test', 'ad-copy-test', 'product-description-test'].includes(type);
const stimuli = isOneStimulus(type) ? [text] : text.split('\n').filter(Boolean);
const headers = {
"Authorization": `Bearer ${token}`,
"Content-Type": "application/json",
"Accept": "application/json",
"X-Requested-With": "XMLHttpRequest",
};
const response = await fetch(`${domain}/api/ocmr/launch`, {
method: 'POST',
body: JSON.stringify({
type,
stimuli,
productType,
description,
}),
headers,
});
const hook = async function loadingHook(response) {
const resolver = async function (response, delay, resolve, reject) {
if (response.data) {
if (response.data.status === 'completed' || response.data.status === 'failed') {
return resolve(response);
}
}
// Try again if response is not completed
setTimeout(async () => {
const hookResponse = await (await fetch(
`${domain}/api/jobs/${response.data.id}/get`,
{headers}
)).json();
resolver(hookResponse, delay, resolve, reject);
}, delay);
};
return await new Promise((resolve, reject) => {
resolver(response, 2000, resolve, reject);
});
}
return await hook(await response.json())
}
let OCMR1 = launch("https://dev.api.conjointly.co", [token] , 'product-concept-test', 'A beautiful corroboree','event');
Where [token]
refers to your unique authentication token.