Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

APIs

Michael Williams
PLUS
Michael Williams
Courses Plus Student 8,059 Points

Tips on working with the FitBit API?

I have finished the AJAX basics course, and I want to text my knowledge by creating a simple experiment by creating a simple app in workspaces that pulls a user's step count and then logs a message to the console when a goal has been reached.

However, I feel overwhelmed over at dev.fitbit.com. There's a simulator (so I can try things without buying the devices), their studio to play around with code, and some code I've never seen before in my life.

The information seems a bit more complicated than the simple Flickr app we built with Guil.

Any pointers?

3 Answers

Eric M
Eric M
11,545 Points

Hi Michael,

Hitting a different API and doing something with the data is a great idea for a project to test and expand your skills!

The Fitbit API is doable, but it's going to require you to learn quite a lot of new things and might take longer than you estimated. If you do go down this path though it will be well worth it, as the things you will learn will be useful for most modern Web APIs!

Before even looking at the fitbit API specifically. Let me say that my biggest tip is to not get discouraged. Building software applications can regularly involve diving into systems that other people have written and making sense of them. Whether the issue is unfamiliar design patterns, incomplete documentation, staggeringly bad code that your surprised works, or staggeringly complex code that looks like you need a math PHD to understand it - you're going to have go on a journey though it, poke and prod it, before you start to understand it enough to work with it. If you can come to enjoy this process you'll be a much more versatile programmer.

I often have two projects or IDEs open at once. One where I'm building the overall application by writing tests and then building out the program to pass them, and another where whenever I hit something I'm unfamiliar with, I write a tiny script to test it out. I also strongly recommend a full blown IDE over workspaces as you start to do your own, more complex projects. I'm a big fan of the JetBrains IDEs (specifically PyCharm for Python and IntelliJ for Rust) and VS Code (for JavaScript). Visual Studio Community (different to VS Code) is also quite good although I've only used it for C# and C. People say good things about Sublime Text as well. It may take a while to get your first IDE working the way you like, but it will pay off. I guess that's my second tip!

Then just start playing with the Web API. Hit an endpoint and see what it comes back with. Can you use the returned payload in your application?

As I suspect you've already realised, for Fitbit this requires a bit of setup. You need to register your application with Fitbit and create an account - which will get you a secret code you can use to identify your app requests as coming from your registered application. As an example, the Google Maps API works the same way.

The Authorization Code Grant Flow is a way for a fitbit user to log your application into fitbit on their behalf and then draw down their data via your API. If you're doing all of this in client side JavaScript you'll need to use the Implicit grant flow instead. Either way, if there's a way to use a "dummy" authorisation for development you should do that and skip this. If not you'll need to allocate some time to learning about OAuth2.0 - it's a standard for these sorts of Authorisations so it's not wasted time, but I would consider it an advanced topic.

Luckily fitbit have an interactive tool that let's you see how their server will respond at each point of the OAuth flow! Check out https://dev.fitbit.com/apps/oauthinteractivetutorial. When going from this tutorial to actual code you'll need to send all of that data to fitbit using your language's OAuth library, process the fitbit response, take the part out of it you need (as described on that page) and use the token to then make your API requests (e.g. to get steps).

If you were looking for a project to complete in a couple of hours, I would shelve this for later and work on something else. Maybe hitting a different part of the Flicker API and doing something else with the data. If you have the time though, this will teach you a lot.

Best of luck regardless on where your next steps take you.

Cheers,

Eric

Michael Williams
PLUS
Michael Williams
Courses Plus Student 8,059 Points

Eric McKibbin, first off, thank you for such a thoughtful answer. Very helpful and insightful. However, it has left me with a lot more questions.

  1. I want to keep on track to learning how to work with the FitBit API, but at the same time, I want to keep honing the skills of working with APIs and do something a little more complicated than what we did with the Flickr API. Any thoughts on how to accomplish those two goals simultaneously?

  2. Do you know of any resources that help a newbie like me transition from course work like what's on Team Treehouse to working with something more complicated like the FitBit API?

  3. I see that OAuth 2.0 is listed in some of the iOS courses, like building an app that searches the iTunes store and a Yelp Review app, but I don't see any courses geared toward just OAuth 2.0 (to be honest, I don't know what it is).

  4. Lastly, do you know of any resources that would help me understand in simple terms how to set up a local server (host it locally?) to test out my API work? I was doing a LinkedIn Learning course on AJAX, and they showed us how to set up an HTTP server on my computer. I use http-server in the terminal that runs under node, which I got the GUI installer from nodejs.org (and honestly, I have no clue what half that stuff is that I just typed in that last sentence). The video told me exactly what to type to get it running, and it worked, but I want to understand what I did/was doing. Any help there?

I spent the past four months or so learning JavaScript and going through the Front End Development course, but I kinda feel like I'm back at square one now that I'm trying to do stuff with the Fitbit API.

Eric M
Eric M
11,545 Points

Hi Michael,

Good questions! I think you're thinking about your learning well and it's great that you're looking around for other resources and trying to get a good understanding of things. I think you'll do well if this is your process.

Just quickly OAuth is a framework or set of standards for a problem domain known as IAM (Identity and Access Management). Lots of companies choose to use these standards when setting up systems to allow third party applications to work with their systems. There is heaps out there on this domain, and OAuth, and I'm no expert - so I'll leave that there.

In terms of how to go from where you are to where you want to be with respect to understanding servers and APIs there are actually some great resources on Treehouse that will give you a leg up here. Node.js is a runtime for JavaScript, and you can use it to write a webserver in JavaScript. So many people use Node for running a webserver that there's a popular framework to simplify this called Express.

Treehouse has a Node.js Basics Course, an Express Basics Course, and courses on User Authentication with Express and using OAuth and Passport with Express. I also recommend going through the Mongo course a couple of those list as prerequisites.

To top it all off there's a course on Building your own REST API using Node and Express after which you'll find it much easier to read other people's API docs and contextualise what's happening.

As you've already been using JavaScript for front end you'll mostly be learning new concepts and frameworks, not an entirely new syntax, which should help a little. If you find you have some gaps while going through these courses check out the Full Stack JavaScript track for some courses that are great along side these.

Cheers,

Eric

Michael Williams
PLUS
Michael Williams
Courses Plus Student 8,059 Points

Hi Eric:

Thanks! I'm going to use this response as my syllabus. It's funny you mentioned the Node.js course. After some poking around, that's where I decided to take my learning next. Sounds like I have my work cut out for me, but I have all the knowledge I need at my fingertips.

Thank you for your help.

Michael