Husdady
Husdady

Reputation: 119

Filter multiple pokemons of PokeApi

I make a request to 'https://pokeapi.co/api/v2/pokemon/${name}' to obtain the information of a specific pokemon, the problem is that the name must be the same otherwise it returns undefined, I need to filter many pokemons, for example: if I search for char it should return charmeleon and charizard, because they both have 'char'. How can I filter a lot of pokemons?

const params = {
  headers: {
    'Content-Type': 'application/json'
  }
}

const searchPokemon = async name => {
  const url = `https://pokeapi.co/api/v2/pokemon/${name}`
  try {
    const response = await fetch(url, params);
    const result = await response.json();
    return result;
  } catch (error) {
    console.log(error)
  }
}

Upvotes: 0

Views: 4838

Answers (1)

nullromo
nullromo

Reputation: 2647

To get a list of all possible pokemon names, you can make a request to https://pokeapi.co/api/v2/pokemon?limit=100000 (where 100000 is larger than the number of pokemon that exist. There appear to be only 1118 pokemon as of now.)

The result looks like this:

[
  {
    name:"bulbasaur",
    url:"https://pokeapi.co/api/v2/pokemon/1/"
  },
  {
    name:"ivysaur",
    url:"https://pokeapi.co/api/v2/pokemon/2/"
  },
  {
    name:"venusaur",
    url:"https://pokeapi.co/api/v2/pokemon/3/"
  },
  ...
]

Then you can filter out that list based on the names you are looking for. After you find the name you want, you can use the corresponding URL to get more information.

Upvotes: 1

Related Questions