Set position config
Set the margin type (cross or isolated) and leverage of one position.
On this page
Write a small program that calls `position.set_position_config` on Grvt {environment} {using}.
Target environment: {environment} (chain ID {chainId}). Recorded signing cases keep their original environment.
{executionGuidance}
Read this first: {origin}/bundle/reference/rpc/set_position_config.md (~73 KB, plain Markdown: this page plus the AGENTS.md rules, Authentication and sessions, Signing, SetSubAccountPositionMarginConfig (signed message), Errors and retries, Python SDK, Environments).
Fetch other pages from the index at {origin}/llms.txt only as you need them. {origin}/downloads/contract.json is a machine-readable contract for tools, not for reading.
Rules: follow AGENTS.md; do not send requests without the user asking you to; read credentials from environment variables; never retry writes automatically; show me the code and how to run it. URLs and generated code samples on this page: Testnet (chain ID 326). Other code on this page doesn't change: it stays on testnet until you switch it in your code. Tutorials, signing cases and live tools always use testnet.
This configuration is applied per-instrument, allowing different margin settings for different positions.
| SDK | client. |
|---|---|
| HTTP | POST https:/, or / for lite names (the SDKs' default) |
| WebSocket | method v1/ on wss:/, or /ws/lite for lite names |
| Auth | Session required (Authentication) |
| Signed | signature is signed: SetSubAccountPositionMarginConfig. always SetSubAccountPositionMarginConfig. (Signing) |
| Retries | Write: never retried automatically (why) |
| Contract id | position. (RPCSetPositionConfigV1) |
Request
Body type ApiSetSubAccountPositionMarginConfigRequest. Fields the server fills are left out.
- The sub-account ID to set the margin type and leverage for
- The instrument of the position to set the margin type and leverage for
- The margin type to set for the position
2 values
- The leverage to set for the position
- The signature of this operation
7 fields of
Signature- The address of the wallet that signed (
0x+ 40 hex characters). That wallet must be allowed to act for the account; for an order, to trade for the sub-account. - The signature's
r, as0x+ 64 hex characters. Without the0xthe signature fails to verify. - The signature's
s, as0x+ 64 hex characters. Without the0xthe signature fails to verify. - The signature's
v, 27 or 28. - When the signature stops being valid, in unix nanoseconds, measured on the exchange's clock. At most 30 days ahead (take-profit and stop-loss orders may be accepted up to 180 days ahead); one already past fails with
2001. On an order it is also the order's lifetime: an order still open then is cancelled with reasonEXPIRED. The SDKs use now + 24 hours. - A number you choose, 0 to 4,294,967,295 (uint32), so that two otherwise identical requests have different signatures. Use a new random one for each request you sign.
A transfer whose signed fields (nonce and expiration included) exactly repeat an earlier one is rejected, not ignored: it fails with
4007.Orders are different. An exact resend of a signed order can be accepted and rest on the book, but the exchange counts fills per signed order, so all copies together never fill more than the signed size, and a resent
IMMEDIATE_OR_CANCELorder whose first copy already filled is rejected. An order signed again (new nonce or expiration) is a new order and can fill in full again. - The chain id in the signed EIP-712 domain, as a string. Testnet 326, production 325;
"0"means the environment's chain id.
Response
Body type ApiSetSubAccountPositionMarginConfigResponse (the same object is the JSON-RPC result over WebSocket).
- Whether the margin type and leverage was acked
Errors
Errors come back as {"code", "message", "status"} (lite {"c", "m", "s"}) with the HTTP status. Codes this call can return (all error codes):
2100InvalidInitialLeverage400- Invalid initial leverage
2101VaultsCannotConfigureLeverage400- Vaults cannot configure leverage
2102MarginTypeChangeFailedHasOpenPosition400- Margin type change failed, has open position for this instrument
2103MarginTypeChangeFailedHasOpenOrders400- Margin type change failed, has open orders for this instrument
2104MarginTypeNotSupported400- Margin type not supported
2105MarginTypeChangeFailed400- Margin type change failed
2107NewLeverageBelowMinimum400- Attempted to set leverage below minimum
2108NewLeverageAboveMaximum400- Attempted to set leverage above maximum
3007NotApplicableForSpotInstrument400- API is not applicable for spot instruments
Plus 6 errors any call can return
1000Unauthorized401- You need to authenticate prior to using this functionality
1001Forbidden403- You are not authorized to access this functionality
1002InternalServer500- Internal Server Error
1003InvalidRequest400- Request could not be processed due to malformed syntax
1004NotFound404- Data Not Found
1006RateLimit429retryable- You have surpassed the allocated rate limit for your tier
Example
Example code
import os
from decimal import Decimal
from grvt import Grvt, enums as e, PrivateKeySigner
with Grvt(
env=os.environ.get("GRVT_ENV", "testnet"),
api_key=os.environ["GRVT_API_KEY"],
signer=PrivateKeySigner(os.environ["GRVT_PRIVATE_KEY"]),
) as client:
resp = client.set_position_config(
sub_account_id=os.environ["GRVT_SUB_ACCOUNT_ID"],
instrument="BTC_USDT_Perp",
margin_type=e.PositionMarginType("ISOLATED"),
leverage=Decimal("10"),
)
print(resp)
Raw HTTP (set GRVT_DOMAIN, see Environments; GRVT_COOKIE and GRVT_ACCOUNT_ID come from login):
The body below is the set_position_config signing case: a real signature by the public test key for chain id 326. Build your own with the Signing guide; the server will reject this one (unknown account). signature.expiration is unix nanoseconds: use now + 1 day (the signing case uses 2100-01-01 so it never expires).
With full field names:
curl -sS "https://trades.testnet.grvt.io/full/v1/set_position_config" \
-H "Content-Type: application/json" \
-H "Cookie: gravity=$GRVT_COOKIE" \
-H "X-Grvt-Account-Id: $GRVT_ACCOUNT_ID" \
--data @- <<EOF
{
"sub_account_id": "8289849667772468",
"instrument": "BTC_USDT_Perp",
"margin_type": "ISOLATED",
"leverage": "10",
"signature": {
"signer": "0x1267c6181aee40f5ddbd70c56ecf4613117326b5",
"expiration": "4102444800000000000",
"nonce": 7003,
"chain_id": "326",
"r": "0x0bb90e28853419909c56ddef5bffa602831db7cd768756c6017fb0d81f425284",
"s": "0x78c895387b2bc64e7206fa8257b0a796bf5adc46788ac61efb857718cc82dc5c",
"v": 27
}
}
EOFWith lite field names:
curl -sS "https://trades.testnet.grvt.io/lite/v1/set_position_config" \
-H "Content-Type: application/json" \
-H "Cookie: gravity=$GRVT_COOKIE" \
-H "X-Grvt-Account-Id: $GRVT_ACCOUNT_ID" \
--data @- <<EOF
{
"sa": "8289849667772468",
"i": "BTC_USDT_Perp",
"mt": "ISOLATED",
"l": "10",
"s": {
"s": "0x1267c6181aee40f5ddbd70c56ecf4613117326b5",
"e": "4102444800000000000",
"n": 7003,
"ci": "326",
"r": "0x0bb90e28853419909c56ddef5bffa602831db7cd768756c6017fb0d81f425284",
"s1": "0x78c895387b2bc64e7206fa8257b0a796bf5adc46788ac61efb857718cc82dc5c",
"v": 27
}
}
EOF