Swap a recipe in your meal plan with a similar alternative that matches your dietary preferences and nutritional goals.
Authentication
This endpoint requires Bearer token authentication. Include your access token in the Authorization header:
Authorization: Bearer <your_access_token>
Request
Query Parameters
The external recipe ID of the meal you want to swap. This is the recipe_id field from the recipe object, not the database id.
The meal type for which you want a replacement. Must be one of:
breakfast
lunch
dinner
snack
The number of similar recipes to search through before filtering. Higher values increase the likelihood of finding a good match but may slow down the response. Range: 1-1000
Response
Returns a recipe object that is nutritionally similar to the original but matches your dietary requirements.
Database ID of the recommended recipe
External recipe ID from the dataset
Name of the recipe Example: "Mediterranean Chicken Bowl"
Total calories per serving Example: 480
Protein content in grams Example: 35
Carbohydrate content in grams Example: 45
Fat content in grams Example: 18
URL to the recipe image Example: "https://example.com/recipes/mediterranean-bowl.jpg"
URL to the full recipe details and instructions Example: "https://example.com/recipes/mediterranean-bowl"
Comma-separated list of ingredients Example: "chicken breast, quinoa, cherry tomatoes, cucumber, feta cheese, olive oil, lemon"
Array of meal type objects indicating when this recipe is suitable Meal type name (breakfast, lunch, dinner, snack)
Array of diet type objects this recipe belongs to Diet type name (high_protein, vegan, vegetarian, low_carb, etc.)
The meal label that was requested in the swap Example: "dinner"
Similarity score from the ML model. Lower values indicate higher similarity to the original recipe. Example: 0.234
Example Request
curl -X POST 'https://api.smarteat.ai/api/ml/swap-recipe?recipe_id=12345&meal_label=dinner&n_search=550' \
-H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
import requests
url = "https://api.smarteat.ai/api/ml/swap-recipe"
headers = {
"Authorization" : "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
}
params = {
"recipe_id" : 12345 ,
"meal_label" : "dinner" ,
"n_search" : 550
}
response = requests.post(url, headers = headers, params = params)
print (response.json())
const params = new URLSearchParams ({
recipe_id: '12345' ,
meal_label: 'dinner' ,
n_search: '550'
});
const response = await fetch ( `https://api.smarteat.ai/api/ml/swap-recipe? ${ params } ` , {
method: 'POST' ,
headers: {
'Authorization' : 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...'
}
});
const data = await response . json ();
console . log ( data );
Example Response
{
"id" : 847 ,
"recipe_id" : 67890 ,
"name" : "Mediterranean Chicken Bowl" ,
"calories" : 480 ,
"protein" : 35 ,
"carbs" : 45 ,
"fat" : 18 ,
"image_url" : "https://example.com/recipes/mediterranean-bowl.jpg" ,
"recipe_url" : "https://example.com/recipes/mediterranean-bowl" ,
"ingredients" : "chicken breast, quinoa, cherry tomatoes, cucumber, feta cheese, olive oil, lemon, red onion, kalamata olives" ,
"meal_types" : [
{
"id" : 2 ,
"name" : "lunch"
},
{
"id" : 3 ,
"name" : "dinner"
}
],
"diet_types" : [
{
"id" : 1 ,
"name" : "high_protein"
},
{
"id" : 5 ,
"name" : "low_calorie"
}
],
"assigned_meal_type" : "dinner" ,
"distance" : 0.187
}
{
"detail" : "No similar recipe found for this meal type"
}
{
"detail" : "Invalid token payload"
}
How It Works
Load User Profile : Retrieves your dietary preferences and restrictions
Get Active Plan : Checks your current meal plan to avoid duplicate recipes
Find Similar Recipes : Uses K-Nearest Neighbors ML model to find recipes with similar nutritional profiles
Apply Filters :
Excludes recipes already in your meal plan
Filters by the requested meal type (breakfast, lunch, dinner, snack)
Ensures compatibility with your diet types (e.g., vegan, high-protein)
Calculate Distance : Measures similarity in scaled nutritional feature space
Random Selection : If multiple good matches exist, randomly selects one for variety
Notes
The algorithm searches through up to n_search neighbors (default 550)
Recipes already in your active meal plan are automatically excluded
The swap respects your dietary restrictions from your profile
Lower distance values indicate closer nutritional similarity
If no suitable match is found, a 404 error is returned
The ML model uses standardized scaling for fair comparison across nutritional metrics
Diet type filtering ensures recommendations align with your preferences (e.g., vegan users only get vegan recipes)
Tips for Best Results
Use higher n_search values (700-1000) if you have strict dietary restrictions
Use lower n_search values (300-400) for faster responses when flexibility is acceptable
Ensure your profile has diet types set for more accurate filtering
The distance metric helps you understand how similar the swap is nutritionally