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.

You can check the API documentation to generate code to do this in any language.

Common Parameters

PropertyDescription
searchPhraseSearch phrase to filter variables by name or description.
includePublicIf true, this also returns variables for which the user has no measurements or reminders yet.
sortThe 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.