Login API key
Log in with an API key and get a session for the trading gateway.
On this page
Agent prompt
Write a small program that calls `auth.login_api_key` 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/login_api_key.md (~36 KB, plain Markdown: this page plus the AGENTS.md rules, Authentication and sessions, 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 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.
The response sets the gravity cookie and the X-Grvt-Account-Id header; send both on every authenticated call and on the WebSocket upgrade.
| SDK | Configure the client with an API key; raw edge login is not a generated client method |
|---|---|
| HTTP | POST https:/ (login is served by the edge host, not the trading gateway) |
| WebSocket | not available (edge is HTTP only) |
| Auth | None |
| Contract id | edge.api_key_login |
Request
Body type EdgeApiKeyLoginRequest.
- The API key created in the Grvt UI.
Response
Body type EdgeLoginResponse.
- "success" on success.
- Always empty for API callers.
- Funding (main) account the key belongs to.
- Set only when the key was created for a trading (sub) account.
Errors
Edge errors are not gateway error codes. Body: EdgeLoginError.
| HTTP | Meaning |
|---|---|
| 400 | invalid request body |
| 401 | Invalid Credentials |
| 403 | IP not whitelisted, country restricted, or account deactivated, closed or on hold |
| 500 | Internal Server Error |
Example
Example code
import os
from grvt import Grvt
# Login happens on the first authenticated call and is renewed before the cookie expires.
with Grvt(env=os.environ.get("GRVT_ENV", "testnet"), api_key=os.environ["GRVT_API_KEY"]) as client:
print(client.get_sub_accounts())
Raw HTTP. -D - prints the response headers, which carry the cookie and account id:
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
From the response, take gravity=... from Set-Cookie into GRVT_COOKIE (value only) and X-Grvt-Account-Id into GRVT_ACCOUNT_ID.