I am not able to connect to the api and modify wellness data as an example in this case using Javascript executed with node.js in VSCode, but I can connect and make changes with my python code. Trying to figure out the possible underlying issue here.
This is my javascript code
const API_KEY = "myapikeyhere";
const ATHLETE_ID = 0; // Use 0 to fetch your own data
// Get today's date in YYYY-MM-DD format
const today = new Date().toISOString().split('T')[0];
// Construct the API URL
const url = `https://intervals.icu/api/v1/athlete/${ATHLETE_ID}/wellness/${today}`;
// JSON payload with updated hrvSDNN value
const wellnessData = {
"hrvSDNN": 80 // Replace with actual HRV SDNN value
};
// Convert data to JSON
const body = JSON.stringify(wellnessData);
// Function to send PUT request
async function updateWellness() {
try {
const response = await fetch(url, {
method: "PUT",
headers: {
"Authorization": `Basic ${Buffer.from(API_KEY).toString("base64")}`,
"Content-Type": "application/json"
},
body: body
});
// Log the status and response
console.log("Status Code:", response.status);
const data = await response.json();
console.log("API Response:", data);
} catch (error) {
console.error("Error updating wellness data:", error);
}
}
// Run the function
updateWellness();
and this returns:
Status Code: 401
API Response: { status: 401, error: 'Unauthorized' }
Here is my python code:
import requests
from datetime import datetime
ATHLETE_ID = 0 # Change this if necessary
API_KEY = "myapikeyhere"
DATE = datetime.today().strftime('%Y-%m-%d')
url = f'https://intervals.icu/api/v1/athlete/{ATHLETE_ID}/wellness/{DATE}'
DATA = {
"hrvSDNN": 88
}
headers = {
"authorization": f"basic {API_KEY}",
"content-type": "application/json"
}
response = requests.put(url, json=DATA, auth=('API_KEY', API_KEY))
if response.status_code == 200:
print("Connection successful!")
print(response.content)
else:
print(f"Failed to connect. Status code: {response.status_code}")
print(response.text)
and this outputs the following and updates my wellness data on my calendar page:
I have no knowledge of Python but also think that you´re making a mistake with basic authehtication. The user is always and for everyone the string ´API_KEY´. The pw is your api-key when using basic auth.
You probably donât understand this right.
âAPI_KEYâ is the string what you have to use for the user name. It doesnât matter how you name your variables.
This is my code
// Basic Auth Informationen
let username = "API_KEY"; // you need this!
let password = "yoursecretkey";
let credentials = `${username}:${password}`;
@R2Tom It seems you have this working but for clarities sake, your API_KEY is absolutely not your user name or your user id. If you look on your settings page there is an âAthlete IDâ and âAPI KEYâ. There is then apart from those two values your user name which is used to login into intervals through the browser, which is typically or always an email address I assume. Finally there is of course your password used with your user name to login to intervals from the browser.
Are you using your user name and password like you would use to log in from the browser?