Authentication and sessions
Log in with an API key or wallet signature, send the session on HTTP and WebSocket, and renew it.
On this page
Help me with: Authentication and sessions. Log in with an API key or wallet signature, send the session on HTTP and WebSocket, and renew it.
Target environment: {environment} (chain ID {chainId}). Recorded signing cases keep their original environment.
{executionGuidance}
Read this first: {origin}/bundle/guides/authentication.md (~16 KB, plain Markdown: this page plus the AGENTS.md rules, 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 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.
Public market data needs no login. Every call to the trading host, and every private stream, needs a
session: a gravity cookie plus an X-Grvt-Account-Id header. You get both by logging in with your API
key. Reference pages that need a session say Auth: Session required.
With an SDK
Pass your API key to the client. It logs in on the first private call, and again at 80% of the cookie's lifetime.
| Python | TypeScript | Go | Rust |
|---|---|---|---|
api_key | apiKey | APIKey in Config | .api_key(...) |
import os
from grvt import Grvt
with Grvt(env=os.environ.get("GRVT_ENV", "testnet"), api_key=os.environ["GRVT_API_KEY"]) as client:
print(client.get_sub_accounts())
Set your testnet account and signer for the tools on this site
By hand
Advanced. When: you call HTTP or WebSocket without an SDK, or you write C++ (C++ guide).
- Log in:
POST https://edge.testnet.grvt.io/auth/api_key/loginwith{"api_key": "..."}.
curl -sS -D - "https://edge.testnet.grvt.io/auth/api_key/login" \
-H "Content-Type: application/json" \
--data @- <<EOF
{"api_key": "$GRVT_API_KEY"}
EOF
- Save two values from the response:
Set-Cookie: gravity=<token>; Expires=...: the session cookie.X-Grvt-Account-Id: <id>: a response header.
export GRVT_COOKIE=<token> # the value after gravity=, without the attributes
export GRVT_ACCOUNT_ID=<id>
- Send both on every trading call.
curl -sS "https://trades.testnet.grvt.io/full/v1/sub_account_summary" \
-H "Content-Type: application/json" \
-H "Cookie: gravity=$GRVT_COOKIE" \
-H "X-Grvt-Account-Id: $GRVT_ACCOUNT_ID" \
--data @- <<EOF
{"sub_account_id": "$GRVT_SUB_ACCOUNT_ID"}
EOF
- For WebSocket, send the same
CookieandX-Grvt-Account-Idheaders on the upgrade request towss://trades.testnet.grvt.io/ws/full. - Log in again before the cookie's
Expirestime. The session lifetime is not published, so read it fromExpires. If the cookie has none, log in again within an hour, as the SDKs do.
Watch out:
- A call without a valid session fails with error
1000(HTTP 401). - After you log in again, open a new socket with the new cookie. There is no login message inside the socket.
- A new login doesn't end your other sessions (unless the account goes over its session limit), so the old socket keeps working while you open the new one. Close it once the new one is subscribed.
- Send the login request without your current
gravitycookie: a login that carries it replaces that session. - The server checks the session when a socket opens. An open socket isn't closed at the cookie's
Expirestime, but it can be closed later if the session ends.
Wallet login
Advanced. When: you log in with a wallet signature instead of an API key, over raw HTTP.
- Sign a
WalletLoginEIP-712 message withsigner,nonceandexpiration(at most 5 minutes ahead of server time). POST /auth/wallet/loginwith{address, signature}. The response body holds onlyfunding_account_address.
Login wallet and the wallet_login signing case show the raw HTTP flow.
Watch out: the SDK clients don't support wallet login. They manage API-key sessions only.
Two keys, two jobs
| Key | Used for | Where it goes |
|---|---|---|
| API key | Getting a session | Sent to edge once per login |
| Signing key (the private key of the wallet registered for the API key) | Signing orders, transfers, withdrawals | Never sent; only signatures leave your machine |
An API key alone can read your account but can't place an order. Orders need a signature from the signing
key. In the SDKs, the signer is the object that holds the signing key (for example PrivateKeySigner).