Skip to content

Beaver Habit Tracker API How‐to Guide

Henry Zhu edited this page Dec 15, 2024 · 2 revisions

About this API

The Beaver Habit Tracker API gives you the ability to create, update, list and complete habits with ease.

Please check out this Swagger UI documentation for all the APIs.

Tutorial

Postman Collection containing the HTTP requests used in this walkthrough is provided in this gist.

Task 1 – Obtain an Access Token

This task produces an access token with your username and password:

# request 
curl --request POST \
  --url {{endpoint}}/auth/login \
  --header 'accept: application/json' \
  --header 'content-type: application/x-www-form-urlencoded' \
  --data grant_type=password \
  --data 'username={{username}}' \
  --data 'password={{password}}'

# response
{
  "access_token": "<token>",
  "token_type": "bearer"
}

Task2 - List All the Habits

# request
curl --request GET \
  --url {{endpoint}}/api/v1/habits \
  --header 'accept: application/json' \
  --header 'authorization: Bearer {{accessToken}}'

# response
[
  {
    "id": "f41e44",
    "name": "Order pizz"
  },
  {
    "id": "87e537",
    "name": "Running"
  },
  ...
]

Task3 - Complete Habit

# request
curl --request POST \
  --url {{endpoint}}/api/v1/habits/f41e44/completions \
  --header 'authorization: Bearer {{accessToken}}' \
  --header 'content-type: application/json' \
  --data '{
  "date_fmt": "%d-%m-%Y",
  "date": "16-12-2024",
  "done": true
}'

# response
{
  "day": "16-12-2024",
  "done": true
}

Task4 - Show Recent Habit Completions

# request
curl --location '{{endpoint}}/api/v1/habits/f41e44/completions?date_fmt=%d-%m-%Y&date_start=01-12-2024&date_end=30-12-2024&sort=asc' \
--header 'accept: application/json' \
--header 'Authorization: Bearer {{accessToken}}'

# response
[
  "10-12-2024",
  "12-12-2024",
  "16-12-2024"
]