Developers

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

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.

PythonTypeScriptGoRust
api_keyapiKeyAPIKey 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).

  1. Log in: POST https://edge.testnet.grvt.io/auth/api_key/login with {"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
  1. 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>
  1. 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
  1. For WebSocket, send the same Cookie and X-Grvt-Account-Id headers on the upgrade request to wss://trades.testnet.grvt.io/ws/full.
  2. Log in again before the cookie's Expires time. The session lifetime is not published, so read it from Expires. 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 gravity cookie: 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 Expires time, 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.

  1. Sign a WalletLogin EIP-712 message with signer, nonce and expiration (at most 5 minutes ahead of server time).
  2. POST /auth/wallet/login with {address, signature}. The response body holds only funding_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

KeyUsed forWhere it goes
API keyGetting a sessionSent to edge once per login
Signing key (the private key of the wallet registered for the API key)Signing orders, transfers, withdrawalsNever 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).

esc

↑ ↓ move↵ open⇧↵ open .mdTry an error code like 2002

Keyboard shortcuts

⌘ K
Search the docs
/
Search
l
Next code language (Python → TypeScript → Go → Rust → curl → WebSocket)
j / k
Next / previous page
t
Light / dark theme
?
This help
Esc
Close dialogs and drawers