Evaluation API
Evaluation API is used to connect directly to managed ZEN Engine over REST API.
Procedure
info
Make sure your decision is published before proceeding.
- Grab the evaluation URL by right-clicking on the document and pressing Copy URL
- Generate a project security token
- Navigate to Project -> Settings -> Security
- Press Generate token
- Fill out the form and confirm
- Copy the access token
- Execute decision by sending
POST
request with security token and payload to endpoint of the decision.
Examples
You can use one of the following code snippets to run the rule engine using API in your application.
- TypeScript
- Go
- Rust
- cURL
// TODO: Define EvaluationResponse interface
const evaluate = async (): EvaluationResponse => {
const response = await fetch('<Your URL>', {
method: 'POST',
headers: {
'X-Access-Token': '<Your Access Token>',
'Content-Type': 'application/json',
},
body: JSON.stringify({
context: {
customer: { country: 'US' },
cart: { totals: 100 },
product: { weight: 15 },
},
}),
});
const { data } = await response.json();
return data as EvaluationResponse;
};
// TODO: Define EvaluationRequest and EvaluationResponse structs
func evaluate() (*EvaluationResponse, error) {
requestData := EvaluationRequest {
Context: EvaluationContext {
Customer: Customer { Country: "US" },
Cart: Cart { Totals: 100 },
Product: Product { Weight: 15 },
}
}
requestJson, err := json.Marshal(requestData)
if err != nil {
return nil, err
}
request, err := http.Post("POST", "<Your URL>", bytes.NewBuffer(requestJson))
if err != nil {
return nil, err
}
request.Header.Add("X-Access-Token", "<Your Access Token>")
request.Header.Add("Content-Type", "application/json")
client := &http.Client{}
response, err := client.Do(request)
if err != nil {
return nil, err
}
defer response.Body.Close()
var evaluationResponse EvaluationResponse
err = json.Unmarshal(response.Body, &evaluationResponse)
if err != nil {
return nil, err
}
}
// TODO: Define EvaluationRequest and EvaluationResponse structs using serde_json
// and derive Deserialize trait
async fn evaluate(): Result<EvaluationResponse, Error> {
let request_data = EvaluationRequest {
context: EvaluationContext {
customer: Customer { country: "US".to_string() },
cart: Cart { totals: 100. },
product: Product { weight: 15. },
},
};
let request_json = serde_json::to_string(&request_data)?;
let client = reqwest::Client::new()
.post("<Your URL>")
.body(request_json)
.header("Content-Type", "application/json")
.header("X-Access-Token", "<Your Access Token>");
let response = client.send().await?;
Ok(response.json::<EvaluationResponse>().await?)
}
curl <Your URL>
-H "X-Access-Token: <Your Access Token>"
-H "Content-Type: application/json"
-d '{ "context": { "customer": { "country": "US" }, "cart": { "totals": 100 }, "product": { "weight": 15 } } }'