# Generate a Meal Plan

At any point in time, you can make adjustments to the meal plan settings and schedule, and replace your existing meal plan with a new one.

{% hint style="info" %}
**Note**: To use the Whisk Meal Plan API, you must acquire a User Token. For more information, see [User Access Tokens](https://docs.whisk.com/api-overview/auth/user-access-token).
{% endhint %}

The Meal Plan API provides you two endpoints that allow you to generate a meal plan. The only difference between the two is that one of them requires a `{meal_plan_id}` and the other doesn't. This arrangement has been made for a future planned feature of supporting multiple meal plans for every user.

While at the moment, since Whisk only supports a single meal plan per user, the  `{meal_plan_id}` has been kept optional for both endpoints. You can leave it blank and it would be directly derived from the user token being used for API authentication.

## Generate a meal plan

<mark style="color:orange;">`PUT`</mark> `https://api.whisk.com/mealplan/v2/generate`

This endpoint allows you to regenerate a meal plan using the Body parameter.

#### Request Body

| Name | Type   | Description                                                                                                                                                             |
| ---- | ------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| body | object | This parameter allows you to specify the settings,  schedule, and preferences for the new meal plan. For more information, see the *Request Body Object Example* below. |

{% tabs %}
{% tab title="200 This how a successful meal plan response looks like. To understand its data structure, see the Meal Plan API topic." %}

```javascript
{
  "meal_plan_id": "5f27a59a9a8641078c7057eeec0a1b90",
  "start_date": 
    ...
  "end_date":
    ...
  "meals": [
    {
      "id": "894f285cd9d543d3b78b374f59b56311",
      "meal_type": "MEAL_TYPE_BREAKFAST",
      "date_time":
        ...
      "content": {
        "recipe": 
          ...
      },
      "generated": true,
      "day_slot": 3,
      "date":
        ...
    },
    {
      "id": "aab8f93131724117836f8cab365518c6",
      "meal_type": "MEAL_TYPE_LUNCH",
      "date_time":
        ...
      "content": {
        "recipe": 
          ...
      },
      "generated": true,
      "day_slot": 4,
      "date":
        ...
    },
    {
      "id": "4b50cf3d2bf24ebe9c4630d2816fea99",
      "meal_type": "MEAL_TYPE_DINNER",
      "date_time":
        ...
      "content": {
        "recipe": 
          ...
      },
      "generated": true,
      "day_slot": 5,
      "date":
        ...
    },
    ...
  ],
  "result_code": "GENERATION_RESULT_CODE_SUCCESS"
}
```

{% endtab %}

{% tab title="400 This failed response appears when specific error codes are found in the endpoint description." %}

```javascript
{
  "error_code": "REAL_CODES_ARE_IN_ENDPOINT_DESCRIPTION",
  "message": "Additional details about error are not static and can be changed"
}
```

{% endtab %}

{% tab title="401 This failed response appears when API authentication gets failed. The possible error codes that may appear are: auth.tokenNotFound auth.tokenExpired auth.tokenInvalid auth.tokenRequired" %}

```javascript
{
  "code": "auth.tokenNotFound"
}
```

{% endtab %}

{% tab title="500 This failed response appears when something is not right at Whisk's end." %}

```javascript
This is unexpected response, something is wrong on our side, please contact: help@whisk.com
```

{% endtab %}
{% endtabs %}

{% hint style="info" %}
**Note**: Whisk provides its APIs on the Swagger interface for you to try it out. You can call this endpoint [here](https://api.whisk.com/spec/#/MealPlanAPI/MealPlanAPI_GenerateMealPlan2).
{% endhint %}

## Generate a specific meal plan

<mark style="color:orange;">`PUT`</mark> `https://api.whisk.com/mealplan/v2/{meal_plan_id}/generate`

This endpoint allows you to regenerate a meal plan using the **meal\_plan\_id** path parameter (currently optional) and the **body** parameter.

#### Path Parameters

| Name           | Type   | Description               |
| -------------- | ------ | ------------------------- |
| meal\_plan\_id | string | The meal plan identifier. |

#### Request Body

| Name | Type   | Description                                                                                                                                                             |
| ---- | ------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| body | object | This parameter allows you to specify the settings,  schedule, and preferences for the new meal plan. For more information, see the *Request Body Object Example* below. |

{% tabs %}
{% tab title="200 This how a successful meal plan response looks like. To understand its data structure, see the Meal Plan API topic." %}

```javascript
{
  "meal_plan_id": "5f27a59a9a8641078c7057eeec0a1b90",
  "start_date": 
    ...
  "end_date":
    ...
  "meals": [
    {
      "id": "894f285cd9d543d3b78b374f59b56311",
      "meal_type": "MEAL_TYPE_BREAKFAST",
      "date_time":
        ...
      "content": {
        "recipe": 
          ...
      },
      "generated": true,
      "day_slot": 3,
      "date":
        ...
    },
    {
      "id": "aab8f93131724117836f8cab365518c6",
      "meal_type": "MEAL_TYPE_LUNCH",
      "date_time":
        ...
      "content": {
        "recipe": 
          ...
      },
      "generated": true,
      "day_slot": 4,
      "date":
        ...
    },
    {
      "id": "4b50cf3d2bf24ebe9c4630d2816fea99",
      "meal_type": "MEAL_TYPE_DINNER",
      "date_time":
        ...
      "content": {
        "recipe": 
          ...
      },
      "generated": true,
      "day_slot": 5,
      "date":
        ...
    },
    ...
  ],
  "result_code": "GENERATION_RESULT_CODE_SUCCESS"
}
```

{% endtab %}

{% tab title="400 This failed response appears when specific error codes are found in the endpoint description." %}

```javascript
{
  "error_code": "REAL_CODES_ARE_IN_ENDPOINT_DESCRIPTION",
  "message": "Additional details about error are not static and can be changed"
}
```

{% endtab %}

{% tab title="401 This failed response appears when API authentication gets failed. The possible error codes that may appear are: auth.tokenNotFound auth.tokenExpired auth.tokenInvalid auth.tokenRequired" %}

```javascript
{
  "code": "auth.tokenNotFound"
}
```

{% endtab %}

{% tab title="500 This failed response appears when something is not right at Whisk's end." %}

```javascript
This is unexpected response, something is wrong on our side, please contact: help@whisk.com
```

{% endtab %}
{% endtabs %}

{% hint style="info" %}
**Note**: Whisk provides its APIs on the Swagger interface to try and test. You can call this endpoint [here](https://api.whisk.com/spec/#/MealPlanAPI/MealPlanAPI_GenerateMealPlan).
{% endhint %}

{% tabs %}
{% tab title="Request Body Object Example" %}

```javascript
{
  "meal_plan_id": "string",
  "settings": {
    ...
  },
  "period": {
    ...
  },
  "fields": [
    ...
  ]
}
```

The body object contains the following attributes and arrays that can be set to generate a meal plan for you:

| Attribute      | Type   | Description                                                                                                                                                                                             | Example Value                                                                                                                                                                                                                                                                                                                                                                                                        |
| -------------- | ------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `meal_plan_id` | string | The meal-plan identifier. You can skip it when using an Access Token for API authentication.                                                                                                            | `5f27a59a9a8641078c7057eeec0a1b90`                                                                                                                                                                                                                                                                                                                                                                                   |
| `settings`     | array  | This includes all the core settings based on which you want to create your meal plan like - the number of servings, caloric goals, effective days, generation algorithm, food preferences, labels, etc. | See [Meal Plan Settings](https://docs.whisk.com/api/meal-plans/get-meal-plan-settings).                                                                                                                                                                                                                                                                                                                              |
| `period`       | array  | The time period that the meal plan needs to cover.                                                                                                                                                      | <p><code>"period": {</code> </p><p>    <code>"from": {</code> </p><p>       <code>"year": 2020,</code> </p><p>       <code>"month": 10,</code> </p><p>       <code>"day": 1</code> </p><p>    <code>},</code></p><p>    <code>"to": {</code> </p><p>      <code>"year": 2020,</code></p><p>      <code>"month": 10,</code> </p><p>      <code>"day": 10</code></p><p>     <code>}</code></p><p>  <code>},</code></p> |
| `fields`       | string | Any additional recipe information you may want to include in your plan.                                                                                                                                 | <ul><li><code>RECIPE\_FIELD\_NORMALIZED\_INGREDIENTS</code></li><li><code>RECIPE\_FIELD\_INSTRUCTIONS</code></li><li><code>RECIPE\_FIELD\_NUTRITION</code> </li><li><code>RECIPE\_FIELD\_INGREDIENTS\_LINKED\_PRODUCTS</code></li></ul>                                                                                                                                                                              |
| {% endtab %}   |        |                                                                                                                                                                                                         |                                                                                                                                                                                                                                                                                                                                                                                                                      |

{% tab title="Curl Call Example" %}

```bash
curl -X POST "https://api.whisk.com/mealplan/v2/generate" 
     -H  "accept: application/json" 
     -H "Authorization: Token <Access-Token>" 
     -H  "Content-Type: application/json" 
     -d "{  \"settings\": {    \"servings\": 1,    \"calorie_per_day\": 2000,    \"enabled_auto_generation\": true,    \"replace_only_generated\": true  },  \"period\": {    \"from\": {      \"year\": 2020,      \"month\": 10,      \"day\": 1    },    \"to\": {      \"year\": 2020,      \"month\": 10,      \"day\": 10    }  },  \"fields\": [    \"RECIPE_FIELD_INVALID\"  ]}"
```

{% endtab %}
{% endtabs %}
