Pulling Health Data After Notification
In the previous section, we explored how to subscribe to the Withings API to receive notifications about new data.
Now, let’s envision a scenario: our user steps on their scale to take a weight measurement.
This action triggers a notification to your system, signaling that it's time to retrieve this fresh data.
Parsing the Notification
When your system receives a notification, it will contain essential information in the payload, such as the userid that uniquely identifies the user and the appli parameter indicating the application context. This information will guide you in fetching the latest health data.
Retrieving the Access Token
The first step in the data retrieval process is to locate the access token associated with the userid. By querying your database, you can retrieve this token, which authorizes your request to the Withings API.
Defining the Time Range
Next, you have two options for specifying the timeframe for your data request:
- Using
startdateandenddate: These values from the notification can pinpoint the exact time range of interest. - Last Update System: Our recommended approach, 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, the next step is to decide which API endpoint to call based on the appli value in the notification. Be sure to consult the next page for detailed instructions on which Web Service (WS) to utilize depending on the application context.
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();