Recipe From Macros

The 'recipe-from-macros' endpoint allows you to generate recipes from an available set of ingredients and additional criterias are available to fine ture the output. Customers have the choice of receiving the response as JSON or as a readable stream.

Introduction

This API route allows you to generate a recipe based on macronutrient distributions and optional constraints. It provides two endpoints, each with a different response format:

  • JSON Route: Returns the recipe details as JSON.
  • Readable Stream Route: Streams the recipe 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:

  • carbs (number, required): The amount of carbohydrates in grams.
  • proteins (number, required): The amount of proteins in grams.
  • fats (number, required): The amount of fats in grams.
  • diet (string, optional): The dietary preference ['vegetarian','pescatarian','vegan','diaryfree','glutenfree','keto','paleo']. Default: No diet restrictions.
  • meal (string, optional): The meal type ['breakfast', 'lunch', 'dinner', 'snack', 'dessert']. Default: No meal restrictions.
  • 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', 'hebrew']. Default: "english".
  • allergies (string, optional): A comma separated list of ingredients to exclude from the recipe.

JSON Route Details

Endpoint

POST /api/generate/recipe-from-macros

Request Example

POST /api/generate/recipe-from-macros
Headers:
{
  "Authorization": "YOUR_API_KEY"
}
Body:
{
  "carbs": 100,
  "proteins": 50,
  "fats": 30,
  "diet": "vegetarian",
  "meal": "lunch"
}

Response (JSON)

  • Status Code: 200 (OK)
  • Content-Type: application/json
{
  "recipeName": "Delicious Chicken and Rice",
  "ingredients": [
    {
      "name": "Chicken",
      "unit": "grams",
      "amount": 500
    },
    {
      "name": "Rice",
      "unit": "cups",
      "amount": 2
    },
    // ... other ingredients
  ],
  "instructions": [
    "1. Preheat the oven to 350°F.",
    "2. Season the chicken with salt and pepper.",
    // ... other instructions
  ],
  "difficulty": "intermediate",
  "macros": {
    "carbs": {
      "amount": 40,
      "unit": "grams"
    },
    // ... other macros
  },
  "preparationTime": 30,
  "servings": 1,
  "kitchenToolsUsed": ["oven", "pot"]
}

Readable Stream Route Details

Endpoint

POST /api/generate/stream/recipe-from-macros

Request Example

POST /api/generate/stream/recipe-from-macros
Headers:
{
  "Authorization": "YOUR_API_KEY"
}
Body:
{
  "carbs": 100,
  "proteins": 50,
  "fats": 30,
  "diet": "vegetarian",
  "meal": "lunch"
}

Response (Readable Stream)

  • Status Code: 200 (OK)
  • Content-Type: application/json (Streaming)
{
  "recipeName": "Delicious Chicken and Rice",
  "ingredients": [
    {
      "name": "Chicken",
      "unit": "grams",
      "amount": 500
    },
    {
      "name": "Rice",
      "unit": "cups",
      "amount": 2
    },
    // ... other ingredients
  ],
  "instructions": [
    "1. Preheat the oven to 350°F.",
    "2. Season the chicken with salt and pepper.",
    // ... other instructions
  ],
  "difficulty": "intermediate",
  "macros": {
    "carbs": {
      "amount": 40,
      "unit": "grams"
    },
    // ... other macros
  },
  "preparationTime": 30,
  "servings": 1,
  "kitchenToolsUsed": ["oven", "pot"]
}

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/recipe-from-macros', {
  method: 'POST',
  headers: {
    'Authorization': 'YOUR_API_KEY',
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
  "ingredients": [
    {
      "name": "Chicken",
      "unit": "grams",
      "amount": 500
    },
    {
      "name": "Rice",
      "unit": "cups",
      "amount": 2
    }
  ],
  "mealType": "dinner",
  "kitchenTools": ["oven", "pot"],
  "preparationTime": 30,
  "difficulty": "intermediate"
}),
});

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

  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 (recipe data)
    const recipeData = JSON.parse(chunks);

    // Handle the recipe data as needed
    console.log(recipeData);

});

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 for the 'recipe-from-macros' endpoint.

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