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
.
// Importing libraries hereimport fetch from 'node-fetch';// Define the ID of any recipe hereconst id = '{{RECIPE_ID}}';// Define endpoint URL hereconst endpointUrl = new URL(`https://api.spoonacular.com/recipes/${id}/similar`);// Define API key hereconst apiKey = '{{API_KEY}}';// Define Header Parameters hereconst headerParameters = {'x-api-key': apiKey,'content-type': 'application/json',};// Define Query Parameters hereconst queryParameters = new URLSearchParams({number: 3,});// Setting API call optionsconst endpointOptions = {method: 'GET',headers: headerParameters,};// Function to make API callasync function fetchSimilarRecipes(url, options) {try {// Using the fetch method to make an API call to Spoonacularconst response = await fetch(url, options);// Custom function for printing the API responseprintResponse(response);} catch (error) {// Custom function for printing the error messageprintError(error);}}// Attaching query parameters to the endpoint URLendpointUrl.search = queryParameters;// Calling function to make API callfetchSimilarRecipes(endpointUrl, endpointOptions);
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}/similar
to fetch similar recipes.Line 11: We assign the API key here to the
apiKey
variable.Lines 14–17: We define the header parameters for the API call. We can modify or add parameters using the
headerParameters
variable.Line 15: We pass the
apiKey
variable to thex-api-key
header parameter.
Lines 20–22: We define the query parameters for the API call. We can modify or add parameters using the
queryParameters
variable.
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:
// Define endpoint URL hereconst endpointUrl = new URL('https://api.spoonacular.com/recipes/random');// Define Query Parameters hereconst queryParameters = new URLSearchParams({tags: 'British, dessert',number: 5,});// Setting API call optionsconst endpointOptions = {method: 'GET',headers: headerParameters,};// Attaching query parameters to the endpoint URLendpointUrl.search = queryParameters;// Calling function to make API callfetchRandomRecipes(endpointUrl, endpointOptions);
Take a look at the following explanation of the code above:
Line 2: We use the endpoint URL
/recipes/random
to fetch random recipes.Lines 5–8: We define the query parameters for the API call. We can modify or add parameters using the
queryParameters
variable.Line 6: We provide the
British
anddessert
tags to thetags
variable, 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 |