Can you explain what the Accept, Accept-Encoding, and Connection headers mean? And are they always necessary? I thought just a user-agent was okay
How to Use the Chess.com API in Python
The user-agent is for Chess.com authentication
The others are for code efficiency.(they are not required.)
🔹 Accept
-
Purpose: Tells the server which content types (MIME types) the client can handle.
-
Example:
-
Meaning: “I prefer json”
🔹 Accept-Encoding
-
Purpose: Tells the server which compression methods the client supports. Speeds downloads.
-
Example:
-
Meaning: “You can compress the response with gzip, or deflate.”
🔹 Connection
-
Purpose: Controls whether the network connection stays open or closes after the request.
-
Example:
→ “Keep the TCP connection open for further requests.”
Oh that’s cool. Where can I read up on the other options / formats and what these specific compression methods mean?
Requests is an elegant and simple HTTP library for Python, built for human beings. Link to the documentation
Great question. I have found some cool info on compression that may change my code...
# pip install brotli
import requests
import os
def get_email():
email = os.getenv("EMAIL")
if not email:
email = input("Enter your email: ").strip()
os.environ["EMAIL"] = email
# Choose rc file based on shell (zsh vs bash)
shell = os.getenv("SHELL", "")
if shell.endswith("zsh"):
rc_file = os.path.expanduser("~/.zshrc")
else:
rc_file = os.path.expanduser("~/.bashrc")
# Avoid duplicate exports
if os.path.exists(rc_file):
with open(rc_file, "r") as f:
contents = f.read()
else:
contents = ""
export_line = f'export EMAIL="{email}"'
if export_line not in contents:
with open(rc_file, "a") as f:
f.write("\n" + export_line + "\n")
return email
if __name__ == "__main__":
url = "https://api.chess.com/pub/club/grand-tourneys/members"
headers = {
"User-Agent": f"GT-Members-App/1.0 (Python) -- Finds members (contact: {get_email()})",
"Accept-Encoding": "br"
}
r = requests.get(url, headers=headers)
print("Content-Encoding:", r.headers.get("Content-Encoding"))
print("Status:", r.status_code)
print("Preview:", r.text[:300])
So Brotli --Googles new compression is better(10%-15%) but you need to install this into python.
pip install brotli
The script above checks if br is can be returned by the api.
Enter your email: my@email.com
Content-Encoding: br [it can]
Status: 200
Preview: {"weekly":[{"username":"alalper","joined":1739026510},{"username":"alexk82","joined":1742453264}, ... }
NOTE: I also fixed this so it will store the email as an environment variable on Windows, Mac or Linux on first use. It should help with the issue I assigned you.
🧠 How to Use the Chess.com API in Python — With Headers & Real Examples♟️🐍
So what do you want to do with the Chess.com API?
You can pull user profiles, ratings, games, tournament participants, and more. The data is all public, and it's a great way to build analysis tools, or club automation systems.
⚠️ Why You Need Custom Headers
Before we jump into the code, here’s a critical tip:
Always include headers in your API requests.
Chess.com uses rate limiting and spam prevention. If you skip the headers, you’ll be
Forbidden.403 Forbidden(Access denied)If do do to much to fast you will get.
429 Too Many Requests(You’ve hit the rate limit)Adding a proper
User-Agentwith your app name and contact info tells their system:🧰 Setup: Define Your Custom Headers in Python
We'll use an
Enumto organize different headers for different purposes. This is optional but clean and scalable:🧪 Example: Get the List of Players in a Tournament
Let’s say you want to get a list of usernames who joined a specific tournament.
📘 Endpoint:
https://api.chess.com/pub/tournament/{tournament-name}/playersHere’s a full Python function to do just that:
🧪 Try It Out:
Output:
✅ Summary
Headers matter: use
User-Agentto avoid getting blocked.The API is free and public — but you still need to be polite.
The data you can grab is super useful for clubs, tournaments, analysis, and automation.
💬 Got questions? Want to explore more endpoints like user stats, game history, or leaderboards? Drop a comment in this thread.