# Creating a Meal Plan

{% hint style="info" %}
Before starting, you'll need to have a user created. Follow [this guide](https://docs.suggestic.com/graphql/query/mutations/user-profile/new-user) to create one.
{% endhint %}

### Getting Started

Suggestic meal plan API finds the best recipes for your user's dietary plan according to their food preferences and goals. The API has different services and parameters that let you specify how these meal plans are generated.

Let's start by generating a **simple meal plan** using the `generateSimpleMealPlan` mutation.&#x20;

{% tabs %}
{% tab title="Request" %}

```bash
curl -XPOST 'https://production.suggestic.com/graphql' \
  -H 'Authorization: Bearer <User-JWT>' \
  -H 'Content-Type: application/json' \
  --data-raw '{"query":"mutation { generateSimpleMealPlan { success message } }","variables":{}}'
```

{% endtab %}

{% tab title="Response" %}

```javascript
{
    "data": {
        "generateSimpleMealPlan": {
            "success": true,
            "message": "Meal plan was successfully created"
        }
    }
}
```

{% endtab %}
{% endtabs %}

{% hint style="warning" %}
Remember to replace `<User-JWT>` with your user's access token
{% endhint %}

Once the meal plan is created, you can request the meal plan using the `mealPlan` query.

{% tabs %}
{% tab title="Request" %}

```bash
curl -XPOST 'https://production.suggestic.com/graphql' \
  -H 'Content-Type: application/json' \
  -H 'Authorization: Bearer Bearer <User-JWT>' \
  --data-raw '{"query":"{ mealPlan { day date calories meals { meal recipe { name } } } }"}'
```

{% endtab %}

{% tab title="Response" %}

```javascript
{
  "data": {
    "mealPlan": [
      {
        "day": 7,
        "date": "2021-03-15 02:00:00+00:00",
        "calories": 1966.93596472514,
        "meals": [
          {
            "meal": "breakfast",
            "recipe": {
              "name": "Chocolate Collagen Keto Granola"
            }
          },
          {
            "meal": "dinner",
            "recipe": {
              "name": "Pan-Seared Salmon And Roasted Vegetables"
            }
          },
          {
            "meal": "lunch",
            "recipe": {
              "name": "Layered Bean Dip"
            }
          },
          {
            "meal": "snack",
            "recipe": {
              "name": "Celery With Cream Cheese"
            }
          }
        ]
      },
      [...]
      {
        "day": 1,
        "date": "2021-03-09 02:00:00+00:00",
        "calories": 2028.06644439517,
        "meals": [
          {
            "meal": "breakfast",
            "recipe": {
              "name": "Scotch Eggs"
            }
          },
          {
            "meal": "dinner",
            "recipe": {
              "name": "Turmero Onion Burgers"
            }
          },
          {
            "meal": "lunch",
            "recipe": {
              "name": "Herb-Seasoned Sheperd'S Pie"
            }
          },
          {
            "meal": "snack",
            "recipe": {
              "name": "Seasoned Roasted Carrots"
            }
          }
        ]
      }
    ]
  }
}
```

{% endtab %}
{% endtabs %}

The `generateSimpleMealPlan` mutation doesn't require any arguments. It generates a 7-days meal plan that complies with the user's program and preferences.

As you can see in the last response, `generateSimpleMealPlan` has series of default values, for example, each day has breakfast,  lunch,  dinner, and a single snack, with a daily caloric goal of 2000 kcal.

If you need more control over these settings, please review the [Generate Meal Plan](https://docs.suggestic.com/graphql/query/mutations/meal-plan/generate-meal-plan) mutation.

### Dates

Suggestic meal plan API always generates a 7-day plan.&#x20;

Each day of the plan has a date. Day 1 has the date when the meal plan was generated, day 2 has the next date, and so on.&#x20;

### Meal Plan Lock

By default, `generateSimpleMealPlan` and `generateMealPlan` put a lock that stops the user from generating a new meal plan if one is *in progress*. In other words, a user can't generate a new meal plan if the current one still has dates in the future.

If you try using `generateSimpleMealPlan`, while a meal plan is in progress, you'll get the following response:

{% tabs %}
{% tab title="Request" %}

```bash
curl -XPOST 'https://production.suggestic.com/graphql' \
  -H 'Authorization: Bearer <User-JWT>' \
  -H 'Content-Type: application/json' \
  --data-raw '{"query":"mutation { generateSimpleMealPlan { success message } }","variables":{}}'
```

{% endtab %}

{% tab title="Response" %}

```javascript
{
    "data": {
        "generateSimpleMealPlan": {
            "success": false,
            "message": "User has a meal plan in progress."
        }
    }
}
```

{% endtab %}
{% endtabs %}

To bypass the lock, send the argument `ignoreLock: true` .

{% tabs %}
{% tab title="Request" %}

```bash
curl -XPOST 'https://production.suggestic.com/graphql' \
  -H 'Authorization: Bearer <User-JWT>' \
  -H 'Content-Type: application/json' \
  --data-raw '{"query":"mutation { generateSimpleMealPlan(ignoreLock: true) { success message } }","variables":{}}'
```

{% endtab %}

{% tab title="Response" %}

```javascript
{
    "data": {
        "generateSimpleMealPlan": {
            "success": true,
            "message": "Meal plan was successfully created"
        }
    }
}
```

{% endtab %}
{% endtabs %}

### Customization and configuration&#x20;

You can think of meal plan generation as a two stages process.

**In the first stage**, the planner finds a set of recipes that are good candidates for a personalized meal plan. These recipes are a good choice according to the user's **restrictions**, dietary **program,** and **custom attributes**. This is the *customization* stage.

You can learn more about how to set users personal restrictions, subscribing users to programs, and setting their custom attributes as follows:

#### How to apply restrictions to a meal plan

Refer to the [User's Restrictions](https://docs.suggestic.com/graphql/query/mutations/user-profile/profilerestrictionsupdate) page.&#x20;

Restrictions can be added in the following scenarios:

* **Before generating a meal plan:** Define which restrictions you need to apply, and then the meal plan is generated based on the applied restrictions.
* **During a meal plan in progress:** It may be the case where new restrictions need to be applied to the meal plan in progress. In this case, apply these new restrictions and generate the meal plan again.

If the meal plan is finished by the time the restrictions are set, a new 7 day meal plan will be generated where these new restrictions will be applied.

#### How to subscribe users to a specific program&#x20;

Refer to the [User's Program](https://docs.suggestic.com/graphql/query/mutations/user-profile/set-program) page.

To add a program, update the User Program and then generate the meal plan.&#x20;

#### How to add custom attributes

Refer to the [User's Custom Attributes](https://docs.suggestic.com/graphql/query/mutations/user-profile/custom-attributes) page.

**The second stage** is *configuration*. Configurations are sent to the meal planner in each query as parameters and are used to sort and group the recipes into daily plans. This includes grouping constraints, like setting the daily caloric goals, the number of snacks per day, or the minimum serving weight in grams per meal.

Learn more about these configurations in the `generateMealPlan` [page](https://docs.suggestic.com/graphql/query/mutations/meal-plan/generate-meal-plan).

You can change configurations and customize as much as you need depending on your app and use cases.

### **Common Errors**

When users have too many restrictions and the meal plan is not capable of generating a whole week of the meal plan, the following error is displayed:

```haskell
"generateMealPlan":
 { 
    "success": false,
    "message": "Couldn't generate a meal plan with the current settings."
 }
```

This error may appear due to:

* There is a problem with the definition of macro values. Check your [user's meal plan settings](https://docs.suggestic.com/graphql/query/mutations/user-profile/profile-meal-plan-settings#example)
* The type of subscription.
* The user program.

For more information, refer to [this documentation](https://docs.suggestic.com/graphql/knowledge-base/common-errors/meal-plan-generation).

### More about meal plans

* Read about user [restrictions](https://docs.suggestic.com/graphql/query/mutations/user-profile/profilerestrictionsupdate), [programs](https://docs.suggestic.com/graphql/query/mutations/user-profile/set-program), and [custom attributes](https://docs.suggestic.com/graphql/query/mutations/user-profile/custom-attributes) to customize a meal plan.
* Meal plans are not automatically generated when a user's program, restrictions, or attributes change. Read more about keeping a meal plan updated on the `generateMealPlan` [page](https://docs.suggestic.com/graphql/query/mutations/meal-plan/generate-meal-plan).
* The `mealPlan` response can include fields that are not used here to keep the examples simple. See a full list of fields on the [meal plan query](https://docs.suggestic.com/graphql/query/queries/meal-plan) page.
* Take a look at the [custom meal plan](https://docs.suggestic.com/graphql/query/queries/meal-plan/meal-plan) query. It lets you request meal plans without assigning them to the user. You can request as many as you need and can help you find the configuration that works best for your app and use cases.
