- Development:
http://localhost:8000 - Production:
https://your-domain.com
FitHub API supports multiple authentication methods with JWT as the primary method for API clients:
The API implements a multi-method authentication strategy to support different client types:
- JWT Authentication (Primary): Stateless, scalable for API clients
- Session Authentication: For web browser clients with CSRF protection
- Token Authentication: Simple DRF tokens for server-to-server communication
- Access Token Lifetime: 60 minutes
- Refresh Token Lifetime: 7 days
- Token Rotation: Enabled for security
- Blacklist: Refresh tokens are blacklisted after rotation
- Algorithm: HS256 with project secret key
Authorization: Bearer your-jwt-token-hereGetting JWT Tokens:
curl -X POST http://localhost:8000/api/auth/jwt/token/ \
-H "Content-Type: application/json" \
-d '{"username": "your-username", "password": "your-password"}'Response:
{
"access": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9...",
"refresh": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9..."
}Refreshing JWT Token:
curl -X POST http://localhost:8000/api/auth/jwt/token/refresh/ \
-H "Content-Type: application/json" \
-d '{"refresh": "your-refresh-token"}'Authorization: Token your-token-hereGetting a Token:
curl -X POST http://localhost:8000/api/auth/token/ \
-H "Content-Type: application/json" \
-d '{"username": "your-username", "password": "your-password"}'Response:
{
"token": "your-authentication-token",
"user_id": 1,
"username": "your-username",
"email": "[email protected]",
"is_staff": false,
"is_superuser": false
}Include CSRF token in requests:
X-CSRFToken: your-csrf-token-here| Endpoint | Method | Description |
|---|---|---|
/diets/ |
GET, POST | List/create diets |
/diets/{id}/ |
GET, PUT, PATCH, DELETE | Diet details |
/diets/{id}/activate/ |
POST | Activate diet |
/diets/active/ |
GET | Get active diet |
/meals/ |
GET, POST | List/create meals |
/meals/{id}/ |
GET, PUT, PATCH, DELETE | Meal details |
/meals/{id}/ingredients/ |
GET | Get meal ingredients |
/meals/{id}/add_ingredient/ |
POST | Add ingredient to meal |
/meals/{id}/nutrition_summary/ |
GET | Get meal nutrition |
/ingredients/ |
GET, POST | List/create ingredients |
/ingredients/{id}/ |
GET, PUT, PATCH, DELETE | Ingredient details |
/ingredients/search/ |
GET | Search ingredients |
/ingredients/personal/ |
GET | Get personal ingredients |
/categories/ |
GET, POST | List/create categories |
/meal-records/ |
GET, POST | List/create meal records |
/meal-records/today/ |
GET | Get today's records |
/meal-records/nutrition_summary/ |
GET | Get nutrition summary |
/meal-preferences/ |
GET, POST | List/create preferences |
/meal-preferences/by_type/ |
GET | Get preferences by type |
/meal-ingredients/ |
GET, POST | List/create meal ingredients |
| Endpoint | Method | Description |
|---|---|---|
/goals/ |
GET, POST | List/create goals |
/goals/{id}/ |
GET, PUT, PATCH, DELETE | Goal details |
/goals/{id}/activate/ |
POST | Activate goal |
/goals/{id}/deactivate/ |
POST | Deactivate goal |
/goals/active/ |
GET | Get active goals |
/goals/upcoming/ |
GET | Get upcoming goals |
/goals/overdue/ |
GET | Get overdue goals |
/goals/{id}/progress/ |
GET | Get goal progress |
/body-measurements/ |
GET, POST | List/create measurements |
/body-measurements/{id}/ |
GET, PUT, PATCH, DELETE | Measurement details |
/body-measurements/latest/ |
GET | Get latest measurements |
/body-measurements/trends/ |
GET | Get measurement trends |
/body-measurements/summary/ |
GET | Get measurement summary |
/body-measurements/bulk_create/ |
POST | Create multiple measurements |
POST /api/nutrition/diets/
Content-Type: application/json
Authorization: Token your-token-here
{
"name": "Weight Loss Diet",
"description": "Calorie deficit diet for weight loss",
"start_date": "2025-01-01",
"end_date": "2025-03-31"
}Response:
{
"id": 1,
"name": "Weight Loss Diet",
"description": "Calorie deficit diet for weight loss",
"user": 1,
"start_date": "2025-01-01",
"end_date": "2025-03-31",
"is_active": true,
"created_at": "2025-01-01T00:00:00Z",
"updated_at": "2025-01-01T00:00:00Z"
}GET /api/nutrition/diets/?is_active=true&ordering=-created_at
Authorization: Token your-token-herePOST /api/nutrition/meals/
Content-Type: application/json
Authorization: Token your-token-here
{
"name": "Breakfast Bowl",
"meal_type": "breakfast",
"diet": 1,
"description": "Healthy breakfast with oats and fruits"
}POST /api/nutrition/meals/1/add_ingredient/
Content-Type: application/json
Authorization: Token your-token-here
{
"ingredient_id": 5,
"quantity": 100,
"unit": "g"
}POST /api/nutrition/ingredients/
Content-Type: application/json
Authorization: Token your-token-here
{
"name": "Chicken Breast",
"category": 1,
"calories_per_100g": 165.0,
"protein_per_100g": 31.0,
"carbs_per_100g": 0.0,
"fat_per_100g": 3.6,
"fiber_per_100g": 0.0,
"sugar_per_100g": 0.0,
"sodium_per_100g": 74.0,
"is_personal": false
}GET /api/nutrition/ingredients/search/?q=chicken
Authorization: Token your-token-herePOST /api/nutrition/meal-records/
Content-Type: application/json
Authorization: Token your-token-here
{
"meal": 1,
"timestamp": "2025-01-01T08:00:00Z",
"calories": 450.0
}GET /api/nutrition/meal-records/today/
Authorization: Token your-token-hereGET /api/nutrition/meal-records/nutrition_summary/?days=7
Authorization: Token your-token-hereResponse:
{
"period_days": 7,
"total_calories": 10500.0,
"average_daily_calories": 1500.0,
"record_count": 21,
"calories_by_day": [
{"date": "2025-01-01", "calories": 1500.0},
{"date": "2025-01-02", "calories": 1600.0}
]
}POST /api/goals/goals/
Content-Type: application/json
Authorization: Token your-token-here
{
"title": "Lose 10kg",
"description": "Weight loss goal for summer",
"goal_type": "weight_loss",
"target_value": 70.0,
"start_date": "2025-01-01",
"target_date": "2025-06-01"
}GET /api/goals/goals/1/progress/
Authorization: Token your-token-hereResponse:
{
"goal": {
"id": 1,
"title": "Lose 10kg",
"target_value": 70.0,
"start_date": "2025-01-01",
"target_date": "2025-06-01"
},
"current_value": 75.0,
"progress_percentage": 50.0,
"days_remaining": 90,
"estimated_completion": "2025-04-15"
}POST /api/goals/body-measurements/
Content-Type: application/json
Authorization: Token your-token-here
{
"metric": "weight",
"value": 75.5,
"unit": "kg",
"timestamp": "2025-01-01T08:00:00Z"
}GET /api/goals/body-measurements/latest/
Authorization: Token your-token-hereGET /api/goals/body-measurements/trends/?metric=weight&days=30
Authorization: Token your-token-hereResponse:
{
"metric": "weight",
"unit": "kg",
"period_days": 30,
"measurements": [
{"date": "2025-01-01", "value": 75.5},
{"date": "2025-01-02", "value": 75.2}
],
"trend": "decreasing",
"change": -0.3,
"change_percentage": -0.4
}POST /api/goals/body-measurements/bulk_create/
Content-Type: application/json
Authorization: Token your-token-here
{
"measurements": [
{
"metric": "weight",
"value": 75.5,
"unit": "kg",
"timestamp": "2025-01-01T08:00:00Z"
},
{
"metric": "body_fat",
"value": 15.2,
"unit": "%",
"timestamp": "2025-01-01T08:00:00Z"
}
]
}All list endpoints support filtering by model fields:
# Filter diets by active status
GET /api/nutrition/diets/?is_active=true
# Filter meals by type
GET /api/nutrition/meals/?meal_type=breakfast
# Filter goals by type
GET /api/goals/goals/?goal_type=weight_loss
# Filter measurements by metric
GET /api/goals/body-measurements/?metric=weightSearch endpoints support text search:
# Search ingredients
GET /api/nutrition/ingredients/search/?q=chicken
# Search meals
GET /api/nutrition/meals/?search=breakfast
# Search goals
GET /api/goals/goals/?search=weightAll list endpoints support ordering:
# Order by creation date (newest first)
GET /api/nutrition/diets/?ordering=-created_at
# Order by name (alphabetical)
GET /api/nutrition/meals/?ordering=name
# Order by date (oldest first)
GET /api/goals/body-measurements/?ordering=timestampAll list endpoints are paginated:
# Get first page (default: 20 items)
GET /api/nutrition/diets/
# Get specific page
GET /api/nutrition/diets/?page=2
# Custom page size
GET /api/nutrition/diets/?page_size=50Pagination Response:
{
"count": 100,
"next": "http://localhost:8000/api/nutrition/diets/?page=2",
"previous": null,
"results": [...]
}{
"id": 1,
"name": "string",
"description": "string",
"user": 1,
"start_date": "2025-01-01",
"end_date": "2025-03-31",
"is_active": true,
"created_at": "2025-01-01T00:00:00Z",
"updated_at": "2025-01-01T00:00:00Z"
}{
"id": 1,
"name": "string",
"description": "string",
"meal_type": "breakfast|lunch|dinner|snack",
"diet": 1,
"user": 1,
"created_at": "2025-01-01T00:00:00Z",
"updated_at": "2025-01-01T00:00:00Z"
}{
"id": 1,
"name": "string",
"category": 1,
"calories_per_100g": 165.0,
"protein_per_100g": 31.0,
"carbs_per_100g": 0.0,
"fat_per_100g": 3.6,
"fiber_per_100g": 0.0,
"sugar_per_100g": 0.0,
"sodium_per_100g": 74.0,
"is_personal": false,
"created_at": "2025-01-01T00:00:00Z",
"updated_at": "2025-01-01T00:00:00Z"
}{
"id": 1,
"title": "string",
"description": "string",
"goal_type": "weight_loss|muscle_gain|endurance|strength",
"target_value": 70.0,
"user": 1,
"start_date": "2025-01-01",
"target_date": "2025-06-01",
"is_active": true,
"created_at": "2025-01-01T00:00:00Z",
"updated_at": "2025-01-01T00:00:00Z"
}{
"id": 1,
"metric": "weight|body_fat|muscle_mass|waist|chest|arms|legs",
"value": 75.5,
"unit": "kg|g|lb|%|cm|in",
"user": 1,
"timestamp": "2025-01-01T08:00:00Z",
"created_at": "2025-01-01T00:00:00Z",
"updated_at": "2025-01-01T00:00:00Z"
}{
"field_name": ["Error message"],
"non_field_errors": ["General error message"]
}- 200 OK: Successful GET, PUT, PATCH
- 201 Created: Successful POST
- 204 No Content: Successful DELETE
- 400 Bad Request: Invalid request data
- 401 Unauthorized: Authentication required
- 403 Forbidden: Permission denied
- 404 Not Found: Resource not found
- 500 Internal Server Error: Server error
{
"name": ["This field is required."],
"start_date": ["Date has wrong format. Use one of these formats instead: YYYY-MM-DD."]
}{
"detail": "Authentication credentials were not provided."
}{
"detail": "You do not have permission to perform this action."
}{
"detail": "Not found."
}# Get all diets
curl -H "Authorization: Token your-token-here" \
http://localhost:8000/api/nutrition/diets/
# Create a new diet
curl -X POST \
-H "Authorization: Token your-token-here" \
-H "Content-Type: application/json" \
-d '{"name": "Test Diet", "start_date": "2025-01-01"}' \
http://localhost:8000/api/nutrition/diets/import requests
# Set up authentication
headers = {'Authorization': 'Token your-token-here'}
# Get all diets
response = requests.get('http://localhost:8000/api/nutrition/diets/', headers=headers)
diets = response.json()
# Create a new diet
diet_data = {
'name': 'Test Diet',
'start_date': '2025-01-01'
}
response = requests.post(
'http://localhost:8000/api/nutrition/diets/',
json=diet_data,
headers=headers
)// Set up authentication
const headers = {
'Authorization': 'Token your-token-here',
'Content-Type': 'application/json'
};
// Get all diets
fetch('http://localhost:8000/api/nutrition/diets/', { headers })
.then(response => response.json())
.then(data => console.log(data));
// Create a new diet
const dietData = {
name: 'Test Diet',
start_date: '2025-01-01'
};
fetch('http://localhost:8000/api/nutrition/diets/', {
method: 'POST',
headers,
body: JSON.stringify(dietData)
})
.then(response => response.json())
.then(data => console.log(data));Visit the interactive API documentation:
- Swagger UI:
http://localhost:8000/api/docs/ - ReDoc:
http://localhost:8000/api/redoc/ - OpenAPI Schema:
http://localhost:8000/api/schema/