Pulling Health Data Manually
In some cases, you might prefer to fetch the user's health data manually rather than relying on the notification system. This approach requires providing a mechanism within your application—such as a button—that allows the user to initiate the data retrieval process when they're ready.
Let's envision a scenario: our user steps on their scale to take a weight measurement.
After the measurement is complete, they can trigger the data fetching process manually within your application.
Initiating the Data Fetch
To facilitate manual data retrieval, include a user interface element (like a "Refresh Data" button) that the user can interact with once their measurement is complete. This action signals your system to fetch the latest health data from the Withings API.
Retrieving the Access Token
The first step in the data retrieval process is to locate the access token associated with the user. By querying your database, you can retrieve this token, which authorizes your request to the Withings API.
Defining the Time Range
Next, specify the timeframe for your data request. You have two options:
- Using startdate and enddate: Define the exact time range for which you want to retrieve data.
- Employing a Last Update System: Use lastupdate to fetch all data updated since a specific timestamp. Read more.
Choosing the Right API Endpoint
With the access token and time range in hand, decide which API endpoint to call based on the type of data you wish to retrieve. For weight measurements, use the measure endpoint.
Making the API Call
Construct your API call with the necessary parameters and execute the request to fetch the health data.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
const axios = require('axios');
const accessToken = 'your_access_token'; // Replace with your actual access token
const getMeasurements = async () => {
const data = {
action: 'getmeas',
startdate: 1727825531, // Replace with your actual startdate
enddate: 1728171131 // Replace with your actual enddate
};
try {
const response = await axios.post('https://wbsapi.withings.net/measure', data, {
headers: {
Authorization: `Bearer ${accessToken}`,
'Content-Type': 'application/x-www-form-urlencoded'
}
});
console.log('Response:', response.data);
} catch (error) {
console.error('Error:', error);
}
};
// Call the async function
getMeasurements();