HTTP API
Rust and Go applications embed the engine and need no server. bree serve is for everything else: other languages, tools, or a quick look at the rules from curl. It evaluates through the same engine, with the same checks, limits and errors.
$ bree serve
bree 0.1.0 serving 1 rule from rules on http://127.0.0.1:7171| Option | Default | |
|---|---|---|
--rules (or BREE_RULES) |
rules |
the rules directory |
--addr (or BREE_ADDR) |
127.0.0.1:7171 |
the address to listen on |
--reload-ms |
1000 |
how often to look for changes to the rules; 0 loads them once |
It refuses to start if the rules do not type check or a test fails. While it runs, it picks up changes that pass, keeps the previous rules otherwise, and prints both to standard error (Reloading).
bree serve has no authentication. Bind it to localhost or a private network, or put it behind something that authenticates.
POST /v1/rules/{rule}
Evaluates a rule. The body is the rule's input. Nested rule names keep their slash: POST /v1/rules/billing/refunds.
$ curl -s -X POST http://127.0.0.1:7171/v1/rules/free_shipping -d '{"order_total_cents":8000,"country":"CA"}'
{"rule":"free_shipping","digest":"sha256:f7c9611f58d4f678cfc0ae1b74b66a9f69e51dc05aad015e1f54b18e5917855c","output":{"free":true}}A client with types generated from the rules can send the fingerprint it was built with in a Bree-Fingerprint header. If the loaded rule's types have changed since, the call fails with 409 rather than returning output the client cannot read:
$ curl -s -X POST http://127.0.0.1:7171/v1/rules/free_shipping -H 'Bree-Fingerprint: 0000000000000000' -d '{"order_total_cents":1,"country":"CA"}'
{"error":{"code":"types_mismatch","message":"the caller was built against the types of free_shipping with fingerprint 0000000000000000, but the loaded rule has ef6a9cd84918712d; regenerate and rebuild the caller"}}GET /v1/rules and GET /v1/rules/{rule}
The loaded rules, or one of them, with their digests and fingerprints:
$ curl -s http://127.0.0.1:7171/v1/rules
{"rules":[{"name":"free_shipping","digest":"sha256:f7c9611f58d4f678cfc0ae1b74b66a9f69e51dc05aad015e1f54b18e5917855c","fingerprint":"ef6a9cd84918712d"}]}GET /v1/health
$ curl -s http://127.0.0.1:7171/v1/health
{"status":"ok","bree":"0.1.0","last_reload_error":null}last_reload_error is the reason the latest change to the rules was refused, until a change is accepted. Alert on it: while it is set, the server is deciding with older rules than the ones on disk.
Errors
Errors have the same shape in every integration, with the status from the error table:
{"error":{"code":"invalid_input","message":"the input was rejected","violations":[{"field":"order_total_cents","message":"must not be negative"}]}}| Status | |
|---|---|
Body is not JSON (invalid_request) |
400 |
No such rule (rule_not_found), or no such route (not_found) |
404 |
Bree-Fingerprint does not match (types_mismatch) |
409 |
Input rejected (invalid_input) |
422 |
The rule failed (invalid_output, rule_error, execution_limit_exceeded, memory_limit_exceeded) |
500 |