Using APIs

If you are not using CustomFit.ai SDK then you can make use of our APIs to integrate our services. To work with APIs you need to get Authorization token from CustomFit.ai dashboard. To know more click here. Follow the below two steps to start working with CustomFit.ai APIs :

Invoke user configs API when you load the page for the first time. The API will get the list of all config variation values by evaluating the rules based on the user object passed.

post
User configs

https://api.customfit.ai/v1/users/configs
Request
Response
Request
Headers
Authentication
required
string
Bearer {{Authentication token}}
Body Parameters
track
optional
boolean
Set the value as true to enable config insights
config_ids
optional
array
List of config ids(config key). If not specified then response will have variations for all the cofigs
user
required
object
If the user is not present then the user gets created. The user object is used for config evaluation
Response
200: OK
All config variation values along with the varation data type
{
"seasonal-promotions": {
"config_id": "430aaf80-6b45-11e9-8358-a3d3a758c284",
"config_customer_id": "seasonal-promotions",
"config_name": "Seasonal Promotions",
"variation": 100,
"variation_name": "test1",
"variation_data_type": "NUMBER",
"experience_behaviour_response": {
"experience_id": "bc7c1730-77d1-11e9-be6e-9f9a668685d7",
"behaviour": "b1"
},
"version": 580
},
"gold-member": {
"config_id": "09d9b990-6b45-11e9-af31-453c10819204",
"config_customer_id": "gold-member",
"config_name": "Gold Member",
"variation": false,
"variation_name": "False",
"variation_data_type": "BOOLEAN",
"experience_behaviour_response": {
"experience_id": "bc7c1730-77d1-11e9-be6e-9f9a668685d7",
"behaviour": "b1"
},
"version": 20
}
400: Bad Request
User customer id is not present
{
"api_correlation_id": "5a726ae3b3033bcb",
"error_code": "014014001E",
"error_message": "User customer_id is mandatory",
"timestamp": "30-07-2019 06:35:50"
}

Sample request body

{
"config_ids": [
"config_one",
"config_two"
],
"user": {
"country": "USA",
"country_code": 1,
"default_location": {
"lat": 0,
"lon": 0
},
"dob": "1992-01-14",
"email": "dev@backbuckle.io",
"first_name": "Jhon",
"gender": "MALE",
"last_name": "Wright",
"phone_number": 2025550192,
"properties": {
"property_key1": "value",
"property_key2": "value"
},
"tags": {
"tag_key1": "value",
"tag_key2": "value"
},
"time_zone": "America/New_York",
"user_customer_id": "2010C6PS627G",
"user_name": "JhonWright001"
}
}

Code Snippet

JavaScript
Java
NodeJs Request
Nodejs Native
Go
JavaScript
var settings = {
"async": true,
"crossDomain": true,
"url": "https://api.customfit.ai/v1/users/configs",
"method": "POST",
"headers": {
"Authorization": "Bearer {{client_key}}",
"Content-Type": "application/json",
},
"processData": false,
"data": "{\n\"user\": {\n\"user_customer_id\": \"user_customer_id_1\"\n},\n\"track\": true\n}"
}
$.ajax(settings).done(function (response) {
console.log(response);
});
Java
OkHttpClient client = new OkHttpClient();
MediaType mediaType = MediaType.parse("application/json");
RequestBody body = RequestBody.create(mediaType, "{\n\"user\": {\n\"user_customer_id\": \"user_customer_id_1\"\n },\n \"track\": true\n}");
Request request = new Request.Builder()
.url("https://api.customfit.ai/v1/users/configs")
.post(body)
.addHeader("Authorization", "Bearer {{client_key}}")
.addHeader("Content-Type", "application/json")
.build();
Response response = client.newCall(request).execute();
NodeJs Request
var request = require("request");
var options = { method: 'POST',
url: 'https://api.customfit.ai/v1/users/configs',
headers:
{ 'Content-Type': 'application/json',
Authorization: 'Bearer {{client_key}}' },
body:
{ user: { user_customer_id: 'user_customer_id_1' },
track: true },
json: true };
request(options, function (error, response, body) {
if (error) throw new Error(error);
console.log(body);
});
Nodejs Native
var http = require("https");
var options = {
"method": "POST",
"hostname": [
"api",
"customfit",
"ai"
],
"path": [
"v1",
"users",
"configs"
],
"headers": {
"Authorization": "Bearer {{client_key}}",
"Content-Type": "application/json"
}
};
var req = http.request(options, function (res) {
var chunks = [];
res.on("data", function (chunk) {
chunks.push(chunk);
});
res.on("end", function () {
var body = Buffer.concat(chunks);
console.log(body.toString());
});
});
req.write(JSON.stringify({ user: { user_customer_id: 'user_customer_id_1' },
track: true }));
req.end();
Go
package main
import (
"fmt"
"strings"
"net/http"
"io/ioutil"
)
func main() {
url := "https://api.customfit.ai/v1/users/configs"
payload := strings.NewReader("{\n\"user\": {\n\"user_customer_id\": \"user_customer_id_1\"\n},\n\"track\": true\n}")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("Authorization", "Bearer {{client_key}}")
req.Header.Add("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := ioutil.ReadAll(res.Body)
fmt.Println(res)
fmt.Println(string(body))
}

Invoke event track API to track the events triggerred accross various experiences and get a valuable insights about how your experience is performing based on different behaviours.

post
Event track

https://api.customfit.ai/v1/events/track
Request
Response
Request
Body Parameters
user
required
object
User object with user customer id
events
optional
string
List of events with event customer id and event timestamp
Response
202: Accepted
Accepted the request
401: Unauthorized
No user object
{
"api_correlation_id": "71164c4f632f7eb2",
"error_code": "014001030E",
"error_message": "Invalid user request. Error: No user object",
"timestamp": "30-07-2019 08:28:02"
}

Sample request body

{
"user": {
"user_customer_id": "user_001"
},
"events": [
{
"event_customer_id": "add-to-cart",
"event_timestamp": "2019-05-18 16:38:48.326+0530"
}
]
}

Code Snippet

JavaScript
Java
NodeJs Request
NodeJs Native
Go
JavaScript
var settings = {
"async": true,
"crossDomain": true,
"url": "https://api.customfit.ai/v1/events/track",
"method": "POST",
"headers": {
"Authorization": "Bearer {{client_key}}",
"Content-Type": "application/json"
},
"processData": false,
"data": "{\n\"user\": {\n\"user_customer_id\": \"user_customer_id_1\"\n},\n\"events\": [\n{\n\"event_customer_id\": \"view-event\",\n\"event_timestamp\": \"2019-07-30 16:38:48.326+0530\"\n}\n]\n}"
}
$.ajax(settings).done(function (response) {
console.log(response);
});
Java
OkHttpClient client = new OkHttpClient();
MediaType mediaType = MediaType.parse("application/json");
RequestBody body = RequestBody.create(mediaType, "{\n\"user\": {\n\"user_customer_id\": \"user_customer_id_1\"\n},\n\"events\": [\n{\n\"event_customer_id\": \"view-event\",\n\"event_timestamp\": \"2019-07-30 16:38:48.326+0530\"\n}\n]\n}");
Request request = new Request.Builder()
.url("https://api.customfit.ai/v1/events/track")
.post(body)
.addHeader("Authorization", "Bearer {{client_key}}")
.addHeader("Content-Type", "application/json")
.build();
Response response = client.newCall(request).execute();
NodeJs Request
var request = require("request");
var options = { method: 'POST',
url: 'https://api.customfit.ai/v1/events/track',
headers:
{ 'Content-Type': 'application/json',
Authorization: 'Bearer {{client_key}}' },
body:
{ user: { user_customer_id: 'user_customer_id_1' },
events:
[ { event_customer_id: 'view-event',
event_timestamp: '2019-07-30 16:38:48.326+0530' } ] },
json: true };
request(options, function (error, response, body) {
if (error) throw new Error(error);
console.log(body);
});
NodeJs Native
var http = require("https");
var options = {
"method": "POST",
"hostname": [
"api",
"customfit",
"ai"
],
"path": [
"v1",
"events",
"track"
],
"headers": {
"Authorization": "Bearer {{client_key}}",
"Content-Type": "application/json"
}
};
var req = http.request(options, function (res) {
var chunks = [];
res.on("data", function (chunk) {
chunks.push(chunk);
});
res.on("end", function () {
var body = Buffer.concat(chunks);
console.log(body.toString());
});
});
req.write(JSON.stringify({ user: { user_customer_id: 'user_customer_id_1' },
events:
[ { event_customer_id: 'view-event',
event_timestamp: '2019-07-30 16:38:48.326+0530' } ] }));
req.end();
Go
package main
import (
"fmt"
"strings"
"net/http"
"io/ioutil"
)
func main() {
url := "https://api.customfit.ai/v1/events/track"
payload := strings.NewReader("{\n\"user\": {\n\"user_customer_id\": \"user_customer_id_1\"\n},\n\"events\": [\n{\n\"event_customer_id\": \"view-event\",\n\"event_timestamp\": \"2019-07-30 16:38:48.326+0530\"\n}\n]\n}")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("Authorization", "Bearer {{client_token}}")
req.Header.Add("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := ioutil.ReadAll(res.Body)
fmt.Println(res)
fmt.Println(string(body))
}