Troubleshooting
Solutions to common issues when using StakeAPI.
Authentication Issues
AuthenticationError: Invalid access token
Cause: Your access token is invalid or has expired.
Solution:
- Log in to Stake.com in your browser
- Open Developer Tools → Network tab
- Find a request to
/_api/graphql - Copy the fresh
x-access-tokenvalue
# Check if token is valid format
from stakeapi.utils import validate_api_key
print(validate_api_key(your_token)) # Should be TrueToken works in browser but not in Python
Cause: Token may have been invalidated after you logged out, or headers are missing.
Solution: Ensure you’re passing the token correctly:
# Correct
client = StakeAPI(access_token="your_actual_token_value")
# Wrong — don't include the header name
client = StakeAPI(access_token="x-access-token: token_value")
Connection Issues
NetworkError: Request failed
Cause: Cannot connect to Stake.com servers.
Solutions:
- Check your internet connection
- Verify Stake.com is accessible from your location
- Check if you’re behind a firewall/proxy
- Try increasing the timeout:
client = StakeAPI(access_token="token", timeout=60)Connection timeout
Cause: Request took too long.
Solution: Increase timeout:
client = StakeAPI(access_token="token", timeout=120)
Rate Limiting
RateLimitError: Rate limit exceeded
Cause: Too many requests too quickly.
Solution: Add delays between requests:
import asyncio
# Add delay between requests
for game_id in game_ids:
game = await client.get_game_details(game_id)
await asyncio.sleep(0.5) # 500ms delay
Or use exponential backoff:
async def fetch_with_backoff(func, max_retries=3):
for i in range(max_retries):
try:
return await func()
except RateLimitError:
await asyncio.sleep(2 ** i)
raise Exception("Max retries exceeded")
Import Issues
ModuleNotFoundError: No module named 'stakeapi'
Solution:
pip install stakeapi
If using a virtual environment, make sure it’s activated:
# Windows
.venv\Scripts\activate
# macOS/Linux
source .venv/bin/activateImportError: cannot import name 'StakeAPI'
Solution: Check your import statement:
# Correct
from stakeapi import StakeAPI
# Wrong
from stakeapi.client import StakeAPI # Also works but not recommended
Async Issues
RuntimeError: no running event loop
Cause: Using async code without an event loop.
Solution: Use asyncio.run():
import asyncio
async def main():
async with StakeAPI(access_token="token") as client:
balance = await client.get_user_balance()
# Correct
asyncio.run(main())
# Wrong — don't call async functions directly
# main() # This returns a coroutine, not the resultRuntimeWarning: coroutine was never awaited
Cause: Forgetting to await an async call.
# Wrong
balance = client.get_user_balance() # Missing await!
# Correct
balance = await client.get_user_balance()RuntimeError: Session is closed
Cause: Using the client after the context manager has exited.
# Wrong
async with StakeAPI(access_token="token") as client:
pass
# Client is closed here!
balance = await client.get_user_balance()
# Correct — keep operations inside the context
async with StakeAPI(access_token="token") as client:
balance = await client.get_user_balance()
Data Issues
Empty results from API calls
Possible causes:
- Account has no data (new account with no bets)
- Filter parameters are too restrictive
- API endpoint returned unexpected format
Solution: Try without filters first:
# Try without category filter
games = await client.get_casino_games() # No filter
print(f"Total games: {len(games)}")Pydantic validation errors
Cause: API returned unexpected data format.
Solution: Use raw GraphQL to inspect the response:
data = await client._graphql_request(
query="query { user { id name } }"
)
print(data) # Inspect raw response
Platform-Specific Issues
Windows: ProactorEventLoop warnings
Add this at the top of your script:
import asyncio
import sys
if sys.platform == "win32":
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())Jupyter Notebook: Event loop already running
Use nest_asyncio:
import nest_asyncio
nest_asyncio.apply()
import asyncio
from stakeapi import StakeAPI
async def main():
async with StakeAPI(access_token="token") as client:
return await client.get_user_balance()
balance = asyncio.run(main())
Still Stuck?
- Check the FAQ
- Search GitHub Issues
- Open a new issue with:
- StakeAPI version
- Python version
- Full error traceback
- Minimal reproducing code