---
title: "Add position margin"
description: "Add margin to an isolated position, or remove some with a negative amount."
---

| | |
|---|---|
| SDK | `client.add_position_margin(...)` |
| HTTP | `POST https://trades.<domain>/full/v1/add_position_margin`, or `/lite/v1/add_position_margin` for [lite names](../../guides/environments.md#encoding-full-or-lite) (the SDKs' default) |
| WebSocket | method `v1/add_position_margin` on `wss://trades.<domain>/ws/full`, or `/ws/lite` for lite names |
| Auth | Session required ([Authentication](../../guides/authentication.md)) |
| Signed | `signature` is signed: [`AddIsolatedPositionMargin`](../signing/AddIsolatedPositionMargin.md). always AddIsolatedPositionMargin. ([Signing](../../guides/signing.md)) |
| Retries | Write: never retried automatically ([why](../../guides/errors-and-retries.md)) |
| Contract id | `position.add_position_margin` (`RPCAddPositionMarginV1`) |

## Request

Body type `ApiAddIsolatedPositionMarginRequest`. Fields the server fills are left out.

- `sub_account_id` · lite `sa` · string (uint64) · required — The sub-account ID to add isolated margin in or remove margin from
- `instrument` · lite `i` · string (instrument name) · required — The instrument to add margin into, or remove margin from
- `amount` · lite `a` · string · required — The amount of margin to add to the position, positive to add, negative to remove, in the quote asset. Decimal string, e.g. "12.5". Scaled to an integer only inside the signature.
- `signature` · lite `s` · object Signature · required — The signature of this operation
- `signature.signer` · lite `s` · string (uint256) · required — 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.
- `signature.r` · lite `r` · string (uint256) · required — The signature's `r`, as `0x` + 64 hex characters. Without the `0x` the signature fails to verify.
- `signature.s` · lite `s1` · string (uint256) · required — The signature's `s`, as `0x` + 64 hex characters. Without the `0x` the signature fails to verify.
- `signature.v` · lite `v` · integer (uint8) · required — The signature's `v`, 27 or 28.
- `signature.expiration` · lite `e` · string (int64 unix ns) · required — 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 reason `EXPIRED`. The SDKs use now + 24 hours.
- `signature.nonce` · lite `n` · integer (uint32) · required — 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_CANCEL` order 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.
- `signature.chain_id` · lite `ci` · string (uint64) · required — 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 `ApiAddIsolatedPositionMarginResponse` (the same object is the JSON-RPC `result` over WebSocket).

- `success` · lite `s` · bool · required — Whether the margin mode and leverage was set successfully

## Errors

Errors come back as `{"code", "message", "status"}` (lite `{"c", "m", "s"}`) with the HTTP status. Codes this call can return, as code, name (HTTP status) ([all error codes](../errors.md)):

`1000` Unauthorized (401) · `1001` Forbidden (403) · `1002` InternalServer (500) · `1003` InvalidRequest (400) · `1004` NotFound (404) · `1006` RateLimit (429, retryable) · `3007` NotApplicableForSpotInstrument (400) · `7450` AddMarginFailed (400) · `7451` AddMarginToEmptyPosition (400) · `7452` AddMarginToNonIsolatedPosition (400) · `7453` MaxAddableAmountExceeded (400) · `7454` MaxRemovableAmountExceeded (400)

## Example

```python
import os
from decimal import Decimal

from grvt import Grvt, 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.add_position_margin(
        sub_account_id=os.environ["GRVT_SUB_ACCOUNT_ID"],
        instrument="BTC_USDT_Perp",
        amount=Decimal("250.5"),
    )
    print(resp)
```

Raw HTTP (set `GRVT_DOMAIN`, see [Environments](../../guides/environments.md); `GRVT_COOKIE` and `GRVT_ACCOUNT_ID` come from [login](../../guides/authentication.md)):

The body below is the `add_position_margin` signing case: a real signature by the public test key for chain id 326. Build your own with the [Signing guide](../../guides/signing.md); 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:

```sh
curl -sS "https://trades.$GRVT_DOMAIN/full/v1/add_position_margin" \
  -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",
  "amount": "250.5",
  "signature": {
    "signer": "0x1267c6181aee40f5ddbd70c56ecf4613117326b5",
    "expiration": "4102444800000000000",
    "nonce": 7001,
    "chain_id": "326",
    "r": "0xa6c5b70511e756de6aaf369108936d269b0e61c7673616aa00487aee57796999",
    "s": "0x475a754620a071b66c392968e668cba6c5953408007805303b47749620c63b6c",
    "v": 28
  }
}
EOF
```

With lite field names:

```sh
curl -sS "https://trades.$GRVT_DOMAIN/lite/v1/add_position_margin" \
  -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",
  "a": "250.5",
  "s": {
    "s": "0x1267c6181aee40f5ddbd70c56ecf4613117326b5",
    "e": "4102444800000000000",
    "n": 7001,
    "ci": "326",
    "r": "0xa6c5b70511e756de6aaf369108936d269b0e61c7673616aa00487aee57796999",
    "s1": "0x475a754620a071b66c392968e668cba6c5953408007805303b47749620c63b6c",
    "v": 28
  }
}
EOF
```
