Fetching Recipe Suggestions

Learn how to fetch several recommended or random recipe suggestions using the Spoonacular API.

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 11 quota point, while each recipe returned has a cost of 0.010.01 quota points. The endpoint method and the URL are as follows:

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

id

Number

Required

The identifier of the recipe we’re basing the recommendations on.

number

Number

Optional

The number of results to include in the API response.

Possible values: 0100.

limitLicense

Boolean

Optional

The option, if set to true, ensures the API will only fetch recipes with an open license, allowing for proper attribution.

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.

Press + to interact
// Importing libraries here
import fetch from 'node-fetch';
// Define the ID of any recipe here
const id = '{{RECIPE_ID}}';
// Define endpoint URL here
const endpointUrl = new URL(`https://api.spoonacular.com/recipes/${id}/similar`);
// Define API key here
const apiKey = '{{API_KEY}}';
// Define Header Parameters here
const headerParameters = {
'x-api-key': apiKey,
'content-type': 'application/json',
};
// Define Query Parameters here
const queryParameters = new URLSearchParams({
number: 3,
});
// Setting API call options
const endpointOptions = {
method: 'GET',
headers: headerParameters,
};
// Function to make API call
async function fetchSimilarRecipes(url, options) {
try {
// Using the fetch method to make an API call to Spoonacular
const response = await fetch(url, options);
// Custom function for printing the API response
printResponse(response);
} catch (error) {
// Custom function for printing the error message
printError(error);
}
}
// Attaching query parameters to the endpoint URL
endpointUrl.search = queryParameters;
// Calling function to make API call
fetchSimilarRecipes(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 the x-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

id

Number

The ID of the recipe

title

String

The title name of the recipe

readyInMinutes

Number

The time that it takes to cook the recipe in minutes

servings

Number

The number of servings in the recipe

sourceUrl

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 11quota point, while each recipe returned has a cost of 0.010.01quota point. The endpoint method and the URL are as follows:

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

tags

String

Optional

The tags that should be associated with the recipe. These tags can be cuisines, diets, intolerances, or meal types.

number

Number

Optional

The number of results to include in the API response.

Possible values: 0100.

limitLicense

Boolean

Optional

The option, if set to true, ensures the API will only fetch recipes with an open license, allowing for proper attribution.

Let’s see how to call the API endpoint. Click the “Run” button to execute the code:

Press + to interact
// Define endpoint URL here
const endpointUrl = new URL('https://api.spoonacular.com/recipes/random');
// Define Query Parameters here
const queryParameters = new URLSearchParams({
tags: 'British, dessert',
number: 5,
});
// Setting API call options
const endpointOptions = {
method: 'GET',
headers: headerParameters,
};
// Attaching query parameters to the endpoint URL
endpointUrl.search = queryParameters;
// Calling function to make API call
fetchRandomRecipes(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 and dessert tags to the tags 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

vegetarian

Boolean

Indicates whether the recipe is a vegetarian dish

pricePerServing

Number

The per-serving price to make the recipe

id

Number

The ID of the recipe

title

String

The name of the recipe

readyInMinutes

Number

The time that it takes to cook the recipe in minutes

servings

Number

The number of servings in the recipe

sourceUrl

String

The source URL of the recipe

instructions

String

The instructions to make the recipe