his function fetches user-defined and public variables based on specified criteria.
Variables can include treatments, symptoms, or any custom user data.
It allows for filtering based on a search phrase, sorting by measurement time.
First, create a getDfdaAccessToken() function as described here if you haven’t done that already.
Common Parameters
| Property | Description |
|---|
| searchPhrase | Search phrase to filter variables by name or description. |
| includePublic | If true, this also returns variables for which the user has no measurements or reminders yet. |
| sort | The order in which variables are returned. Default is -latestMeasurementTime. |
async function fetchUserVariables(searchPhrase, includePublic = true, limit = 10, offset = 0, sort = '-latestMeasurementTime') {
const baseUrl = 'https://safe.dfda.earth/api/v3/userVariables';
const params = new URLSearchParams({
includePublic: includePublic,
limit: limit,
offset: offset,
searchPhrase: searchPhrase,
sort: sort
});
const response = await fetch(`${baseUrl}?${params.toString()}`, {
method: 'GET',
headers: {
'accept': 'application/json',
'Authorization': `Bearer ${getDfdaAccessToken()}`,
},
credentials: 'include'
});
if (!response.ok) {
throw new Error('Network response was not ok');
}
return await response.json();
}
Use the sort parameter to manage the order of returned variables, such as by their latest measurement time here.