Pokeapi: Dynamically fetching a random pokemon (not including alternative forms)

Created on 21 Jan 2020  路  8Comments  路  Source: PokeAPI/pokeapi

What I'm trying to achieve
Fetch information on a random standard pokemon (not including alternative forms).

What I assumed
I assumed that I could read the "count" value from https://pokeapi.co/api/v2/pokemon/ and use this value as a maximum when generating a random pokemon index to fetch.

The problem I encounter
The randomized call that I attempt to make ends up returning :null whenever the number resolves to anything between 807 (last pokemon) and 964 (your record count).

Suggestion
Fair enough, I've simply made a faulty assumption when thinking that count represents the database's last valid index, and count appears to be doing what it should (informing of record count). However, would it be possible to also add a value to the /pokemon/ endpoint that offers the user the highest pokedex entry index that exists? This seems like a worthy enough addition to the API when it already largely adheres to a pokedex ordering.

Note
I realise that I could technically traverse the entire dataset and randomly decide to stop or somehow count the "valid" pokemon entries myself, but this would be far less efficient and I feel it would make sense for the API to offer this information up front.

Most helpful comment

In pokeapi/v2/pokemon-species, you have a count value, which is equel to 807,so the number of pokemon in the API, without counting alternate forms. You could eventually use that value.

All 8 comments

Why not just grab the list endpoint and pick a random option from it?

Why not just grab the list endpoint and pick a random option from it?

Do you mean by fetching https://pokeapi.co/api/v2/pokemon/ and traversing the results? Since they're limited to 20 by default I'd have to hardcode passing the amount of options as a parameter, which I'd preferably avoid. Ideally my code doesn't assume the amount of options available.

Sorry if I misunderstood where to find the list endpoint.

You can just call it like:
https://pokeapi.co/api/v2/pokemon/?limit=9999999

You can just call it like:
https://pokeapi.co/api/v2/pokemon/?limit=9999999

That also gives me the alternative pokemon forms, and I only want the standard pokemon entries, not including their variations.

I realise that I could probably do ?limit=1000 to only get the 807 standard pokemon entries, but it wouldn't be perfectly dynamic considering future games will likely introduce more pokemon, and theoretically going beyond 1000 at some point. Of course, it's entirely possible I'm being too nitpicky here. :P

There are clearly workarounds but I do think that the count variable does seem to be a little misleading. It's a fairly easy to intuit that given a count and incremental IDs that they would be sequential.

@sindrekjr for now, I'd recommend getting the full list with https://pokeapi.co/api/v2/pokemon/?limit=9999999, splitting the list at the point when the IDs make a jump to the 10k range (where the variants start) and then randomising from that pool instead of from the value of the ID.

In pokeapi/v2/pokemon-species, you have a count value, which is equel to 807,so the number of pokemon in the API, without counting alternate forms. You could eventually use that value.

@froulet001 That looks to be exactly what I should be using, yup. Thanks!

fairly easy to intuit that given a count and incremental IDs that they would be sequential

This is a fair assumption but also an assumption that you shouldn't be making. It's helpful for learning that humans recognise patterns and sometimes systems don't follow patterns.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Elgeneinar picture Elgeneinar  路  6Comments

renatoi picture renatoi  路  7Comments

phalt picture phalt  路  8Comments

cmmartti picture cmmartti  路  4Comments

neverendingqs picture neverendingqs  路  5Comments