Meal Plan

The 'meal-plan' endpoint allows you to generate a personalized meal plan based on specific criteria and constraints. Customers have the choice of receiving the response as JSON or as a readable stream.

Introduction

This API route allows you to generate a personalized meal plan based on specific criteria and constraints. It provides two endpoints, each with a different response format:

  • JSON Route: Returns the Meal Plan details as JSON.
  • Readable Stream Route: Streams the Meal Plan details in a readable stream.

Authentication

To access these endpoints, you must include an Authorization header with a valid API key.

Common Request Parameters

Both routes accept the following request parameters:

  • goal (enum, required): The meal plan goal, ['lose-weight', 'gain-muscles', 'eat-healthy']
  • duration (number, required): The number of days for the meal plan.
  • activityLevel (enum, required): The user's daily activity level ['sedentary', 'light-active', 'moderately-active', 'very-active', 'extremely-active'].
  • age (number, required): The user's age.
  • height (number, required): The user's height in centimeters.
  • weight (number, required): The user's weight in kilograms.
  • gender (string, required): The user's gender ['male', 'female', 'undefined']
  • diet (string, required): The dietary preference ['vegetarian','pescatarian','vegan','diaryfree','glutenfree','keto','paleo'].
  • measurement (string, required): The measurement system used in the output ['imperial', 'metric']. Default: Metric.
  • language (string, optional): The language in which the recipe should be generated. Supported languages ['english', 'spanish', 'portuguese', 'french', 'italian', 'german', 'chinese', 'japanese', 'korean']. Default: "english".

JSON Route Details

Endpoint

POST /api/generate/meal-plan

Request Example

POST /api/generate/meal-plan
Headers:
{
  "Authorization": "YOUR_API_KEY"
}
Body:
{
  "goal": "lose-weight",
  "duration": 30,
  "activityLevel": "moderately-active",
  "age": 30,
  "height": 175,
  "weight": 70,
  "gender": "male",
  "diet": "vegetarian"
}

Response (JSON)

  • Status Code: 200 (OK)
  • Content-Type: application/json
{
  "mealPlan": [
    {
      "day": 1,
      "breakfast": {
        "recipeName": "Healthy Oatmeal",
        "difficulty": "novice",
        "kitchenToolsUsed": ["bowl", "spoon"],
        "instructions": ["Cook oats with water.", "Add fruits and nuts.", "Enjoy!"],
        "preparationTime": 15,
        "servings": 1,
        "ingredients": [
          {
            "name": "Oats",
            "unit": "grams",
            "amount": 50
          },
          {
            "name": "Banana",
            "unit": "pieces",
            "amount": 1
          },
          {
            "name": "Almonds",
            "unit": "grams",
            "amount": 20
          }
        ],
        "macros": {
          "carbs": {
            "amount": 30,
            "unit": "grams"
          },
          "fats": {
            "amount": 10,
            "unit": "grams"
          },
          "proteins": {
            "amount": 5,
            "unit": "grams"
          },
          "calories": {
            "amount": 250,
            "unit": "kcal"
          }
        }
      },
      "lunch": {
        // ... (same structure as breakfast)
      },
      "snack": {
        // ... (same structure as breakfast)
      },
      "dinner": {
        // ... (same structure as breakfast)
      }
    },
    // ... (meal plan for other days)
  ]
}

Readable Stream Route Details

Endpoint

POST /api/generate/stream/meal-plan

Request Example

POST /api/generate/stream/meal-plan
Headers:
{
  "Authorization": "YOUR_API_KEY"
}
Body:
{
  "goal": "lose-weight",
  "duration": 30,
  "activityLevel": "moderately-active",
  "age": 30,
  "height": 175,
  "weight": 70,
  "gender": "male",
  "diet": "vegetarian"
}

Response (Readable Stream)

  • Status Code: 200 (OK)
  • Content-Type: application/json (Streaming)
{
  "mealPlan": [
    {
      "day": 1,
      "breakfast": {
        "recipeName": "Healthy Oatmeal",
        "difficulty": "novice",
        "kitchenToolsUsed": ["bowl", "spoon"],
        "instructions": ["Cook oats with water.", "Add fruits and nuts.", "Enjoy!"],
        "preparationTime": 15,
        "servings": 1,
        "ingredients": [
          {
            "name": "Oats",
            "unit": "grams",
            "amount": 50
          },
          {
            "name": "Banana",
            "unit": "pieces",
            "amount": 1
          },
          {
            "name": "Almonds",
            "unit": "grams",
            "amount": 20
          }
        ],
        "macros": {
          "carbs": {
            "amount": 30,
            "unit": "grams"
          },
          "fats": {
            "amount": 10,
            "unit": "grams"
          },
          "proteins": {
            "amount": 5,
            "unit": "grams"
          },
          "calories": {
            "amount": 250,
            "unit": "kcal"
          }
        }
      },
      "lunch": {
        // ... (same structure as breakfast)
      },
      "snack": {
        // ... (same structure as breakfast)
      },
      "dinner": {
        // ... (same structure as breakfast)
      }
    },
    // ... (meal plan for other days)
  ]
}

The response is streamed and varies in size based on the generated content. On the client side, you should process the stream accordingly.

Handling the Readable Stream Response (JavaScript Example)

To handle the Readable Stream response in JavaScript, you can use the following code as an example:

const responseStream = fetch('/api/generate/stream/meal-plan', {
  method: 'POST',
  headers: {
    'Authorization': 'YOUR_API_KEY',
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
  "goal": "lose-weight",
  "duration": 30,
  "activityLevel": "moderately-active",
  "age": 30,
  "height": 175,
  "weight": 70,
  "gender": "male",
  "diet": "vegetarian"
}),
});

// Process the stream
responseStream.then(async (response) => {
  const reader = response.body.getReader();
  const decoder = new TextDecoder();

  let done
  let chunks

  while (!done) {
      const { value, done: doneReading } = await reader.read();

      done = doneReading; // Break the loop if stream completed
      const newValue = decoder.decode(value);
      chunks = chunks + newValue;
    }

    // Parses the completed response (mealPlan data)
    const mealPlanData = JSON.parse(chunks);

    // Handle the mealPlan data as needed
    console.log(mealPlanData);

});

Execution Time

As responses are generated on the fly through AI, response time will vary depending on the lenght of the response. Approximate time for response is 10s per recipe. A 'meal-plan' endpoint response is composed by multiple days and each days is composed by 4 recipes, you should account for a response time of 40/45 seconds per day. We strongly recommend using the Readable Stream endpoint for meal-plan as it will benefit your end-user experience.

Error Responses

Both routes return specific error responses for various scenarios:

  • Missing API Key (Status Code: 403):
    • Response: No API Key in request
  • Invalid API Key (Status Code: 403):
    • Response: Invalid API Key
  • Missing Required Inputs (Status Code: 400):
    • Response: Required inputs not provided
  • Inactive Subscription (Status Code: 403):
    • Response: You don't have an active subscription