Learn how to authenticate with the Stake.com API using access tokens and session cookies.
StakeAPI uses the same authentication mechanism as the Stake.com website. You need an access token (and optionally a session cookie) to make authenticated requests.
You must have a Stake.com account to use StakeAPI. If you don’t have one yet, sign up here — it takes less than a minute.
F12 or Ctrl+Shift+I (Windows) / Cmd+Option+I (Mac)/_api/graphqlx-access-token header — This is your access tokenYour access token will look something like:
2775b505cccaee723e5c705ba552fea7c272f6d20f68d7224eb3ba23446ca295...
from stakeapi.auth import AuthManager
curl_command = """
curl "https://stake.com/_api/graphql" \
-H "x-access-token: your_token_here" \
-H "content-type: application/json" \
...
"""
# Extract access token
token = AuthManager.extract_access_token_from_curl(curl_command)
print(f"Access Token: {token}")
# Extract session cookie
session = AuthManager.extract_session_from_curl(curl_command)
print(f"Session Cookie: {session}")
Store your token securely in an environment variable:
# Windows (PowerShell)
$env:STAKE_ACCESS_TOKEN = "your_access_token_here"
# macOS/Linux
export STAKE_ACCESS_TOKEN="your_access_token_here"
Then use it in your code:
import os
from stakeapi import StakeAPI
token = os.getenv("STAKE_ACCESS_TOKEN")
async with StakeAPI(access_token=token) as client:
balance = await client.get_user_balance()
Create a .env file in your project root:
STAKE_ACCESS_TOKEN=your_access_token_here
STAKE_SESSION_COOKIE=your_session_cookie_here
Then load it with python-dotenv:
import os
from dotenv import load_dotenv
from stakeapi import StakeAPI
load_dotenv()
token = os.getenv("STAKE_ACCESS_TOKEN")
session = os.getenv("STAKE_SESSION_COOKIE")
async with StakeAPI(access_token=token, session_cookie=session) as client:
balance = await client.get_user_balance()
Never commit your
.envfile to version control. Add it to your.gitignorefile.
💡 Don't have a Stake.com account yet? Sign up here to get started and unlock all API features.
StakeAPI supports multiple authentication methods:
The simplest approach — sufficient for most use cases:
async with StakeAPI(access_token="your_token") as client:
# Make API calls
pass
For maximum compatibility, use both:
async with StakeAPI(
access_token="your_token",
session_cookie="your_session_cookie"
) as client:
# Make API calls
pass
For advanced token management:
from stakeapi.auth import AuthManager
auth = AuthManager(access_token="your_token")
# Check if token is expired
if auth.is_token_expired():
print("Token has expired, get a new one!")
# Update token
auth.set_access_token("new_token", expires_in=3600)
# Get auth headers for custom requests
headers = await auth.get_auth_headers()
| Aspect | Detail |
|---|---|
| Format | 96-character hex string |
| Lifetime | Session-based (varies) |
| Scope | Full account access |
| Rotation | New token per login session |
| Invalidation | Logging out invalidates the token |
.env files.gitignore — Exclude .env and any files containing tokens# .gitignore
.env
*.env
config.py
secrets.py
Tokens can expire or be invalidated. Handle this gracefully:
from stakeapi import StakeAPI
from stakeapi.exceptions import AuthenticationError
async with StakeAPI(access_token="your_token") as client:
try:
balance = await client.get_user_balance()
except AuthenticationError:
print("Token expired! Please get a new token from stake.com")
# Optionally: re-authenticate or notify user
from stakeapi.auth import AuthManager
auth = AuthManager(access_token="your_token")
# Set expiration tracking
auth.set_access_token("your_token", expires_in=7200) # 2 hours
# Check later
if auth.is_token_expired():
print("Time to refresh your token!")
auth.clear_tokens()
💬 Join the StakeAPI Community on Discord
Get help, share your projects, discuss strategies, and stay up to date with the latest StakeAPI news.
Join Our Discord Server →✏️ Built with ChipaEditor
ChipaEditor is the ultimate code editor built for speed, simplicity, and power. Try it free today.
Try ChipaEditor Free →Now that you’re authenticated, make your first API call:
Need a Stake.com account to get started? Sign up here — it’s free and takes less than a minute.