document.write('\x3cmeta http-equiv=\x22x-dns-prefetch-control\x22 content=\x22off\x22/\x3e\x3col class=\x22tumblr_posts\x22\x3e\x0a \x0a \x0a \x3cli class=\x22tumblr_post tumblr_text_post\x22\x3e\x0a \x0a \x3cdiv class=\x22tumblr_title\x22\x3eMoved to blog.beddit.com\x3c/div\x3e\x0a \x0a \x0a \x3cdiv class=\x22tumblr_body\x22\x3e\x0a \x3cp\x3eWe started a new blog at \x3ca href=\x22http://blog.beddit.com\x22\x3e\x3ca href=\x22http://blog.beddit.com\x22\x3ehttp://blog.beddit.com\x3c/a\x3e\x3c/a\x3e, and will post news for developers in that same blog in future.\x3c/p\x3e\x0a \x3c/div\x3e\x0a \x3c/li\x3e\x0a \x0a\x0a \x0a\x0a \x0a\x0a \x0a \x0a \x0a \x0a \x0a\x0a \x0a \x0a \x0a \x3cli class=\x22tumblr_post tumblr_text_post\x22\x3e\x0a \x0a \x3cdiv class=\x22tumblr_title\x22\x3eExample code for Beddit API\x3c/div\x3e\x0a \x0a \x0a \x3cdiv class=\x22tumblr_body\x22\x3e\x0a \x3cp\x3eHere is an example of how to authenticate and fetch data from the Beddit API. It is written in Python language, but it should be easy to understand even if you don\x26#8217;t know Python. I hope this helps in getting started with our API.\x3c/p\x3e\x0a\x0a\x3cp\x3e\x3ccode\x3e\x0a\x3cp\x3e# Beddit API Example\x3cbr/\x3e# \x3cbr/\x3e# This example program (written in Python) demonstrates how to authenticate\x3cbr/\x3e# and fetch sleep information via the Beddit API.\x3cbr/\x3e#\x3cbr/\x3e# Please also read the API Help page in Beddit website.\x3cbr/\x3e\x3cbr/\x3eimport urllib2\x3cbr/\x3eimport json\x3cbr/\x3eimport sys\x3cbr/\x3eimport datetime\x3cbr/\x3e\x3cbr/\x3e\x3cbr/\x3edef get_json(url):\x3cbr/\x3e\u00a0\u00a0\u00a0 \x22\x22\x22Utility function for fetching and parsing json data\x3cbr/\x3e\u00a0\u00a0\u00a0 \x22\x22\x22\x3cbr/\x3e\u00a0\u00a0\u00a0 response = urllib2.urlopen(url)\x3cbr/\x3e\u00a0\u00a0\u00a0 response_content = response.read()\x3cbr/\x3e\u00a0\u00a0\u00a0 \x3cbr/\x3e\u00a0\u00a0\u00a0 try:\x3cbr/\x3e\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 response_dict = json.loads(response_content)\x3cbr/\x3e\u00a0\u00a0\u00a0 except ValueError:\x3cbr/\x3e\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 print \x22Invalid response:\x22, response_content\x3cbr/\x3e\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 sys.exit()\x3cbr/\x3e\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \x3cbr/\x3e\u00a0\u00a0\u00a0 return response_dict\x3cbr/\x3e\x3cbr/\x3e\x3cbr/\x3e# You need to get these settings from Beddit. If you don\'t have these,\x3cbr/\x3e# please email hello@beddit.com to register your application.\x3cbr/\x3eclient_id = \x22XXX\x22\x3cbr/\x3eclient_secret = \x22YYY\x22\x3cbr/\x3eredirect_uri = \x22http://www.mysite.com/beddit_auth_handler\x22\x3cbr/\x3e\x3cbr/\x3e# 1. Get a temporary code that you can use to get access_token.\x3cbr/\x3e\x3cbr/\x3eauth_url = \x22https://api.beddit.com/api/oauth/authorize?\\\x3cbr/\x3eclient_id=\x22 + client_id + \x22\x26amp;\\\x3cbr/\x3eredirect_uri=\x22 + redirect_uri + \x22\x26amp;\\\x3cbr/\x3eresponse_type=code\x22\x3cbr/\x3e\x3cbr/\x3e# If you are building a web server application, you should redirect the \x3cbr/\x3e# user to the auth_url. If you are doing a desktop/mobile app, you can embed\x3cbr/\x3e# a web browser / or call external web browser, and after the user has\x3cbr/\x3e# given the permission to your app, intercept the page change and read\x3cbr/\x3e# code parameter from new the url.\x3cbr/\x3e\x3cbr/\x3eprint \x22Please open the following url in a web browser:\x22, auth_url\x3cbr/\x3eprint \x22\x22\x3cbr/\x3eprint \x22After giving the permission, copy-paste the contents of \\\x22code\\\x22 parameter in the url\x22\x3cbr/\x3e\x3cbr/\x3ecode = raw_input(\x22code\x26gt;\x22)\x3cbr/\x3e\x3cbr/\x3e# 2. Get the access token that you can use to access users information\x3cbr/\x3e\x3cbr/\x3etoken_url = \x22https://api.beddit.com/api/oauth/access_token?\\\x3cbr/\x3eclient_id=\x22 + client_id + \x22\x26amp;\\\x3cbr/\x3eredirect_uri=\x22 + redirect_uri + \x22\x26amp;\\\x3cbr/\x3eclient_secret=\x22 + client_secret + \x22\x26amp;\\\x3cbr/\x3egrant_type=code\x26amp;\\\x3cbr/\x3ecode=\x22 + code\x3cbr/\x3e\x3cbr/\x3eprint \x22Fetching access token from url\x22, token_url\x3cbr/\x3e\u00a0\u00a0\u00a0 \x3cbr/\x3eresponse = get_json(token_url)\x3cbr/\x3e\x3cbr/\x3eif response.has_key(\x22access_token\x22):\x3cbr/\x3e\u00a0\u00a0\u00a0 access_token = response[\x22access_token\x22]\x3cbr/\x3eelse:\x3cbr/\x3e\u00a0\u00a0\u00a0 print \x22An error occurred\x22\x3cbr/\x3e\u00a0\u00a0\u00a0 if response.has_key(\x22error\x22):\x3cbr/\x3e\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 print \x22Error:\x22, response[\x22error\x22]\x3cbr/\x3e\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 print \x22Description:\x22, response[\x22error_description\x22]\x3cbr/\x3e\u00a0\u00a0\u00a0 sys.exit()\x3cbr/\x3e\x3cbr/\x3eprint \x22Access token is:\x22, access_token\x3cbr/\x3e\x3cbr/\x3e# 3. Fetch some sleep data!\x3cbr/\x3e\x3cbr/\x3estart_date = datetime.date.today() - datetime.timedelta(days=3)\x3cbr/\x3e\x3cbr/\x3esleep_url = \x22https://api.beddit.com/api/me/sleep?\\\x3cbr/\x3estart=\x22 + start_date.strftime(\x22%Y-%m-%d\x22) + \x22\x26amp;\\\x3cbr/\x3eaccess_token=\x22 + access_token\x3cbr/\x3e\x3cbr/\x3eprint \x22Fetching sleep information from\x22, sleep_url\x3cbr/\x3e\x3cbr/\x3eresponse = get_json(sleep_url)\x3cbr/\x3e\x3cbr/\x3eif len(response) \x26gt; 0:\x3cbr/\x3e\u00a0\u00a0\u00a0 for night in response:\x3cbr/\x3e\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 print night[\x22date\x22], \x22slept\x22, night[\x22time_sleeping\x22], \x22seconds, heart rate was\x22, night[\x22resting_heart_rate\x22]\x3cbr/\x3eelse:\x3cbr/\x3e\u00a0\u00a0\u00a0 print \x22There was no sleep information for last 3 days\x22\x3cbr/\x3e\x3cbr/\x3e\x3c/p\x3e\x0a\x3c/code\x3e\x3c/p\x3e\x0a \x3c/div\x3e\x0a \x3c/li\x3e\x0a \x0a\x0a \x0a\x0a \x0a\x0a \x0a \x0a \x0a \x0a \x0a\x0a \x0a \x0a \x0a \x3cli class=\x22tumblr_post tumblr_text_post\x22\x3e\x0a \x0a \x3cdiv class=\x22tumblr_title\x22\x3eGet latest heart rate, and doc improvements\x3c/div\x3e\x0a \x0a \x0a \x3cdiv class=\x22tumblr_body\x22\x3e\x0a \x3cp\x3eLatest Beddit update is just installed to our production server, and it comes with an interesting feature: you can now get almost-real-time heart rate events via our api.\x3c/p\x3e\x0a\x3cp\x3eHere\x26#8217;s how it looks like:\x3c/p\x3e\x0a\x3cblockquote\x3e\x0a\x3cp\x3e[\x3c/p\x3e\x0a\x3cp\x3e{\x3c/p\x3e\x0a\x3cp\x3e\u00a0\u00a0 \u00a0\x26#8221;description\x26#8221;: \x26#8220;Persons heart rate is 57.3\x26#8221;,\u00a0\x3c/p\x3e\x0a\x3cp\x3e\u00a0\u00a0 \u00a0\x26#8221;time\x26#8221;: \x26#8220;2010-12-15T22:14:36\x26#8221;,\u00a0\x3c/p\x3e\x0a\x3cp\x3e\u00a0\u00a0 \u00a0\x26#8221;type\x26#8221;: \x26#8220;heart-rate\x26#8221;,\u00a0\x3c/p\x3e\x0a\x3cp\x3e\u00a0\u00a0 \u00a0\x26#8221;value\x26#8221;: 57.299999999999997\x3c/p\x3e\x0a\x3cp\x3e\u00a0\u00a0},\u00a0\x3c/p\x3e\x0a\x3cp\x3e\u00a0\u00a0{\x3c/p\x3e\x0a\x3cp\x3e\u00a0\u00a0 \u00a0\x26#8221;description\x26#8221;: \x26#8220;Persons heart rate is 51.2\x26#8221;,\u00a0\x3c/p\x3e\x0a\x3cp\x3e\u00a0\u00a0 \u00a0\x26#8221;time\x26#8221;: \x26#8220;2010-12-15T22:14:47\x26#8221;,\u00a0\x3c/p\x3e\x0a\x3cp\x3e\u00a0\u00a0 \u00a0\x26#8221;type\x26#8221;: \x26#8220;heart-rate\x26#8221;,\u00a0\x3c/p\x3e\x0a\x3cp\x3e\u00a0\u00a0 \u00a0\x26#8221;value\x26#8221;: 51.200000000000003\x3c/p\x3e\x0a\x3cp\x3e\u00a0\u00a0},\x3c/p\x3e\x0a\x3cp\x3e\x26#8230;\x3c/p\x3e\x0a\x3cp\x3e]\x3c/p\x3e\x0a\x3c/blockquote\x3e\x0a\x3cp\x3eGetting heart rate results works the same way as bed exit and bed entering events. The\u00a0/api/me/events\u00a0was divided to /api/me/events/presence and /api/me/events/heart_rate resources.\u00a0The behaviour of /api/events resource was also changed a bit. As you might expect, it now returns both events. Also, to limit the amount of data, it now only returns data from last two hours by default.\x3c/p\x3e\x0a\x3cp\x3eHow real-time is it? The Beddit architecture does not currently give any strict guarantees on how timely the data is. There are delays that come from transferring data to servers, and processing the data with our algorithms. However, in most cases, the data should be transferred and analyzed within a minute or two.\x3c/p\x3e\x0a\x3cp\x3eWhile you can always get the latest events by periodically requesting the events resources, this is less than optimal. In future, we hope to support something like HTML5 Server-Sent Events (see\u00a0\x3ca href=\x22http://dev.w3.org/html5/eventsource/\x22\x3e\x3ca href=\x22http://dev.w3.org/html5/eventsource/\x22\x3ehttp://dev.w3.org/html5/eventsource/\x3c/a\x3e\x3c/a\x3e). That way, the server could push updates to clients, which can also be written easily in JavaScript (or any other programming language, of course).\x3c/p\x3e\x0a\x3cp\x3eIf you have any thoughts on that, please drop me a note (mikko@beddit.com).\x3c/p\x3e\x0a\x3cp\x3eP.S.\u00a0We also added some api documentation and examples of returned data, please see the link \x26#8220;API\x26#8221; inside the service, in page footer.\x3c/p\x3e\x0a \x3c/div\x3e\x0a \x3c/li\x3e\x0a \x0a\x0a \x0a\x0a \x0a\x0a \x0a \x0a \x0a \x0a \x0a\x0a \x0a \x0a \x0a \x3cli class=\x22tumblr_post tumblr_text_post\x22\x3e\x0a \x0a \x3cdiv class=\x22tumblr_title\x22\x3eHello world, we have an API\x3c/div\x3e\x0a \x0a \x0a \x3cdiv class=\x22tumblr_body\x22\x3e\x0a \x3cp\x3eWith this first post in our developer blog, I\x26#8217;m happy to announce that the\u00a0\x3ca title=\x22Beddit website\x22 href=\x22http://www.beddit.com\x22\x3eBeddit.com\x3c/a\x3e API is now available!\x3c/p\x3e\x0a\x3cp\x3e\x3cimg src=\x22http://media.tumblr.com/tumblr_lcuw6dzsvt1qbcsgv.png\x22/\x3e\x3c/p\x3e\x0a\x3cp\x3eThe API lets you access the sleep information in Beddit service in machine-readable way. The API is freely available for non-commercial use.\x3c/p\x3e\x0a\x3cp\x3e\x3cstrong\x3eSleep information and some events\x3c/strong\x3e\x3c/p\x3e\x0a\x3cp\x3eRight now, you can basically get a list of measured nights together with some summary numbers about your sleep. For any night, you can also request detailed sleep information including times of different sleep stages, and heart rate.\x3c/p\x3e\x0a\x3cp\x3eIn addition to sleep information, you can read bed exit and bed enter events. These are generated when the user goes to bed and when she leaves the bed.\x3c/p\x3e\x0a\x3cp\x3eAs the service evolves, we can add more information to the API, and also support write access where appropriate.\x3c/p\x3e\x0a\x3cp\x3e\x3cstrong\x3eOAuth2 for authentication\x3c/strong\x3e\x3c/p\x3e\x0a\x3cp\x3eWe choose to use \x3ca href=\x22http://oauth.net/2/\x22\x3eOAuth2 protocol\x3c/a\x3e for authentication in the API. That way, the user does not have to share her password with third-party services. Instead,\u00a0the idea is that you use an\u00a0\x3cem\x3eaccess token\x3c/em\x3e to authenticate your requests.\x3c/p\x3e\x0a\x3cp\x3eTo get an access token, you have to direct the user to an authorization page, where the user can sign in to the service (if she is not already signed in) and choose whether she grants your application access to her data.\x3c/p\x3e\x0a\x3cp\x3e\x3cstrong\x3eFeedback?\x3c/strong\x3e\x3c/p\x3e\x0a\x3cp\x3eIf you have feedback or questions about the API, you reach us at \x3ca href=\x22mailto:hello@beddit.com\x22\x3ehello@beddit.com\x3c/a\x3e, or you can post comments here so that others can see them, too.\x3c/p\x3e\x0a\x3cp\x3eMore detailed information on how to use the API is found inside the Beddit service, check \x26#8220;API\x26#8221; link in the footer of the page.\x3c/p\x3e\x0a \x3c/div\x3e\x0a \x3c/li\x3e\x0a \x0a\x0a \x0a\x0a \x0a\x0a \x0a \x0a \x0a \x0a \x0a\x0a \x0a \x0a\x3c/ol\x3e');