Examples - Using the API
Below are examples of how to use the Coinrule B2B API to create a rule, using different tools and languages. The example corresponds to creating a daily DCA (dollar-cost averaging) rule: Buy $50 of ETH and $30 of AAVE every day using USDC, starting on May 16, 2025, and repeating 100 times. The rule uses market orders with a maximum gas fee of $0.2 and max slippage of 0.5%.
HTTPS Request
This is the raw HTTP format of the request. It includes the necessary headers and the JSON body. The response will be a JSON confirming rule creation or detailing any errors.
POST /v3/rules HTTP/1.1
Host: api.coinrule.com
X-API-Key: <YOUR_BUSINESS_API_KEY>
X-User-Public-Key: <USER_PUBLIC_KEY>
Content-Type: application/json
{
"name": "Daily DCA for ETH and AAVE",
"trigger": {
"type": "time",
"schedule": {
"start": "2025-07-16T11:08:00Z",
"interval": { "unit": "days", "value": 1 },
"repeatCount": 100
}
},
"data": {
"sequences": [
{
"t": "ACTION",
"d": {
"do": "trade",
"ta": {
"at": "buy",
"b": ["ETH"],
"q": "USDC",
"ot": "market",
"ort": "base",
"v": { "v": "50", "iv": "50", "s": "USD" },
"ls": 0,
"maxgf": "63244506424060",
"maxgfinbasecurrency": "0.2",
"maxpi": "",
"maxsl": "0.5",
"sr": null
}
}
},
{
"t": "ACTION",
"d": {
"do": "trade",
"ta": {
"at": "buy",
"b": ["AAVE"],
"q": "USDC",
"ot": "market",
"ort": "base",
"v": { "v": "30", "iv": "30", "s": "USD" },
"ls": 0,
"maxgf": "63244506424060",
"maxgfinbasecurrency": "0.2",
"maxpi": "",
"maxsl": "0.5",
"sr": null
}
}
}
]
},
"timestamp": "2025-05-16T10:11:35.214Z"
}
cURL (Command Line)
You can use cURL to send the same request from the command line:
curl -X POST "https://api.coinrule.com/v3/rules" \
-H "X-API-Key: <YOUR_BUSINESS_API_KEY>" \
-H "X-User-Public-Key: <USER_PUBLIC_KEY>" \
-H "Content-Type: application/json" \
-d '{
"name": "Daily DCA for ETH and AAVE",
"trigger": {
"type": "time",
"schedule": {
"start": "2025-07-16T11:08:00Z",
"interval": { "unit": "days", "value": 1 },
"repeatCount": 100
}
},
"data": {
"sequences": [
{
"t": "ACTION",
"d": {
"do": "trade",
"ta": {
"at": "buy",
"b": ["ETH"],
"q": "USDC",
"ot": "market",
"ort": "base",
"v": { "v": "50", "iv": "50", "s": "USD" },
"ls": 0,
"maxgf": "63244506424060",
"maxgfinbasecurrency": "0.2",
"maxpi": "",
"maxsl": "0.5",
"sr": null
}
}
},
{
"t": "ACTION",
"d": {
"do": "trade",
"ta": {
"at": "buy",
"b": ["AAVE"],
"q": "USDC",
"ot": "market",
"ort": "base",
"v": { "v": "30", "iv": "30", "s": "USD" },
"ls": 0,
"maxgf": "63244506424060",
"maxgfinbasecurrency": "0.2",
"maxpi": "",
"maxsl": "0.5",
"sr": null
}
}
}
]
},
"timestamp": "2025-05-16T10:11:35.214Z"
}'
This cURL command will send the POST request with the appropriate headers and JSON payload. On success, you should receive a JSON response with a 201 Created status. For example:
{
"ruleId": "60d5adf568a3e60012345678",
"status": "active",
"message": "Rule created successfully."
}If there's an error (e.g., a missing field or invalid API key), you will receive an error response, for example a 400 Bad Request:
{
"error": "Bad Request",
"message": "Validation failed: 'q' (quote asset) field is required."
}Python (requests library)
Below is an example of creating the same rule using Python and the requests library:
import requests
import json
url = "https://api.coinrule.com/v3/rules"
headers = {
"X-API-Key": "<YOUR_BUSINESS_API_KEY>",
"X-User-Public-Key": "<USER_PUBLIC_KEY>",
"Content-Type": "application/json"
}
payload = {
"name": "Daily DCA for ETH and AAVE",
"trigger": {
"type": "time",
"schedule": {
"start": "2025-07-16T11:08:00Z",
"interval": { "unit": "days", "value": 1 },
"repeatCount": 100
}
},
"data": {
"sequences": [
{
"t": "ACTION",
"d": {
"do": "trade",
"ta": {
"at": "buy",
"b": ["ETH"],
"q": "USDC",
"ot": "market",
"ort": "base",
"v": { "v": "50", "iv": "50", "s": "USD" },
"ls": 0,
"maxgf": "63244506424060",
"maxgfinbasecurrency": "0.2",
"maxpi": "",
"maxsl": "0.5",
"sr": None
}
}
},
{
"t": "ACTION",
"d": {
"do": "trade",
"ta": {
"at": "buy",
"b": ["AAVE"],
"q": "USDC",
"ot": "market",
"ort": "base",
"v": { "v": "30", "iv": "30", "s": "USD" },
"ls": 0,
"maxgf": "63244506424060",
"maxgfinbasecurrency": "0.2",
"maxpi": "",
"maxsl": "0.5",
"sr": None
}
}
}
]
},
"timestamp": "2025-05-16T10:11:35.214Z"
}
In this Python example, we construct the payload dictionary with the rule details and send it via requests.post. The headers include our API keys. The response's status code and JSON content are then printed. If successful, response.status_code should be 201, and response.json() will contain the created rule's ID and status message (or an error message if the request was not successful).
response = requests.post(url, headers=headers, json=payload)JavaScript (fetch API in Node or Browser)
Here's how you might send the request using JavaScript (for example, in a Node.js environment or in the browser, if CORS allows):
const fetch = require('node-fetch'); // if in Node.js; in browser, fetch is globally available
const url = "https://api.coinrule.com/v3/rules";
const headers = {
"X-API-Key": "<YOUR_BUSINESS_API_KEY>",
"X-User-Public-Key": "<USER_PUBLIC_KEY>",
"Content-Type": "application/json"
};
const body = {
name: "Daily DCA for ETH and AAVE",
trigger: {
type: "time",
schedule: {
start: "2025-07-16T11:08:00Z",
interval: { unit: "days", value: 1 },
repeatCount: 100
}
},
data: {
sequences: [
{
t: "ACTION",
d: {
do: "trade",
ta: {
at: "buy",
b: ["ETH"],
q: "USDC",
ot: "market",
ort: "base",
v: { v: "50", iv: "50", s: "USD" },
ls: 0,
maxgf: "63244506424060",
maxgfinbasecurrency: "0.2",
maxpi: "",
maxsl: "0.5",
sr: null
}
}
},
{
t: "ACTION",
d: {
do: "trade",
ta: {
at: "buy",
b: ["AAVE"],
q: "USDC",
ot: "market",
ort: "base",
v: { v: "30", iv: "30", s: "USD" },
ls: 0,
maxgf: "63244506424060",
maxgfinbasecurrency: "0.2",
maxpi: "",
maxsl: "0.5",
sr: null
}
}
}
]
},
timestamp: "2025-05-16T10:11:35.214Z"
};
fetch(url, {
method: "POST",
headers: headers,
body: JSON.stringify(body)
})
.then(res => {
console.log("Status:", res.status);
return res.json();
})
.then(data => {
console.log("Response:", data);
})
.catch(err => {
console.error("Error:", err);
});
This JavaScript example uses fetch to POST the rule. We stringify the body object and include the required headers. On success, the response JSON is logged (which should contain the ruleId and a success message). On error, the error is caught and logged.
Last updated