Authenticate with an API key
Every read operation in the KRKplaysTennis API needs an API key. You send the key in the X-API-Key header of each request.
An API key can only read data. To submit match results, you need an access token instead. See Get an access token.
Before you begin
Section titled “Before you begin”- An API key. With the local mock, any non-empty value works.
KRK_API_URLset to the API base URL. For the mock, that’shttp://127.0.0.1:4010.
-
Store the key in an environment variable, so it stays out of your code:
Terminal window export KRK_API_KEY=your-api-keyTo check it, run
echo $KRK_API_KEY. The output is your key. -
Send the key in the
X-API-Keyheader. This request gets a player’s profile:Terminal window curl --silent "$KRK_API_URL/players/plr_2b7xq9mhk4" \--header "X-API-Key: $KRK_API_KEY"Save this code as
get-player.mjs, then runnode get-player.mjs:const response = await fetch(`${process.env.KRK_API_URL}/players/plr_2b7xq9mhk4`, {headers: { 'X-API-Key': process.env.KRK_API_KEY },});if (!response.ok) throw new Error(`Request failed: ${response.status}`);console.log(await response.json());Save this code as
get_player.py, then runpython3 get_player.py:import osimport requestsresponse = requests.get(f"{os.environ['KRK_API_URL']}/players/plr_2b7xq9mhk4",headers={"X-API-Key": os.environ["KRK_API_KEY"]},)response.raise_for_status()print(response.json())The response has status
200and the profile of Ola K., with"display_name": "Ola K."and aratingobject.
If the key is missing or wrong
Section titled “If the key is missing or wrong”When a request has no X-API-Key header, or the key isn’t valid, the API returns status 401 with this body:
{ "type": "https://saverio-cirillo.com/krk-plays-tennis-api/errors/#unauthenticated", "title": "Authentication required", "status": 401, "detail": "Send a valid API key in the X-API-Key header.", "instance": "/v1/players/plr_2b7xq9mhk4"}Check that the header name is exactly X-API-Key and that the environment variable is set in the terminal where you run the request.
Keep your key secret
Section titled “Keep your key secret”- Keep the key on a server. Don’t put it in a browser app or a mobile app, where anyone can read it.
- Don’t commit the key to a repository. Load it from an environment variable or a secrets manager.
- If a key leaks, replace it and stop using the old one.
© 2026 Saverio Cirillo · KRKplaysTennis is a fictional API · Built with Starlight