REST API

fitcypher

We will use Django Rest Framework as the API framework to interact with this model.

Using API’s allows to either add entries in bulk, allow users to export and import from other systems, allow for automation and scheduled data syncing.

To make the API friendly to use we’ll provide an API UI :

alt text

Rest API Authentication

For now we will just use basic authentication, username and password, however in the future we’ll implement some better authentication methods like token authentication.

FitCypher API Curl Commands

GET

Curl Command :

curl - X GET http: //127.0.0.1:8000/api/entries/ -u your_username:your_password

JSON Response :

    [{
        "id": 1,
        "user": {
            "id": 1,
            "username": "alex"
        },
        "date": "2025-02-02T07:51:00Z",
        "tracking": "Food",
        "string_value": "Apple",
        "numerical_value": null,
        "notes": "",
        "tags": "",
        "source": "FitCypher"
    }, {
        "id": 2,
        "user": {
            "id": 1,
            "username": "alex"
        },
        "date": "2025-02-02T07:52:00Z",
        "tracking": "Exercise",
        "string_value": "Push Ups",
        "numerical_value": "10.00",
        "notes": "",
        "tags": "",
        "source": "FitCypher"
    }]

If you install JQ you can also get pretty printed output via :

curl -s -X GET http://127.0.0.1:8000/api/entries/ -u your_username:your_password | jq
[
  {
    "id": 1,
    "user": {
      "id": 1,
      "username": "alex"
    },
    "date": "2025-02-02T07:51:00Z",
    "tracking": "Food",
    "string_value": "Apple",
    "numerical_value": null,
    "notes": "",
    "tags": "",
    "source": "FitCypher"
  },
  {
    "id": 2,
    "user": {
      "id": 1,
      "username": "alex"
    },
    "date": "2025-02-02T07:52:00Z",
    "tracking": "Exercise",
    "string_value": "Push Ups",
    "numerical_value": "10.00",
    "notes": "",
    "tags": "",
    "source": "FitCypher"
  },
  {
    "id": 3,
    "user": {
      "id": 1,
      "username": "alex"
    },
    "date": "2023-10-15T12:00:00Z",
    "tracking": "weight",
    "string_value": null,
    "numerical_value": "70.50",
    "notes": "After breakfast",
    "tags": "morning",
    "source": "fitcypher"
  }
]

POST

Add a new entry via the API

curl -X POST http://127.0.0.1:8000/api/entries/ \
-H "Content-Type: application/json" \
-u your_username:your_password \
-d '{
    "date": "2023-10-15T12:00:00Z",
    "tracking": "weight",
    "numerical_value": 70.5,
    "notes": "After breakfast",
    "tags": "morning",
    "source": "fitcypher"
}'

Add a new entry via the API to the PythonAnywhere hosted instance :

curl -X POST https://alexlaverty.pythonanywhere.com/api/entries/ \
-H "Content-Type: application/json" \
-u your_username:your_password \
-d '{
    "date": "2023-10-15T12:00:00Z",
    "tracking": "food",
    "string_value": "Banana",
    "numerical_value": "",
    "notes": "",
    "tags": "",
    "source": "fitcypher"
}'

Try out FitCypher here :

https://alexlaverty.pythonanywhere.com/

Or git clone the FitCypher github repo and run it locally :

https://github.com/alexlaverty/fitcypher