Building a Digital Habit Tracker: Advanced API Authentication and Requests
Advanced authentication methods and different types of HTTP requests are essential skills for any modern developer. Today, we’ll explore how to leverage these techniques to build a practical digital habit tracking application.
Understanding HTTP Request Types
The Requests module in Python allows us to make several types of HTTP requests:
- GET: Retrieves data from an external system
- POST: Sends data to an external system
- PUT: Updates existing data in an external service
- DELETE: Removes data from an external service
Each request type serves a specific purpose in API interactions, and today we’ll implement all four types in our habit tracker.
The Pixela API
Pixela is a Japanese-developed API that allows users to track habits visually through a graph. What makes it unique is that it not only shows which days you’ve maintained your habit but also displays intensity through color variations. For example, if tracking reading habits, darker pixels might represent more pages read that day.
Setting Up a User Account
The first step is creating a user account with Pixela using a POST request:
import requests PIXELA_ENDPOINT = "https://pixe.la/v1/users" user_params = { "token": "your_secret_token", # Create your own secure token "username": "your_username", # Choose a unique username "agreeTermsOfService": "yes", "notMinor": "yes" } response = requests.post(url=PIXELA_ENDPOINT, json=user_params) print(response.text) # Check if successful
This creates your account on Pixela with your chosen username and token, which functions as your password for future interactions.
Creating a Habit Graph
After setting up an account, we need to create a graph to track our habit. This requires a more secure authentication method using request headers:
USERNAME = "your_username" TOKEN = "your_secret_token" GRAPH_ID = "graph1" graph_endpoint = f"{PIXELA_ENDPOINT}/{USERNAME}/graphs" graph_config = { "id": GRAPH_ID, "name": "My Cycling Graph", "unit": "kilometers", "type": "float", "color": "ajisai" # Purple } headers = { "X-USER-TOKEN": TOKEN } response = requests.post(url=graph_endpoint, json=graph_config, headers=headers) print(response.text)
The key difference here is the use of headers for authentication rather than including the token in the request body. This provides better security by preventing the token from appearing in logs or being visible to potential request sniffers.
Posting Data to Your Graph
Now for the fun part – adding actual data to track your habit:
from datetime import datetime pixel_creation_endpoint = f"{PIXELA_ENDPOINT}/{USERNAME}/graphs/{GRAPH_ID}" today = datetime.now() formatted_date = today.strftime("%Y%m%d") pixel_data = { "date": formatted_date, "quantity": "9.74" # How many kilometers cycled today } response = requests.post(url=pixel_creation_endpoint, json=pixel_data, headers=headers) print(response.text)
By using the strftime()
method, we can format dates exactly as required by the API. This allows us to post data for the current day automatically.
Updating and Deleting Data
Sometimes we need to correct or remove data. We can update a pixel using a PUT request:
update_endpoint = f"{PIXELA_ENDPOINT}/{USERNAME}/graphs/{GRAPH_ID}/{formatted_date}" new_pixel_data = { "quantity": "4.5" # Corrected value } response = requests.put(url=update_endpoint, json=new_pixel_data, headers=headers) print(response.text)
And to delete a data point, we use a DELETE request:
delete_endpoint = f"{PIXELA_ENDPOINT}/{USERNAME}/graphs/{GRAPH_ID}/{formatted_date}" response = requests.delete(url=delete_endpoint, headers=headers) print(response.text)
These methods give you complete control over your habit tracking data.
Practical Applications
This habit tracker can be adapted to monitor any habit you’re trying to build:
- Reading pages per day
- Minutes spent coding
- Meditation duration
- Exercise metrics
- Water consumption
The visual nature of the pixel graph makes it particularly effective as it creates a strong motivation to maintain streaks and see progress over time.
Conclusion
Building a habit tracker with the Pixela API demonstrates several important concepts in modern web development: making different types of HTTP requests, securing API interactions with headers, and working with date formatting. These skills are transferable to countless other projects and APIs.
By incorporating technology into habit building, we create not just a record of our activities but also a visual incentive system that helps maintain momentum toward our goals.