Get a Random Meal

Learn how we can get a random meal recipe using this API.

TheMealDB API provides an endpoint using which the user can search for a random meal.

Sometimes, the user might not know what to cook. In this case, we can provide a random recipe to a user. This is where we can use this endpoint to give the user the recipe for a random meal. The base URI of this endpoint is https://www.themealdb.com/api/json/v1/1/random.php.

Request parameter

This endpoint does not require any query parameters.

The code below shows how we can call this endpoint to get the recipe for a random meal.

Press + to interact
const endpointUrl = new URL('https://www.themealdb.com/api/json/v1/1/random.php');
const options = {
method: 'GET'
};
async function fetchRandomMeal() {
try {
const response = await fetch(endpointUrl, options);
printResponse(response);
}
catch (error) {
printError(error);
}
}
fetchRandomMeal();

A brief explanation of the above code is given below:

  • Line 1: We define the URL of the endpoint.
  • Lines 3–5: We define the HTTP request type to call the endpoint.
  • Lines 7–16: We define the searchRandomMeal() function that calls the endpoint and prints the response.
  • Line 18: We call the searchRandomMeal() function.

Let's look at how we can use the data provided by this API. We've altered the printResponse() function in the code widget below. It'll now use the information returned by the API to provide us with the recipe for the random meal along with its image. Click the "Run" button to view the recipe. Each time the code is executed, it fetches a new meal recipe.

Press + to interact
const endpointUrl = new URL('https://www.themealdb.com/api/json/v1/1/random.php');
const options = {
method: 'GET'
};
async function fetchRandomDrink() {
try {
const response = await fetch(endpointUrl, options);
printResponse(response);
}
catch (error) {
printError(error);
}
}
fetchRandomDrink();

Response fields

The table below contains some important response fields:

Response field

Type

Description

idMeal

Integer

The API-assigned ID of the meal

strMeal

String

The name of the meal retrieved by this endpoint

strCategory

String

The category of the meal

strInstructions

String

The instructions to prepare the meal

strMealThumb

String

A link for the image of the meal

strIngredient1 - strIngredient20

String

The names of the required ingredients to make the meal

strMeasure1-strMeasure20

String

The amount of ingredients required to make the meal

Note: The value of some of the response objects might be null depending on the availability of the data.