---
title: "Authentication and sessions"
description: "Log in with an API key or wallet signature, send the session on HTTP and WebSocket, and renew it."
order: 4
llms: trading, account
---

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(...)` |

```python
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())
```

## By hand

**Advanced.** **When:** you call HTTP or WebSocket without an SDK, or you write C++ ([C++ guide](../sdks/cpp.md)).

1. Log in: `POST https://edge.<domain>/auth/api_key/login` with `{"api_key": "..."}`.

```sh
curl -sS -D - "https://edge.$GRVT_DOMAIN/auth/api_key/login" \
  -H "Content-Type: application/json" \
  --data @- <<EOF
{"api_key": "$GRVT_API_KEY"}
EOF
```

2. Save two values from the response:
   - `Set-Cookie: gravity=<token>; Expires=...`: the session cookie.
   - `X-Grvt-Account-Id: <id>`: a response header.

```sh
export GRVT_COOKIE=<token>          # the value after gravity=, without the attributes
export GRVT_ACCOUNT_ID=<id>
```

3. Send both on every trading call.

```sh
curl -sS "https://trades.$GRVT_DOMAIN/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
```

4. For WebSocket, send the same `Cookie` and `X-Grvt-Account-Id` headers on the upgrade request to
   `wss://trades.<domain>/ws/full`.
5. 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](../reference/rpc/login_wallet.md) 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`).
