Fetching Recipe Suggestions
Learn how to fetch several recommended or random recipe suggestions using the Spoonacular API.
We'll cover the following...
Sometimes, people get hungry and are unsure of what to cook and eat. Spoonacular solves this problem by providing recipe recommendations that are random or based on a recipe the user already likes. The following two endpoints of the Spoonacular API offer these services:
- similar recipes
- random recipes
The similar recipes endpoint
The similar recipes endpoint takes in the ID of a recipe as a path parameter and fetches similar recipes based on that ID. The similar recipes endpoint has a cost of
GET https://api.spoonacular.com/recipes/{id}/similar
We can use one of the following recipe IDs for the code widget in this section:
Recipe IDs
Recipe Name | ID |
Classic Fruit Salad | 639600 |
Tex-Mex Burger | 663050 |
Baked Sirloin Steak | 633790 |
Vegan Chana Masala Curry | 1096306 |
Belgian Hot Chocolate | 634818 |
Here are the request parameters we can use with the similar recipes endpoint when fetching similar recipes:
Request Parameters
Parameter | Type | Category | Description |
| Number | Required | The identifier of the recipe we’re basing the recommendations on. |
| Number | Optional | The number of results to include in the API response. Possible values: |
| Boolean | Optional | The option, if set to |
Let’s see how to call the API endpoint. Click the “Run” button to execute the code. Make sure you’ve provided the value for RECIPE_ID.
Take a look at the following explanation of the code above:
Line 5: We set and store the ID of a recipe for which we want to get similar recipe suggestions.
Line 8: We use the endpoint URL
/recipes/{id}/similarto fetch similar recipes.Line 11: We assign the API key here to the
apiKeyvariable.Lines 14–17: We define the header parameters for the API call. We can modify or add parameters using the
headerParametersvariable.Line 15: We pass the
apiKeyvariable to thex-api-keyheader parameter.
Lines 20–22: We define the query parameters for the API call. We can modify or add parameters using the
queryParametersvariable.
The response of this endpoint is an array of recipe objects. Here are some of the fields from the API response:
Response Fields
Name | Type | Description |
| Number | The ID of the recipe |
| String | The title name of the recipe |
| Number | The time that it takes to cook the recipe in minutes |
| Number | The number of servings in the recipe |
| String | The source URL of the recipe |
The random recipes endpoint
The random recipes endpoint fetches random recipes. The random recipes endpoint has a cost of
GET https://api.spoonacular.com/recipes/random
Here are the request parameters we can use with the random recipes endpoint to alter the search results:
Request Parameters
Parameter | Type | Category | Description |
| String | Optional | The tags that should be associated with the recipe. These tags can be cuisines, diets, intolerances, or meal types. |
| Number | Optional | The number of results to include in the API response. Possible values: |
| Boolean | Optional | The option, if set to |
Let’s see how to call the API endpoint. Click the “Run” button to execute the code:
Take a look at the following explanation of the code above:
Line 2: We use the endpoint URL
/recipes/randomto fetch random recipes.Lines 5–8: We define the query parameters for the API call. We can modify or add parameters using the
queryParametersvariable.Line 6: We provide the
Britishanddesserttags to thetagsvariable, which fetches random British dessert recipes.
The response of this endpoint is an array of recipe objects stored in the recipes field. Here are some of the fields from the API response:
Response Fields
Name | Type | Description |
| Boolean | Indicates whether the recipe is a vegetarian dish |
| Number | The per-serving price to make the recipe |
| Number | The ID of the recipe |
| String | The name of the recipe |
| Number | The time that it takes to cook the recipe in minutes |
| Number | The number of servings in the recipe |
| String | The source URL of the recipe |
| String | The instructions to make the recipe |