pokedex-promise V2 Guide
pokedex-promise-v2 is a popular, lightweight Node.js client wrapper for PokéAPI. It features built-in in-memory caching, automatic rate limiting fallback configurations, and support for Promises, Async/Await, and Callbacks.
This guide will walk you through installing the package, configuring the client, and fetching Pokémon data.
Getting Started
Section titled “Getting Started”Follow these steps to integrate pokedex-promise-v2 into your Node.js application:
-
Install the package Choose your preferred package manager to add
pokedex-promise-v2to your project:Terminal npm install pokedex-promise-v2Terminal yarn add pokedex-promise-v2Terminal pnpm i pokedex-promise-v2 -
Configure your project for ESM Since version
4.0.0,pokedex-promise-v2is a pure ES Modules (ESM) package Required. -
Initialize the client Import the package and initialize a new
Pokedexinstance.app.js import Pokedex from 'pokedex-promise-v2';const P = new Pokedex(); -
Run your first query Retrieve information for a Pokémon by name or ID.
app.js import Pokedex from 'pokedex-promise-v2';const P = new Pokedex();const pikachu = await P.getPokemonByName("pikachu");console.log(`Pokedex ID: ${pikachu.id}`);console.log(`Base Experience: ${pikachu.base_experience}`);console.log(`Primary Type: ${pikachu.types[0].type.name}`);Run the file with Node:
Pokedex ID: 25Base Experience: 112Primary Type: electric
Querying Patterns
Section titled “Querying Patterns”pokedex-promise-v2 is highly flexible. You can query endpoints using Async/Await, Promises, or traditional Callbacks. You can also fetch single records or retrieve arrays of records in a single call.
1. Main Fetching Methods
Section titled “1. Main Fetching Methods”getPokemonByName(nameOrId): Fetch complete Pokémon data (stats, abilities, types, sprites) by name string (e.g.'pikachu') or integer ID (e.g.25).getPokemonSpeciesByName(nameOrId): Fetch species-level data (flavor text descriptions, evolution chain URLs, multilingual name translations, generation info).
2. Implementation Patterns
Section titled “2. Implementation Patterns”import Pokedex from 'pokedex-promise-v2';const P = new Pokedex();
try { const species = await P.getPokemonSpeciesByName("golduck"); const frenchName = species.names.find(n => n.language.name === 'fr').name; console.log(`French name of Golduck: ${frenchName}`);} catch (error) { console.error("Failed to fetch Pokémon species data:", error);}French name of Golduck: Akwakwakimport Pokedex from 'pokedex-promise-v2';const P = new Pokedex();
// Passing an array fetches multiple records simultaneouslyP.getPokemonByName(['eevee', 'ditto']) .then((results) => { results.forEach((poke) => { console.log(`${poke.name} (ID: ${poke.id}) - Weight: ${poke.weight}`); }); }) .catch((error) => { console.error('Error fetching Pokémon:', error); });eevee (ID: 133) - Weight: 65ditto (ID: 132) - Weight: 40import Pokedex from 'pokedex-promise-v2';const P = new Pokedex();
// You can pass an integer ID or name, followed by a (response, error) callbackP.getPokemonByName(34, (response, error) => { if (!error) { console.log(`Pokémon Name: ${response.name} (Height: ${response.height})`); } else { console.error(error); }});Pokémon Name: nidoking (Height: 14)3. Pagination & Resource Lists
Section titled “3. Pagination & Resource Lists”Root resource list endpoints (such as getPokemonsList, getBerriesList, and getItemsList) accept an optional interval object to paginate through results using limit and offset:
import Pokedex from 'pokedex-promise-v2';const P = new Pokedex();
const interval = { limit: 5, // Number of items to retrieve offset: 33, // Starting index (0-indexed)};
const pokemonList = await P.getPokemonsList(interval);console.log(`Total Pokemon: ${pokemonList.count}`);console.log(`Results:`);pokemonList.results.forEach((pokemon) => { console.log(` - ${pokemon.name} (${pokemon.url})`);});Total Pokemon: 1302Results: - nidoking (https://pokeapi.co/api/v2/pokemon/34/) - clefairy (https://pokeapi.co/api/v2/pokemon/35/) - clefable (https://pokeapi.co/api/v2/pokemon/36/) - vulpix (https://pokeapi.co/api/v2/pokemon/37/) - ninetales (https://pokeapi.co/api/v2/pokemon/38/)Always specify limit and offset
By default, if you call list endpoints without passing an interval object, pokedex-promise-v2 attempts to fetch every single item in the database and cache the entire dataset into your application’s RAM. Always provide an explicit { limit, offset } when querying large resource collections!
TypeScript Support
Section titled “TypeScript Support”pokedex-promise-v2 ships with built-in TypeScript type definitions out of the box no separate @types/* package is required.
When working in TypeScript, method responses are fully typed according to the official PokéAPI schemas:
import Pokedex from 'pokedex-promise-v2';
const P = new Pokedex();
// Return values are fully typedconst pokemon = await P.getPokemonByName('pikachu');console.log(pokemon.name); // stringconsole.log(pokemon.weight); // number
// Access specific return types using TypeScript utility typestype PokemonData = Awaited<ReturnType<Pokedex['getPokemonByName']>>;
function logPokemonAbilities(data: PokemonData) { for (const item of data.abilities) { console.log(`Ability: ${item.ability.name} (Hidden: ${item.is_hidden})`); }}
logPokemonAbilities(pokemon);pikachu (Weight: 60)Ability: static (Hidden: false)Ability: lightning-rod (Hidden: true)Advanced Configurations
Section titled “Advanced Configurations”By default, the client is initialized to target the public API endpoint (https://pokeapi.co/api/v2/), timeout after 20 seconds, and cache responses in memory for 11 days.
You can customize this behavior by passing a configuration object to the constructor:
import Pokedex from 'pokedex-promise-v2';
const customOptions = { protocol: 'https', hostName: 'pokeapi.co', versionPath: '/api/v2/', cacheLimit: 5 * 60 * 1000, // Cache for 5 minutes (in milliseconds) timeout: 5000 // Timeout after 5 seconds (in milliseconds)};
const P = new Pokedex(customOptions);