Cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

getting the sleep stages with the web API

In the web api documentation, under sleep logs: https://dev.fitbit.com/build/reference/web-api/sleep/

It says that you can get one of these two responses:

- stages: Levels data is returned with 30-second granularity. 'Sleep Stages' levels include deeplightrem, and wake.

- classic:  Levels data returned with 60-second granularity. 'Sleep Pattern' levels include asleeprestless, and awake.

 

I'm accessing my sleep data through the api (using python) like this:

client.sleep(date=datetime.date(2018,04,20))

and only getting the classic response.

I've also tried

client.time_series(resource="sleep",period='1d')
client.intraday_time_series(resource="sleep")

both returned an "Invalid time series resource path: /sleep" error

 

I have a fitbit alta hr and in the app i can see the sleep stages in detail (REM, deep etc.) -

How can I access them through the API?

Best Answer
0 Votes
18 REPLIES 18

I'm assuming you're receiving the "Invalid time series resource path: /sleep" because you're trying to use a sleep time series endpoint which doesn't exist.  

 

Would you use the Web API Explorer tool, https://dev.fitbit.com/build/reference/web-api/explore/, to confirm that you're getting back the sleep stages?

Gordon Crenshaw
Senior Technical Solutions Consultant
Fitbit Partner Engineering & Web API Support | Google
Best Answer
0 Votes

@GordonFitbit I can't authenticate in the website, i get this error:

 

Developer information: invalid_request - Invalid redirect_uri parameter value

 

Is there any other way to access this inforamtion?

Best Answer
0 Votes

How can I get a time serie of REM minutes for instance ? is there a direct call ? the sleep part of the API is not super clear (compared to activities for instance)

Best Answer
0 Votes

Hi @cyrillm,

 

The options we have available are the endpoints

  • Get Sleep Log by Date - /1.2/user/-/sleep/date/{date}.json
  • Get Sleep Log by Date Range - /1.2/user/-/sleep/date/{begin-date}/{end-date}.json

These endpoints will display the rem sleep and the timestamp.  Unlike the Activities Time Series, we group the same sleep levels occurring consecutively.

Gordon Crenshaw
Senior Technical Solutions Consultant
Fitbit Partner Engineering & Web API Support | Google
Best Answer
0 Votes

Hello,

I am using fitbit-python library to access fitbit API.

```res = auth_client.time_series('sleep', base_date=start_date, end_date=end_date)```

 

I am accessing sleep range log data by this path:

/sleep/date/2017-04-02/2017-04-08.json

' as instructed in the fitbit website ('https://dev.fitbit.com/build/reference/web-api/sleep/')

 

However, I am only receiving sleep data excluding sleep stages (deep, rem, light.. )

 

any help?

 

thanks

 

 

 

Best Answer
0 Votes

Hi @skim88 

 

There are several reasons why you may only get classic sleep information instead of sleep stages

1. Back in 2017, do you know if this user had a device that supported heart rate and sleep stages?  

2. The sleep wasn't long enough to calculate sleep stages.  The minimum is around 3 hours.

3. The sleep data was manually entered.

etc.

 

Would you have this user confirm in their Fitbit dashboard that sleep stage information is present by selecting one of the sleep logs?

 

Gordon Crenshaw
Senior Technical Solutions Consultant
Fitbit Partner Engineering & Web API Support | Google
Best Answer
0 Votes
Hi, thanks for responding quickly.

Couple things to clarify is that, this is my own data and the data I am
getting doesnt include classic data either. It includes, “minuteData”.
Also, if I request by each date, I get stage data.

It is only when I request multiple days in a range through time series that
the response doesnt include the detailed data regardless of classic or
stage data.

Thanks,
Will be looking forward to your input.
--
Best regards,
Seho Kim
Best Answer
0 Votes

Hi Seho,

 

Would you please try executing the same endpoint using the Web API Explorer tool?  Here's the link: https://dev.fitbit.com/build/reference/web-api/explore/.  First, authorize yourself by clicking on the "authorize" button and selecting all of the scopes.  Then execute the endpoint "Get Sleep Logs by Date Range" /1.2/user/-/sleep/date/{base-date}/{end-date}.json and let me know if you get the same results.

 

Gordon

Gordon Crenshaw
Senior Technical Solutions Consultant
Fitbit Partner Engineering & Web API Support | Google
Best Answer
0 Votes
Hi Gordon,

I was able to get the sleep stage data using this method.
Do you have any speculation as to why I wasn't able to get this data
using Python-Fitbit library? Even though, I follow the instruction from
Fitbit Website for querying the same data.

I appreciate your input.

--
Best regards,
Seho Kim
Best Answer
0 Votes

thanks for this - I changed my api call, but the result I get only holds the classic results : for instance

'sleep': [
{'dateOfSleep': '2019-02-08', 'duration': 24180000, 'efficiency': 90, 'endTime': '2019-02-08T05: 43: 00.000', 'infoCode': 0, 'levels': {'data': [
{'dateTime': '2019-02-07T23: 00: 00.000', 'level': 'asleep', 'seconds': 3120
},
{'dateTime': '2019-02-07T23: 52: 00.000', 'level': 'restless', 'seconds': 180
},
{'dateTime': '2019-02-07T23: 55: 00.000', 'level': 'asleep', 'seconds': 4500
},
{'dateTime': '2019-02-08T01: 10: 00.000', 'level': 'awake', 'seconds': 60
},
.........
{'dateTime': '2019-02-08T05: 41: 00.000', 'level': 'restless', 'seconds': 60
},
{'dateTime': '2019-02-08T05: 42: 00.000', 'level': 'asleep', 'seconds': 60
}
], 'summary': {'asleep': {'count': 0, 'minutes': 361
}, 'awake': {'count': 4, 'minutes': 5
}, 'restless': {'count': 13, 'minutes': 37
}
}
}, 'logId': 21136005214, 'minutesAfterWakeup': 0, 'minutesAsleep': 361, 'minutesAwake': 42, 'minutesToFallAsleep': 0, 'startTime': '2019-02-07T23: 00: 00.000', 'timeInBed': 403, 'type': 'classic'
Best Answer
0 Votes

Hi @skim88 

 

I'm not sure why the python library is not working for you.  We don't directly support those libraries.  My recommendation would be to contact the developer of the library and request help.  I can help arm you with information.  If you reproduce the problem again, and private message me your client ID, the time you recreated the problem, and the user id you're using, I'll see what information we have in our logs that you can send to the developer.

 

Gordon

Gordon Crenshaw
Senior Technical Solutions Consultant
Fitbit Partner Engineering & Web API Support | Google
Best Answer
0 Votes

Hi @cyrillm 

 

What type of device are you using to track your sleep?

Gordon Crenshaw
Senior Technical Solutions Consultant
Fitbit Partner Engineering & Web API Support | Google
Best Answer
0 Votes
Charge 2



Works fine on the app
Best Answer
0 Votes

Hi @cyrillm ,

 

Check the data in your Fitbit dashboard on 2019-02-08 to see if the tracker stored the data as a classic sleep.  If it did, the APIs will not convert it to staged sleep.

 

Gordon

Gordon Crenshaw
Senior Technical Solutions Consultant
Fitbit Partner Engineering & Web API Support | Google
Best Answer
0 Votes

GET https://api.fitbit.com/1.2/user/[user-id]/sleep/date/[startDate]/[endDate].json

is the endpoint I am trying to execute.  

 

I couldn't send a private message, it says I have reached the limit for sending private message after sending one.. 

 

Best Answer
0 Votes

Hi @skim88,

 

Thank you for sending me the endpoint and your user id.  It appears your tracker is recording the sleep stages in your account correctly.  Have you tried executing the endpoint using the Web API Explorer, https://dev.fitbit.com/build/reference/web-api/explore/?  See if the data is coming back correctly here.  This will help determine if the problem is related to the Web APIs or the python library you're using.

 

 

Gordon Crenshaw
Senior Technical Solutions Consultant
Fitbit Partner Engineering & Web API Support | Google
Best Answer
0 Votes

Hi Gordon,

 

I have tried the Web API explorer and was able to get what I wanted. 

We had this conversion:

Hi @skim88 

 

I'm not sure why the python library is not working for you.  We don't directly support those libraries.  My recommendation would be to contact the developer of the library and request help.  I can help arm you with information.  If you reproduce the problem again, and private message me your client ID, the time you recreated the problem, and the user id you're using, I'll see what information we have in our logs that you can send to the developer.

 

Gordon

 

I was hoping to figure out what is going on with the library. 

 

Thanks,

Best Answer
0 Votes

If the data is coming back correctly with the Web APIs, then I'm going to assume the problem is with the python library.  The OAuth libraries are not owned or supported by Fitbit.  I would recommend you reach out to the developer of the OAuth library to get assistance.

Gordon Crenshaw
Senior Technical Solutions Consultant
Fitbit Partner Engineering & Web API Support | Google
Best Answer
0 Votes