Fetching Recipe Information

Learn how to fetch detailed information for specific recipes using the Spoonacular API.

The Spoonacular API allows us to fetch detailed information for specific or multiple recipes. To do so, we can use the following endpoints based on our use case:

  • recipe information
  • recipe information bulk

We can use one of the following recipe IDs for the code widgets in this lesson:

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

The recipe information endpoint

The recipe information endpoint takes in the ID of a recipe as a path parameter and fetches detailed information for that recipe. The recipe information endpoint has a cost of 11quota point, and an additional cost of 0.10.1quota point is incurred if the includeNutrition option is enabled. The endpoint method and the URL are as follows:

GET https://api.spoonacular.com/recipes/{id}/information

Here are the request parameters we can use with the recipe information endpoint:

Request Parameters

Parameter

Type

Category

Description

id

Number

Required

The identifier of the recipe for which we’re fetching the detailed information.

includeNutrition

Boolean

Optional

The option, if set to true, includes the per-serving nutrition data in the API response.

Let’s see how to call the API endpoint. Click the “Run” button to execute the code. Please provide 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}/information`);
// 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({
includeNutrition: true,
});
// Setting API call options
const endpointOptions = {
method: 'GET',
headers: headerParameters,
};
// Function to make API call
async function fetchRecipeInformation(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
fetchRecipeInformation(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 fetch the recipe information.

  • Line 8: We use the endpoint URL /recipes/{id}/information to fetch detailed information for a specific recipe.

  • 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.

    • Line 21: We set the includeNutrition variable to true, which will include nutritional data in the API response.

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

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 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

instructions

String

The instructions to make the recipe

nutrition

Object[ ]

The detailed nutritional data for the recipe

The recipe information bulk endpoint

The recipe information bulk endpoint fetches detailed information for a list of recipes. It’s faster to use this endpoint to fetch detailed information for multiple recipes rather than calling the recipe information endpoint several times. The recipe information bulk endpoint for the first recipe has a cost of 11quota point, while each additional recipe has a cost of 0.50.5quota point. The endpoint method and URL are as follows:

GET https://api.spoonacular.com/recipes/informationBulk

Here are the request parameters we can use with the recipe information bulk endpoint:

Request Parameters

Parameter

Type

Category

Description

ids

String

Optional

The comma-separated list of identifiers of the recipes for which we’re fetching the detailed bulk information.

includeNutrition

Boolean

Optional

The option, if set to true, includes the per-serving nutrition data in the API response.

Let’s see how to call the API endpoint. Click the “Run” button to execute the code. Make sure to provide the value for RECIPE_ID2. Also, provide the value for RECIPE_ID.

Press + to interact
// Define endpoint URL here
const endpointUrl = new URL('https://api.spoonacular.com/recipes/informationBulk');
// Define Query Parameters here
const queryParameters = new URLSearchParams({
ids: '{{RECIPE_ID}},{{RECIPE_ID2}}',
includeNutrition: false,
});
// 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
fetchRecipeInformationBulk(endpointUrl, endpointOptions);

Take a look at the following explanation of the code above:

  • Line 2: We use the endpoint URL /recipes/informationBulk to fetch detailed information for multiple specific 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 set the comma-separated list of recipe IDs in the ids variable to fetch the detailed information for those recipes.

    • Line 7: We set the includeNutrition variable to false to exclude nutritional data in the API response.

The response of this endpoint is a recipe object. 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 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

instructions

String

The instructions to make the recipe