Call a Ruleset from Your Software
Introduction
This guide explains how to call a ruleset via its API from external software such as a web application.
Rulesets are a means to an end, and the purpose of a ruleset is to implement a business rule and make its functionality generally available to other software. Because then you only have to maintain the rule, and the external software always automatically uses the latest implementation. To do so, you need to call a ruleset via its API from your external software.
Usually, ruleset APIs are called from JavaScript (JS). Therefore, Rulecube automatically provides a JS example for each ruleset of how to call the set. And thus, this guide starts with calling a ruleset from JS.
But because calling the API of a ruleset only requires a POST request with an API key for authorization and a JSON input body, you can do this from any modern programming language or development environment. And for your reference, we'll also provide a Python example.
Before you begin
This guide provides the basic steps for calling a ruleset; if you want a more comprehensive explanation of calling rulesets from external software, see the following tutorials:
Procedure
Call a ruleset API from JS
JS and JS frameworks like React and Vue are the backbone of many web applications. Therefore, Rulecube integrates closely with JS, such as, for example, with the ability to include JS in expressions. But also by being able to easily call ruleset APIs from JS:
Open your ruleset and click Docs. ↳ The Documentation page opens with the Summary tab activated.
Click the Examples tab. ↳ Rulecube shows a
fetch
example for calling the ruleset via its API.Copy the
fetch
example.Move over to your code editor and create a new JS or HTML file with a basic skeleton to be functional.
Add the following code to the file in the appropriate place—for example, to the
<script>
section in an HTML file:⇨ Replace
yourFunction
with your preferred function name.Paste the copied
fetch
example into the function.Call the function from HTML or JS. For instance, like in this HTML snippet:
For example, your complete HTML file could look something like this for the ruleset that was created with our tutorial about applying entities:
Replace <<Your rulecube URL>> with the correct base URL of your rulecube instance. E.g. https://app.rulecube.com
Save your file.
Test your JS or HTML file through the Inspect function of your browser.
Call a ruleset API from Python
Data Scientists often use Python to analyze data, and rulesets can play a big role in that by providing data and performing complex calculations. Furthermore, many data-driven websites are built with Python frameworks like Django and Flask. So, let's look at how to call a ruleset API from Python:
Step 1: Lookup your API key
Select <<Your tenant>> from the Rulecube menu bar. ↳ Your Profile page opens and shows a section for API keys like this:
Click the
icon behind the blurred key to copy it to the clipboard.
Open a text editor and paste the copied API key into the editor.
Step 2: Create the Python script that calls the ruleset API
If necessary, install the
requests
package with this command:pip install requests
.Open your code editor and create a new Python file.
Copy the following code into the file:
Replace
<<Your Rulecube url>>
with your Rulecube base url (e.g. https://app.rulecube.com)Replace
YOUR_RULESET
andYOUR-API_KEY
(from the text editor you copied the key into earlier) with your values.Move over to Rulecube and click Docs in your ruleset.
Click the Examples tab. ↳ Rulecube shows a JS example of calling your ruleset, including the JSON input, which also applies to calling your ruleset from Python.
Copy the complete
input
section from the JSON body.Return to your code editor and replace the empty
input
section with the copied version.Save your file.
Test your Python script from your code editor or a command prompt.
Additional resources
Tutorial: Call a Ruleset via Its API From Postman.
Tutorial: Use Your Ruleset from Your Software.
Last updated