# Get Sleep Data

TIP

This guide assumes that your app has already been authorized and the snippet of code that I will show you can access to the Fitbit user id, here called userID, the Fitbit OAuth 2.0 client ID, here called clientID, and the Fitbit client secret, here called clientSecret.

Sleep data (opens new window) contain details about a user's activity. In Fitbitter, a datapoint is expressed by the FitbitSleepData model. Fitbitter fetches a simplified version of Sleep data (opens new window) as described by Fitbit. Fitbitter fetches, for each sleep session, a list of FitbitSleepData. Each datapoint describes, with a 30-seconds granularity, if the user is in deep sleep, light sleep, rem, or awake. In particular, an instance of FitbitSleepData has the following fields:

  /// The user encoded id.
  String? userID;

  /// The date of when the sleep session begun.
  DateTime? dateOfSleep;

  /// The date of the data entry.
  DateTime? entryDateTime;

  /// The level of the sleep data (can be light, deep, rem, or wake).
  String? level;

For example:

[
    FitbitSleepData(userID: 7ML2XV, dateOfSleep: 2022-05-09 00:00:00.000, entryDateTime: 2022-05-08 22:38:30.000, level: wake, ), 
    FitbitSleepData(userID: 7ML2XV, dateOfSleep: 2022-05-09 00:00:00.000, entryDateTime: 2022-05-08 22:39:00.000, level: wake, ),
    FitbitSleepData(userID: 7ML2XV, dateOfSleep: 2022-05-09 00:00:00.000, entryDateTime: 2022-05-08 22:39:30.000, level: wake, ), 
    FitbitSleepData(userID: 7ML2XV, dateOfSleep: 2022-05-09 00:00:00.000, entryDateTime: 2022-05-08 22:40:00.000, level: wake, ), 
    FitbitSleepData(userID: 7ML2XV, dateOfSleep: 2022-05-09 00:00:00.000, entryDateTime: 2022-05-08 22:40:30.000, level: wake, ), 
    FitbitSleepData(userID: 7ML2XV, dateOfSleep: 2022-05-09 00:00:00.000, entryDateTime: 2022-05-08 22:41:00.000, level: wake, ), 
    FitbitSleepData(userID: 7ML2XV, dateOfSleep: 2022-05-09 00:00:00.000, entryDateTime: 2022-05-08 22:41:30.000, level: wake, ), 
    ...
]

Information about the user's Sleep data can be obtained in three steps:

# Step 1: Instanciate a manager

First, you need to instanciate a FitbitSleepDataManager

    FitbitSleepDataManager fitbitSleepDataManager = FitbitSleepDataManager(
        clientID: '<OAuth 2.0 Client ID>',
        clientSecret: '<Client Secret>',
    );

# Step 2: Create the request url

Then, you have to create a url object, FitbitSleepAPIURL, that fetches the Sleep data, during the desidered time range. For example:

    FitbitSleepAPIURL fitbitSleepAPIURL = FitbitSleepAPIURL.day(
                                    date: DateTime.now(),
                                    fitbitCredentials: fitbitCredentials,
                                  );

For the complete list of possible FitbitSleepAPIURL, defined for different time ranges, please refer to the API Doc (opens new window).

# Step 3: Get the data

Finally you can obtain the Sleep data using

    List<FitbitSleepAPIURL> fitbitSleepAPIURL = await fitbitSleepDataManager.fetch(fitbitSleepAPIURL);