{ "id": 25, "name": "pikachu", "base_experience": 112, "height": 4, "is_default": true, "order": 35, "weight": 60, "abilities": [ { "is_hidden": false, "slot": 1, "ability": { "name": "static", "url": "https://pokeapi.co/api/v2/ability/9/" } } ], "forms": [ { "name": "pikachu", "url": "https://pokeapi.co/api/v2/pokemon-form/25/" } ]}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}`); // 25To run this file, open your terminal and press node app.js followed by Enter.
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)Async/AwaitPromiseCallback Fetch Pokemon data by string name or integer ID.getPokemonSpeciesByName(nameOrId)Async/AwaitPromiseCallback Fetch species data (including language translations, evolution chains, and descriptions).
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);}import Pokedex from 'pokedex-promise-v2';const P = new Pokedex();
// Passing an array fetches multiple records simultaneouslyP.getPokemonByName(['eevee', 'ditto']) .then((response) => { console.log(response); // Array containing Eevee and Ditto data }) .catch((error) => { console.error('Error fetching Pokémon:', error); });import Pokedex from 'pokedex-promise-v2';const P = new Pokedex();
// You can pass an integer ID instead of a string nameP.getPokemonByName(34, (response, error) => { if (!error) { console.log(`Pokémon Name: ${response.name}`); // nidoking } else { console.error(error); }});View Pikachu Query JSON Response Payload Structure
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);