Recipe From Ingredients

The 'recipe-from-ingredients' 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 a set of provided ingredients 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:

  • ingredients (array, required): An array of strings representing ingredients.
  • mealType (enum, optional): The type of meal you want to create ['breakfast', 'lunch', 'dinner', 'snack', 'dessert']. Default: No meal restrictions.
  • kitchenTools (array, optional): An array of strings representing kitchen tools available for cooking. Default: No tools restrictions.
  • preparationTime (number, optional): The maximum preparation time in minutes. Default: No time restrictions.
  • servings (number, optional): The amount of servings the recipe should yield. Default: 1 serving.
  • difficulty (enum, optional): The desired difficulty level of the recipe ['novice', 'intermediate', 'expert']. Default: No difficulty 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-ingredients

Request Example

POST /api/generate/recipe-from-ingredients
Headers:
{
  "Authorization": "YOUR_API_KEY"
}
Body:
{
  "ingredients": ["Chicken 500 grams","2 Cups of Rice "],
  "mealType": "dinner",
  "kitchenTools": ["oven", "pot"],
  "preparationTime": 30,
  "servings": 2,
  "difficulty": "intermediate"
}

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": 2,
  "kitchenToolsUsed": ["oven", "pot"]
}

Readable Stream Route Details

Endpoint

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

Request Example

POST /api/generate/stream/recipe-from-ingredients
Headers:
{
  "Authorization": "YOUR_API_KEY"
}
Body:
{
  "ingredients": ["Chicken 500 grams","2 Cups of Rice "],
  "mealType": "dinner",
  "kitchenTools": ["oven", "pot"],
  "preparationTime": 30,
  "servings": 2,
  "difficulty": "intermediate"
}

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": 2,
  "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 = await fetch('/api/generate/stream/recipe-from-ingredients', {
  method: 'POST',
  headers: {
    'Authorization': 'YOUR_API_KEY',
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
  "ingredients": "ingredients": ["Chicken 500 grams","2 Cups of Rice "],
  "mealType": "dinner",
  "kitchenTools": ["oven", "pot"],
  "preparationTime": 30,
  "servings": 2,
  "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-ingredients' 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