from compass_api_sdk import CompassAPI
with CompassAPI(
api_key_auth="<YOUR_API_KEY_HERE>",
) as compass_api:
res = compass_api.perpetual_trading.perpetual_trading_set_leverage(owner="<value>", asset="<value>")
# Handle response
print(res)import { CompassApiSDK } from "@compass-labs/api-sdk";
const compassApiSDK = new CompassApiSDK({
apiKeyAuth: "<YOUR_API_KEY_HERE>",
});
async function run() {
const result = await compassApiSDK.perpetualTrading.perpetualTradingSetLeverage({
owner: "<value>",
asset: "<value>",
});
console.log(result);
}
run();curl --request POST \
--url https://api.compasslabs.ai/v2/perpetual_trading/set_leverage \
--header 'Content-Type: application/json' \
--header 'x-api-key: <api-key>' \
--data '
{
"owner": "<string>",
"asset": "<string>",
"leverage": 2
}
'const options = {
method: 'POST',
headers: {'x-api-key': '<api-key>', 'Content-Type': 'application/json'},
body: JSON.stringify({owner: '<string>', asset: '<string>', leverage: 2})
};
fetch('https://api.compasslabs.ai/v2/perpetual_trading/set_leverage', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://api.compasslabs.ai/v2/perpetual_trading/set_leverage",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
'owner' => '<string>',
'asset' => '<string>',
'leverage' => 2
]),
CURLOPT_HTTPHEADER => [
"Content-Type: application/json",
"x-api-key: <api-key>"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"strings"
"net/http"
"io"
)
func main() {
url := "https://api.compasslabs.ai/v2/perpetual_trading/set_leverage"
payload := strings.NewReader("{\n \"owner\": \"<string>\",\n \"asset\": \"<string>\",\n \"leverage\": 2\n}")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("x-api-key", "<api-key>")
req.Header.Add("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.post("https://api.compasslabs.ai/v2/perpetual_trading/set_leverage")
.header("x-api-key", "<api-key>")
.header("Content-Type", "application/json")
.body("{\n \"owner\": \"<string>\",\n \"asset\": \"<string>\",\n \"leverage\": 2\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://api.compasslabs.ai/v2/perpetual_trading/set_leverage")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["x-api-key"] = '<api-key>'
request["Content-Type"] = 'application/json'
request.body = "{\n \"owner\": \"<string>\",\n \"asset\": \"<string>\",\n \"leverage\": 2\n}"
response = http.request(request)
puts response.read_body{
"action": {
"asset": 12,
"isCross": true,
"leverage": 5,
"type": "updateLeverage"
},
"leverage_ok": false,
"nonce": 1747010983250,
"typed_data": {
"domain": {
"chainId": 1337,
"name": "Exchange",
"verifyingContract": "0x0000000000000000000000000000000000000000",
"version": "1"
},
"message": {
"connectionId": "0xc4e1f04d6b1bf2f2f7a6c0e8a5f3b1d2c9e0a4b7d1c3e5f7a9b1d3e5f7a9e9a2",
"source": "a"
},
"primaryType": "Agent",
"types": {
"Agent": [
{
"name": "source",
"type": "string"
},
{
"name": "connectionId",
"type": "bytes32"
}
]
}
}
}{
"detail": [
{
"loc": [
"<string>"
],
"msg": "<string>",
"type": "<string>"
}
]
}Set leverage (defaults to market maximum)
Check leverage and prepare an updateLeverage action to the requested value.
If leverage is omitted, targets the asset’s maximum leverage for that
market. If the asset is already at the requested leverage, returns
leverage_ok=true with null typed_data — no signing needed. Otherwise,
returns EIP-712 typed data for the user to sign. After signing, submit the
signature via the /execute endpoint.
from compass_api_sdk import CompassAPI
with CompassAPI(
api_key_auth="<YOUR_API_KEY_HERE>",
) as compass_api:
res = compass_api.perpetual_trading.perpetual_trading_set_leverage(owner="<value>", asset="<value>")
# Handle response
print(res)import { CompassApiSDK } from "@compass-labs/api-sdk";
const compassApiSDK = new CompassApiSDK({
apiKeyAuth: "<YOUR_API_KEY_HERE>",
});
async function run() {
const result = await compassApiSDK.perpetualTrading.perpetualTradingSetLeverage({
owner: "<value>",
asset: "<value>",
});
console.log(result);
}
run();curl --request POST \
--url https://api.compasslabs.ai/v2/perpetual_trading/set_leverage \
--header 'Content-Type: application/json' \
--header 'x-api-key: <api-key>' \
--data '
{
"owner": "<string>",
"asset": "<string>",
"leverage": 2
}
'const options = {
method: 'POST',
headers: {'x-api-key': '<api-key>', 'Content-Type': 'application/json'},
body: JSON.stringify({owner: '<string>', asset: '<string>', leverage: 2})
};
fetch('https://api.compasslabs.ai/v2/perpetual_trading/set_leverage', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://api.compasslabs.ai/v2/perpetual_trading/set_leverage",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
'owner' => '<string>',
'asset' => '<string>',
'leverage' => 2
]),
CURLOPT_HTTPHEADER => [
"Content-Type: application/json",
"x-api-key: <api-key>"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"strings"
"net/http"
"io"
)
func main() {
url := "https://api.compasslabs.ai/v2/perpetual_trading/set_leverage"
payload := strings.NewReader("{\n \"owner\": \"<string>\",\n \"asset\": \"<string>\",\n \"leverage\": 2\n}")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("x-api-key", "<api-key>")
req.Header.Add("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.post("https://api.compasslabs.ai/v2/perpetual_trading/set_leverage")
.header("x-api-key", "<api-key>")
.header("Content-Type", "application/json")
.body("{\n \"owner\": \"<string>\",\n \"asset\": \"<string>\",\n \"leverage\": 2\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://api.compasslabs.ai/v2/perpetual_trading/set_leverage")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["x-api-key"] = '<api-key>'
request["Content-Type"] = 'application/json'
request.body = "{\n \"owner\": \"<string>\",\n \"asset\": \"<string>\",\n \"leverage\": 2\n}"
response = http.request(request)
puts response.read_body{
"action": {
"asset": 12,
"isCross": true,
"leverage": 5,
"type": "updateLeverage"
},
"leverage_ok": false,
"nonce": 1747010983250,
"typed_data": {
"domain": {
"chainId": 1337,
"name": "Exchange",
"verifyingContract": "0x0000000000000000000000000000000000000000",
"version": "1"
},
"message": {
"connectionId": "0xc4e1f04d6b1bf2f2f7a6c0e8a5f3b1d2c9e0a4b7d1c3e5f7a9b1d3e5f7a9e9a2",
"source": "a"
},
"primaryType": "Agent",
"types": {
"Agent": [
{
"name": "source",
"type": "string"
},
{
"name": "connectionId",
"type": "bytes32"
}
]
}
}
}{
"detail": [
{
"loc": [
"<string>"
],
"msg": "<string>",
"type": "<string>"
}
]
}Body
Request to set leverage on a perpetual trading asset.
Omit leverage (or pass null) to target the asset's maximum leverage.
User's EOA address
Asset ticker (e.g. 'AAPL', 'CL')
Target leverage as a whole-number multiplier. Omit (or pass null) to use the asset's maximum leverage. Must be between 1 and the asset's max leverage, which is available from the /opportunities endpoint.
x >= 1Response
Successful Response
Returned by the set_leverage endpoint.
If the asset is already at the requested leverage, typed_data/action/nonce are null. If not, they contain the EIP-712 payload the user must sign.
True if leverage already matches the requested value, false if an update is needed
EIP-712 typed data for wallet signing, or null if no change is needed
Raw Hyperliquid action (passed back to the execute endpoint), or null
Timestamp-based nonce, or null if no action needed
Was this page helpful?