Fitbit subscription create and remove using java code

1) Register a Fitbit account.
2) Now you need to create a demo app on Fitbit. For that, you just need to login (
3) Register the application through this URL. Kindly find below the screenshot for reference.
4) For subscription, you need to click onAdd a subscriberbutton with a plus sign. Here it will ask you some information, includes:
  • Endpoint URL (Basically you have to put rest service URL which will be verified later) example:
  • Type: You can prefer any type from the dropdown but I used “jsonbody” in my project
  • subscribed ( You should add unique ID)
5) Click on the checkbox to accept the Terms and Conditions.
6) Now you can click the register button.
Step 2:
In the second step, You have to verify the subscriberId which you created in step1.
1) Now as you can see verify button and verification code on the bottom of the above screenshot.
2) Basically, you need to copy that key from here and store it somewhere which we will use in our next step.
3) Now you need to create 2 REST API’s with the same URL which we mentioned above in Step 1 as Endpoint URL.
4) Both URLs will have different request methods like @GET and @POST. Here GET and POST both methods will get a call whenever we will click the verify button but skip this step for now.
public Response getFitBitNotification(@QueryParam("verify") String verify) throws   DemoException {"<------ inside fitbit notification verify ------>");
{"<------ fitbit subscription verify success------>");
return Response.status(Status.NO_CONTENT).build();
}"<------ fitbit subscription verify not applied------>");
return Response.status(Status.NOT_FOUND).build();
You need to put your fitBitEndPointVerificationCode which you copied from Fitbit verification screen. Basically, this method will have a “no content” status code.
public Response getFitBitNotificationData(@Context HttpServletRequest request,List<FitBitNotificationModel> notificationList) throws MangoMirrorException 
{"<------inside fitbit notification data ------>");
 ExecutorService executor = Executors.newFixedThreadPool(2);
 Runnable taskOne = new FitbitDataThread(notificationList);
 return Response.status(Status.NO_CONTENT).build();
This POST method will be used to get continuous updates from Fitbit, if any update has been done on Fitbit. So this method will start working when we create a subscription for a particular user. Here in the above method, I used FitBitNotificationModel to receive Fitbit data in a custom model.
public class FitBitNotificationModel {
    private String collectionType;
    private String ownerId;
    private String ownerType;
    private String subscriberId;
    private String subscriptionId;
    private String date;

//setter and getter of the above variable
Now you can click on the verify button on Fitbit to verify your subscriber Id. You can cross-check it on the same page if the verify button gets removed and verified time will appear.
Step 3 :
In this step, we will see how we can create a subscription on Fitbit for a particular user account. So we can get the data updates asynchronously from the specific user account.
Here we have to create a unique subscription for every user but for that, we need users to access token.
Void createSubscription(String FitbitAccessToken)
HttpHeaders headers = new HttpHeaders();
headers.set("X-Fitbit-Subscriber-Id", fitBitSubscriberId);
headers.set("Authorization", "Bearer  " +FitbitAccessToken);
HttpEntity<String> entity = new HttpEntity<>(headers);
String subscriptionUrl = “”, HttpMethod.POST, entity, String.class);
In the above code, I used “1.json” in subscriptionUrl so 1 is actually a unique subscriptionId for that account.
Here you need to put your fitBitSubscriberId which you created in the first step. FitbitAccessToken you can get from that user’s account. You can use the above function for creating a subscription.
Whenever data or profile change happens for subscribed accounts, then the below method will be called automatically and from there you can see which category of data is modified. (POST) created in step2.
I have used subscriptionURL in the above method. That can be used in different ways as per your requirement.
Here in 1st URL, we didn’t mention any particular category for subscription, so we will get a notification if any data is modified within these (activities, foods, sleep, body) categories. (if you want to create a subscription for a particular category in that case you can use other URL’s)
Step 4:
You can’t use the same subscription for multiple users’ accounts neither multiple subscriptionId for the same account. If you try to do that, in that case, fitbit server will throw ERROR Code 409 Conflict. But if you want to use the same Fitbit account with other subscriptionId in that case you need to first unsubscribe existing subscriptionId then you can create a new subscription for that account. You can use below sample code to unsubscribe existing subscriptionId.
Void removeSubscription(String FitbitAccessToken)
HttpHeaders headers = new HttpHeaders();
headers.set("X-Fitbit-Subscriber-Id", fitBitSubscriberId);
headers.set("Authorization", "Bearer  " +FitbitAccessToken);
HttpEntity<String> entity = new HttpEntity<>(headers);
String subscriptionUrl = “”, HttpMethod.DELETE, entity, String.class);
In my next blog, I will explain how we can fetch data from Fitbit after getting a notification. I hope that this in-depth research blog can accomplish all your queries.
For subscription related details you can check their official doc on below link.
If without creating any application want to check their api’s and its response you can use below link

About Author

Gaurav Tripathi is a full stack developer with over 4+ years of experience with excellent knowledge in Java (Core+Advanced), Spring Boot, Angular JS and AWS Cloud services. He is very dedicated towards his work and that’s the reason he is always trying to learn new technologies to update himself. He loves to help people solving their queries in the technical aspects.
Related Posts

Leave a Comment